2016-10-03 28 views
0

我能夠從github回購中獲得csharp-sample-appsAffectiva。我使用網絡攝像頭運行了演示程序,處理和性能非常好。當我嘗試在文件系統中的圖像上運行時,我沒有從PhotoDetector獲得相同的處理速度。任何幫助或改進將不勝感激。與網絡攝像頭相比,對文件系統中圖像的圖像處理速度慢

namespace Logical.EmocaoFace 
{ 
    public class AnaliseEmocao : Affdex.ImageListener, Affdex.ProcessStatusListener 
    { 
     private Bitmap img { get; set; } 
     private Dictionary<int, Affdex.Face> faces { get; set; } 
     private Affdex.Detector detector { get; set; } 
     private ReaderWriterLock rwLock { get; set; } 

     public void processaEmocaoImagem() 
     { 

      for (int i = 0; i < resultado.count; i++){ 

       RetornaEmocaoFace(); 

       if (faceAffdex != null) 
       { 

       } 
      } 
     } 


     public void RetornaEmocaoFace(string caminhoImagem) 
     { 
      Affdex.Detector detector = new Affdex.PhotoDetector(1, Affdex.FaceDetectorMode.LARGE_FACES); 
      detector.setImageListener(this); 
      detector.setProcessStatusListener(this); 

      if (detector != null) 
      { 
       //ProcessVideo videoForm = new ProcessVideo(detector); 
       detector.setClassifierPath(@"D:\Desenvolvimento\Componentes\Afectiva\data"); 
       detector.setDetectAllEmotions(true); 
       detector.setDetectAllExpressions(false); 
       detector.setDetectAllEmojis(false); 
       detector.setDetectAllAppearances(false); 
       detector.start(); 

       ((Affdex.PhotoDetector)detector).process(LoadFrameFromFile(caminhoImagem)); 

       detector.stop(); 
      } 
     } 

     static Affdex.Frame LoadFrameFromFile(string fileName) 
     { 
      Bitmap bitmap = new Bitmap(fileName); 

      // Lock the bitmap's bits. 
      Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); 
      BitmapData bmpData = bitmap.LockBits(rect, ImageLockMode.ReadWrite, bitmap.PixelFormat); 

      // Get the address of the first line. 
      IntPtr ptr = bmpData.Scan0; 

      // Declare an array to hold the bytes of the bitmap. 
      int numBytes = bitmap.Width * bitmap.Height * 3; 
      byte[] rgbValues = new byte[numBytes]; 

      int data_x = 0; 
      int ptr_x = 0; 
      int row_bytes = bitmap.Width * 3; 

      // The bitmap requires bitmap data to be byte aligned. 
      // http://stackoverflow.com/questions/20743134/converting-opencv-image-to-gdi-bitmap-doesnt-work-depends-on-image-size 

      for (int y = 0; y < bitmap.Height; y++) 
      { 
       Marshal.Copy(ptr + ptr_x, rgbValues, data_x, row_bytes);//(pixels, data_x, ptr + ptr_x, row_bytes); 
       data_x += row_bytes; 
       ptr_x += bmpData.Stride; 
      } 

      bitmap.UnlockBits(bmpData); 

      //Affdex.Frame retorno = new Affdex.Frame(bitmap.Width, bitmap.Height, rgbValues, Affdex.Frame.COLOR_FORMAT.BGR); 

      //bitmap.Dispose(); 

      //return retorno; 

      return new Affdex.Frame(bitmap.Width, bitmap.Height, rgbValues, Affdex.Frame.COLOR_FORMAT.BGR); 
     } 

     public void onImageCapture(Affdex.Frame frame) 
     { 
      frame.Dispose(); 

     } 

     public void onImageResults(Dictionary<int, Affdex.Face> faces, Affdex.Frame frame) 
     { 
      byte[] pixels = frame.getBGRByteArray(); 
      this.img = new Bitmap(frame.getWidth(), frame.getHeight(), PixelFormat.Format24bppRgb); 
      var bounds = new Rectangle(0, 0, frame.getWidth(), frame.getHeight()); 
      BitmapData bmpData = img.LockBits(bounds, ImageLockMode.WriteOnly, img.PixelFormat); 
      IntPtr ptr = bmpData.Scan0; 

      int data_x = 0; 
      int ptr_x = 0; 
      int row_bytes = frame.getWidth() * 3; 

      // The bitmap requires bitmap data to be byte aligned. 
      // http://stackoverflow.com/questions/20743134/converting-opencv-image-to-gdi-bitmap-doesnt-work-depends-on-image-size 

      for (int y = 0; y < frame.getHeight(); y++) 
      { 
       Marshal.Copy(pixels, data_x, ptr + ptr_x, row_bytes); 
       data_x += row_bytes; 
       ptr_x += bmpData.Stride; 
      } 
      img.UnlockBits(bmpData); 

      this.faces = faces; 

      frame.Dispose(); 
     } 

     public void onProcessingException(Affdex.AffdexException A_0) 
     { 
      throw new NotImplementedException("Encountered an exception while processing " + A_0.ToString()); 
     } 

     public void onProcessingFinished() 
     { 
      string idArquivo = CodEspaco + "," + System.Guid.NewGuid().ToString(); 

      for(int i = 0; i < faces.Count; i++) 
      { 
      } 
     } 



    } 

    public static class GraphicsExtensions 
    { 
     public static void DrawCircle(this Graphics g, Pen pen, 
             float centerX, float centerY, float radius) 
     { 
      g.DrawEllipse(pen, centerX - radius, centerY - radius, 
          radius + radius, radius + radius); 
     } 
    } 
} 

回答

0

找到了答案,以我自己的問題:

使用PhotoDetector是不是在這種情況下,理想的,因爲它是用在下一幀調用臉部檢測裝置配置昂貴。

提高性能的最佳選擇是使用FrameDetector類的實例。

以下是analyze-frames的入門指南。

相關問題