Esempio n. 1
0
        public void GetFeatureVec(object obj)
        {
            FeatureData feature = (FeatureData)obj;

            using (Bitmap bitmapSource = feature.localRead ? new Bitmap(feature.localFileName) : videoSourcePlayer.GetCurrentVideoFrame())
            {
                if (bitmapSource != null)
                {
                    //Bitmap bitmapSource = BitmapHelper.ScaleToSize(bitmapVideo, 256, 256);
                    float[]    vec        = new float[FaceApi.FeatureVecLen()];
                    int[]      landmark   = new int[FaceApi.LandMarkLen()];
                    BitmapData bitmapData = bitmapSource.LockBits(new Rectangle(0, 0, bitmapSource.Width, bitmapSource.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                    while (isDetectrunning)
                    {
                        Console.WriteLine("waitting Detect");
                    }
                    ;
                    isDetectrunning = true;
                    bool success = FaceApi.FindFacePtr(bitmapData.Scan0, bitmapData.Width, bitmapData.Height, bitmapData.Stride, 3, out int x1, out int y1, out int width, out int height, landmark);
                    isDetectrunning = false;
                    bitmapSource.UnlockBits(bitmapData);
                    if (success)
                    {
                        bitmapData = bitmapSource.LockBits(new Rectangle(x1, y1, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                        for (int idx = 0; idx < landmark.Length / 2; ++idx)
                        {
                            landmark[idx * 2]     -= x1;
                            landmark[idx * 2 + 1] -= y1;
                        }
                        while (isFeaRunning)
                        {
                            Console.WriteLine("waitting FeaRunning");
                        }
                        ;
                        isFeaRunning = true;
                        success      = FaceApi.FeatureVecPtr(bitmapData.Scan0, bitmapData.Width, bitmapData.Height, bitmapData.Stride, 3, landmark, vec);
                        isFeaRunning = false;
                        bitmapSource.UnlockBits(bitmapData);
                    }
                    if (success)
                    {
                        float sum = 0;
                        for (int idx = 0; idx < vec.Length; ++idx)
                        {
                            sum += vec[idx];
                        }
                        SetIDBox(feature, sum + "", vec, bitmapSource.Clone(new Rectangle(x1, y1, width, height), PixelFormat.Format24bppRgb));
                    }
                    else
                    {
                        MessageBox.Show("添加数据失败!");
                    }
                }
            }
        }
Esempio n. 2
0
 public void play_video()
 {
     flag = true;
     SetTestBox("正在进行人脸检测");
     while (flag)
     {
         try
         {
             using (Bitmap bitmapSource = videoSourcePlayer.GetCurrentVideoFrame())
             {
                 if (bitmapSource != null)
                 {
                     if (!isDetectrunning)
                     {
                         BitmapData bitmapData = bitmapSource.LockBits(new Rectangle(0, 0, bitmapSource.Width, bitmapSource.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                         int[]      landmark   = new int[FaceApi.LandMarkLen()];
                         isDetectrunning = true;
                         bool success = FaceApi.FindFacePtr_(bitmapData.Scan0, bitmapData.Width, bitmapData.Height, bitmapData.Stride, 3, out int x1, out int y1, out int x2, out int y2, landmark);
                         isDetectrunning = false;
                         bitmapSource.UnlockBits(bitmapData);
                         if (success)
                         {
                             if (!closing)
                             {
                                 if (facedata.Count != 0)
                                 {
                                     if (matchingThread == null || matchingThread.ThreadState != ThreadState.Running)
                                     {
                                         for (int idx = 0; idx < landmark.Length / 2; ++idx)
                                         {
                                             landmark[idx * 2]     -= x1;
                                             landmark[idx * 2 + 1] -= y1;
                                         }
                                         MatchData matchData = new MatchData(bitmapSource.Clone(new Rectangle(x1, y1, x2 - x1, y2 - y1), PixelFormat.Format24bppRgb), landmark);
                                         matchingThread = new Thread(new ParameterizedThreadStart(Matching));
                                         matchingThread.Start(matchData);
                                     }
                                 }
                                 SetSearchBox(bitmapSource.Clone(new Rectangle(x1, y1, x2 - x1, y2 - y1), PixelFormat.Format24bppRgb));
                                 SetBox("0");
                             }
                         }
                         else
                         {
                             if (!closing)
                             {
                                 SetBox("-6");
                             }
                         }
                     }
                     else
                     {
                         if (!closing)
                         {
                             SetBox("-6");
                         }
                     }
                 }
                 else
                 {
                     break;
                 }
             }
         }
         catch (Exception ex)
         {
             if (!closing)
             {
                 SetBox("摄像头异常:" + ex.Message);
             }
         }
     }
     if (!closing)
     {
         SetBox("连接摄像头");
         SetTestBox("已停止人脸检测");
     }
 }