【问题标题】:Slow image processing of images from filesystem as compared to the webcam与网络摄像头相比,来自文件系统的图像处理速度较慢
【发布时间】:2016-10-13 02:20:42
【问题描述】:

我能够从Affectiva 的 github 存储库中关注csharp-sample-apps。我使用我的网络摄像头运行演示,处理和性能都很棒。当我尝试在文件系统中的图像上运行它时,我没有从 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);
        }
    }
}

【问题讨论】:

    标签: affdex-sdk


    【解决方案1】:

    找到了我自己问题的答案:

    在这种情况下使用PhotoDetector 并不理想,因为在后续帧调用中使用人脸检测器配置的成本很高。

    提高性能的最佳选择是使用FrameDetector 类的实例。

    这是analyze-frames 的入门指南。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-30
      • 1970-01-01
      • 1970-01-01
      • 2020-10-15
      • 2021-01-02
      相关资源
      最近更新 更多