Example #1
0
        /// <summary>
        /// 比对函数,将每一帧抓拍的照片和目标人物照片进行比对
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        private void CompareImgWithIDImg(Bitmap bitmap, PaintEventArgs e)
        {
            if (bitmap != null)
            {
                //保证只检测一帧,防止页面卡顿以及出现其他内存被占用情况
                if (isLock == false)
                {
                    isLock = true;
                    Graphics g       = e.Graphics;
                    float    offsetX = (pic_Video.Width * 1f / bitmap.Width);
                    float    offsetY = (pic_Video.Height * 1f / bitmap.Height);
                    //根据Bitmap 获取人脸信息列表
                    List <FaceInfoModel> list = FaceUtil.GetFaceInfos(pImageEngine, bitmap);
                    foreach (FaceInfoModel sface in list)
                    {
                        //异步处理提取特征值和比对,不然页面会比较卡
                        ThreadPool.QueueUserWorkItem(new WaitCallback(delegate
                        {
                            try
                            {
                                //提取人脸特征
                                float similarity = CompareTwoFeatures(sface.feature, imageTemp);
                                if (similarity > threshold)
                                {
                                    this.pic_cutImg.Image = bitmap;

                                    this.Invoke((Action)(() =>
                                    {
                                        this.lbl_simiValue.Text = similarity.ToString();
                                    }));
                                }
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.Message);
                            }
                        }));


                        MRECT rect   = sface.faceRect;
                        float x      = rect.left * offsetX;
                        float width  = rect.right * offsetX - x;
                        float y      = rect.top * offsetY;
                        float height = rect.bottom * offsetY - y;
                        //根据Rect进行画框
                        g.DrawRectangle(pen, x, y, width, height);
                        trackUnit.message = "年龄:" + sface.age.ToString() + "\r\n" + "性别:" + (sface.gender == 0 ? "男" : "女");
                        g.DrawString(trackUnit.message, font, brush, x, y + 5);
                    }
                    isLock = false;
                }
            }
        }