Example #1
0
        private void TimerDetector_Tick(object sender, EventArgs e)
        {
            //label1.Text = "";
            if (VideoPlayer.IsRunning)
            {
                Bitmap bitmap = VideoPlayer.GetCurrentVideoFrame(); // 获取摄像头画面

                if (bitmap != null)
                {
                    var infos = ViewFace.FaceDetector(bitmap); // 识别画面中的人脸
                    FaceRectangles.Clear();
                    var test = ViewFace.FaceType;

                    foreach (var info in infos)
                    {
                        FaceRectangles.Add(new Rectangle(info.Location.X, info.Location.Y, info.Location.Width, info.Location.Height));

                        // 识别 bitmap 中指定的人脸信息 info 的关键点坐标。
                        var faceps = ViewFace.FaceMark(bitmap, info);
                        var s      = ViewFace.Extract(bitmap, faceps);
                        //bitmap.Save($"{DateTime.Now.Ticks}.jpg");

                        personInfos.Where(x => true || x.Name == "刘松").ToList().ForEach(x =>
                        {
                            float similarity = ViewFace.Similarity(x.oldEigenValues, s);      // 对比两张照片上的数据,确认是否是同一个人。


                            if (ViewFace.IsSelf(similarity))
                            {
                                label1.Text = $"{x.Name} ";

                                //label1.Text = $"{similarity} ";
                            }
                        });
                    }
                    if (FaceRectangles.Count > 0)
                    {
                        using (Graphics g = Graphics.FromImage(bitmap))
                        {
                            g.DrawRectangles(new Pen(Color.Red, 4), FaceRectangles.ToArray());
                        }
                    }
                }
                FacePictureBox.Image = bitmap;
            }
        }
Example #2
0
 private void btnTakePhoto_Click(object sender, EventArgs e)
 {
     if (videoSource == null)
     {
         MessageBox.Show("请先单击下方图标启动摄像头!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
     else
     {
         PictureName = string.Concat(txtStudentNo.Text.Trim(), ".jpg");
         Bitmap bitmap = VideoPlayer.GetCurrentVideoFrame();
         bitmap.Save(string.Concat(InitSettings.GetPicturePath, PictureName), ImageFormat.Jpeg);
         bitmap.Dispose();
         VideoPlayer.Stop();
         VideoPlayer.Visible     = false;
         pbPreview.Visible       = true;
         pbPreview.ImageLocation = string.Concat(InitSettings.GetPicturePath, PictureName);
     }
 }
Example #3
0
 /// <summary>
 /// 每 100 ms 检测一次人脸
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void TimerDetector_Tick(object sender, EventArgs e)
 {
     if (VideoPlayer.IsRunning)
     {
         Bitmap bitmap = VideoPlayer.GetCurrentVideoFrame(); // 获取摄像头画面
         if (bitmap != null)
         {
             var infos = ViewFace.FaceDetector(bitmap); // 识别画面中的人脸
             FaceRectangles.Clear();
             foreach (var info in infos)
             {
                 FaceRectangles.Add(new Rectangle(info.Location.X, info.Location.Y, info.Location.Width, info.Location.Height));
             }
             if (FaceRectangles.Count > 0) // 如果有人脸,在 bitmap 上绘制出人脸的位置信息
             {
                 using (Graphics g = Graphics.FromImage(bitmap))
                 {
                     g.DrawRectangles(new Pen(Color.Red, 4), FaceRectangles.ToArray());
                 }
             }
         }
         FacePictureBox.Image = bitmap;
     }
 }
Example #4
0
        /// <summary>
        /// 持续检测一次人脸,直到停止。
        /// </summary>
        /// <param name="token">取消标记</param>
        private async void StartDetector(CancellationToken token)
        {
            List <double> fpsList      = new List <double>();
            double        fps          = 0;
            Stopwatch     stopwatchFPS = new Stopwatch();
            Stopwatch     stopwatch    = new Stopwatch();

            while (VideoPlayer.IsRunning && !token.IsCancellationRequested)
            {
                if (CheckBoxFPS.Checked)
                {
                    stopwatch.Restart();
                    if (!stopwatchFPS.IsRunning)
                    {
                        stopwatchFPS.Start();
                    }
                }
                Bitmap bitmap = VideoPlayer.GetCurrentVideoFrame(); // 获取摄像头画面
                if (bitmap != null)
                {
                    FaceRectangles.Clear(); Ages.Clear(); Pids.Clear();
                    if (CheckBoxDetect.Checked)
                    {
                        var infos = await ViewFace.FaceTrackAsync(bitmap); // 识别画面中的人脸

                        foreach (var info in infos)
                        {
                            FaceRectangles.Add(info.Location);
                            Pids.Add(info.Pid);
                            if (CheckBoxFaceProperty.Checked)
                            {
                                Ages.Add(await ViewFace.FaceAgePredictorAsync(bitmap, await ViewFace.FaceMarkAsync(bitmap, new ViewFaceCore.Sharp.Model.FaceInfo()
                                {
                                    Location = info.Location, Score = info.Score
                                })));
                                Gender.Add((await ViewFace.FaceGenderPredictorAsync(bitmap, await ViewFace.FaceMarkAsync(bitmap, new ViewFaceCore.Sharp.Model.FaceInfo()
                                {
                                    Location = info.Location, Score = info.Score
                                }))).ToDescription());
                            }
                        }
                    }
                    else
                    {
                        await Task.Delay(1000 / 60);
                    }
                    using (Graphics g = Graphics.FromImage(bitmap))
                    {
                        if (FaceRectangles.Any()) // 如果有人脸,在 bitmap 上绘制出人脸的位置信息
                        {
                            g.DrawRectangles(new Pen(Color.Red, 4), FaceRectangles.ToArray());
                            if (CheckBoxDetect.Checked && CheckBoxFaceProperty.Checked)
                            {
                                string pid = "";
                                for (int i = 0; i < FaceRectangles.Count; i++)
                                {
                                    if (Pids.Any())
                                    {
                                        pid = $"| Pid: {Pids[i]}";
                                    }
                                    g.DrawString($"{Ages[i]} 岁 | {Gender[i]} {pid}", new Font("微软雅黑", 24), Brushes.Green, new PointF(FaceRectangles[i].X + FaceRectangles[i].Width + 24, FaceRectangles[i].Y));
                                }
                            }
                        }

                        if (CheckBoxFPS.Checked)
                        {
                            stopwatch.Stop();

                            if (numericUpDownFPSTime.Value > 0)
                            {
                                fpsList.Add(1000f / stopwatch.ElapsedMilliseconds);
                                if (stopwatchFPS.ElapsedMilliseconds >= numericUpDownFPSTime.Value)
                                {
                                    fps = fpsList.Average();
                                    fpsList.Clear();
                                    stopwatchFPS.Reset();
                                }
                            }
                            else
                            {
                                fps = 1000f / stopwatch.ElapsedMilliseconds;
                            }
                            g.DrawString($"{fps:#.#} FPS", new Font("微软雅黑", 24), Brushes.Green, new Point(10, 10));
                        }
                    }
                }
                else
                {
                    await Task.Delay(10);
                }
                FacePictureBox.Image?.Dispose();
                FacePictureBox.Image = bitmap;
            }

            VideoPlayer?.SignalToStop();
            VideoPlayer?.WaitForStop();
            if (IsClose)
            {
                Close();
            }
        }