Beispiel #1
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();
            }
        }