예제 #1
0
        public PhotoWindow()
        {
            InitializeComponent();
            uint maxNumFaces = 1;//最多识别图片中几张脸

            Detector = new Affdex.PhotoDetector(maxNumFaces, Affdex.FaceDetectorMode.SMALL_FACES);
            Detector.setImageListener(this);
            Detector.start();

            byte[]       bytes        = FileHelper.FileToBytes(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "timg.jpg"));
            BitmapSource bitmapSource = ImageHelper.BytesToBitmapImage(bytes);
            var          w            = bitmapSource.Width;
            var          h            = bitmapSource.Height;
            var          stride       = bitmapSource.Format.BitsPerPixel * (int)w / 8; //计算Stride

            byte[] byteList = new byte[(int)h * stride];
            bitmapSource.CopyPixels(byteList, stride, 0);
            Affdex.Frame frame = new Affdex.Frame((int)w, (int)h, byteList, Affdex.Frame.COLOR_FORMAT.BGRA);
            Detector.process(frame);
        }
예제 #2
0
        public PhotoWindowAdvance()
        {
            InitializeComponent();
            uint maxNumFaces = 1;//最多识别图片中几张脸

            Detector = new Affdex.PhotoDetector(maxNumFaces, Affdex.FaceDetectorMode.SMALL_FACES);
            //Set location of the classifier data files, needed by the SDK
            Detector.setClassifierPath("C:\\Program Files\\Affectiva\\AffdexSDK\\data");
            //String newPath = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "data"); //可以使用其它路径,但需要把Data复制过去
            //Detector.setClassifierPath(newPath);

            //跟踪一些 我们预先设置的的分类器,比如开心,讨厌等等
            Detector.setDetectAllEmotions(false);
            Detector.setDetectAllExpressions(false);
            Detector.setDetectAllEmojis(true);
            Detector.setDetectGender(true);
            Detector.setDetectGlasses(true);

            //以下为分类器
            Detector.setDetectJoy(true);
            Detector.setDetectSadness(true);
            Detector.setDetectAnger(true);
            Detector.setDetectDisgust(true);
            Detector.setDetectSurprise(true);
            Detector.setDetectFear(true);


            Detector.setImageListener(this);//设置两个监听
            Detector.setProcessStatusListener(this);
            Detector.start();


            //Bitmap bmpt = (Bitmap)System.Drawing.Image.FromFile(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "timg.jpg"));
            //bmpt.Save(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "timg_1.png"));

            //==================================================================================================================================
            byte[]       bytes        = FileHelper.FileToBytes(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "timg.jpg"));
            BitmapSource bitmapSource = ImageHelper.BytesToBitmapImage(bytes);


            //var imageSrc = bitmapSource;
            //System.Windows.Media.Imaging.BitmapEncoder encoder = new System.Windows.Media.Imaging.PngBitmapEncoder();
            //encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(imageSrc));
            //using (var stream = new System.IO.FileStream(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory,
            //    "timg_new.png"), System.IO.FileMode.Create))
            //{
            //    encoder.Save(stream); 导出 bitmapsource已经存储成功 证明图片是正常导入进了bitmapsource
            //}
            //==================================================================================================================================
            var w      = bitmapSource.Width;
            var h      = bitmapSource.Height;
            var stride = bitmapSource.Format.BitsPerPixel * (int)w / 8; //计算Stride

            byte[] byteList = new byte[(int)h * stride];
            bitmapSource.CopyPixels(byteList, stride, 0);  //调用CopyPixels
            Affdex.Frame frame = new Affdex.Frame((int)w, (int)h, byteList, Affdex.Frame.COLOR_FORMAT.BGRA);
            //==================================================================================================================================
            //var len = frame.getBGRByteArrayLength();
            //byte[] imageData = frame.getBGRByteArray();
            //Console.WriteLine($"onImageCapture帧的buf len{imageData.Length} len{len}");
            //int width = frame.getWidth();
            //int height = frame.getHeight();
            //var ColorFormat = frame.getColorFormat();

            //if (imageData != null && imageData.Length > 0)
            //{
            //    var _stride = (width * System.Windows.Media.PixelFormats.Rgb24.BitsPerPixel + 7) / 8;//_stride 1791
            //    Console.WriteLine($"_stride{_stride}");
            //    var imageSrc = System.Windows.Media.Imaging.BitmapSource.Create(width, height, 96d, 96d, System.Windows.Media.PixelFormats.Bgr24,
            //        null, imageData, _stride);

            //    System.Windows.Media.Imaging.BitmapEncoder encoder = new System.Windows.Media.Imaging.PngBitmapEncoder();
            //    encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(imageSrc));

            //    using (var stream =
            //    new System.IO.FileStream(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory,
            //        "我是分析前.png"), System.IO.FileMode.Create))
            //    {
            //        encoder.Save(stream);
            //    }
            //}
            //==================================================================================================================================
            Detector.process(frame);
        }