Exemplo n.º 1
0
        public bool MatchBitmap(Bitmap bitmap, FaceModel fm)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            #region 图片转换 0.7-2微妙
            var bmpData   = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
            var imageData = new ImageData
            {
                u32PixelArrayFormat = 513,//Rgb24,
                i32Width            = bitmap.Width,
                i32Height           = bitmap.Height,
                pi32Pitch           = new int[4],
                ppu8Plane           = new IntPtr[4]
            };
            imageData.pi32Pitch[0] = bmpData.Stride;
            imageData.ppu8Plane[0] = bmpData.Scan0;

            sw.Stop();
            faceResult.Score = sw.ElapsedTicks;

            #endregion

            try
            {
                #region 人脸检测 5-8毫秒
                DetectResult pDetectResult = new DetectResult();
                var          ret           = (ErrorCode)Detect.Detection(faceDetectEnginer, ref imageData, out pDetectResult.lfaceOrient);
                if (ret != ErrorCode.Ok)
                {
                    return(false);
                }
                var detectResult = (DetectResult)Marshal.PtrToStructure(pDetectResult.lfaceOrient, typeof(DetectResult));
                if (detectResult.nFace == 0)
                {
                    return(false);
                }
                var faceRect = (FaceRect)Marshal.PtrToStructure(detectResult.rcFace, typeof(FaceRect));
                faceResult.Rectangle    = new Rectangle((int)(faceRect.left * rateW), (int)(faceRect.top * rateH), (int)((faceRect.right - faceRect.left) * rateW), (int)((faceRect.bottom - faceRect.top) * rateH));
                faceResult.RealRetangle = new Rectangle(faceRect.left, faceRect.top, (faceRect.right - faceRect.left), (faceRect.bottom - faceRect.top));
                var faceOrient = (int)Marshal.PtrToStructure(detectResult.lfaceOrient, typeof(int));
                #endregion

                #region 获取人脸特征 160-180毫秒
                var faceFeatureInput = new FaceFeatureInput
                {
                    rcFace  = faceRect,
                    lOrient = faceOrient
                };

                var faceModel = new FaceModel();
                ret = (ErrorCode)Match.ExtractFeature(faceMatchEngine, ref imageData, ref faceFeatureInput, out faceModel);
                #endregion

                if (ret == ErrorCode.Ok)
                {
                    if (registerClicked)
                    {
                        this.registerFeatureData = new byte[faceModel.lFeatureSize];
                        Marshal.Copy(faceModel.pbFeature, registerFeatureData, 0, faceModel.lFeatureSize);
                    }

                    #region 人脸识别(100张人脸) 17-20毫秒
                    float score = 0;
                    Match.FacePairMatch(faceMatchEngine, ref fm, ref faceModel, out score);
                    if (score > 0.5)
                    {
                        return(true);
                    }
                    #endregion
                }
            }
            finally
            {
                bitmap.UnlockBits(bmpData);
            }
            return(false);
        }
Exemplo n.º 2
0
        private void MatchFrame()
        {
            #region 获取图片 1毫秒
            var bitmap = this.VideoPlayer.GetCurrentVideoFrame();
            #endregion

            Stopwatch sw = new Stopwatch();
            sw.Start();
            #region 图片转换 0.7-2微妙
            var bmpData   = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
            var imageData = new ImageData
            {
                u32PixelArrayFormat = 513,//Rgb24,
                i32Width            = bitmap.Width,
                i32Height           = bitmap.Height,
                pi32Pitch           = new int[4],
                ppu8Plane           = new IntPtr[4]
            };
            imageData.pi32Pitch[0] = bmpData.Stride;
            imageData.ppu8Plane[0] = bmpData.Scan0;

            sw.Stop();
            faceResult.Score = sw.ElapsedTicks;


            #endregion

            try
            {
                #region 人脸检测 5-8毫秒
                DetectResult pDetectResult = new DetectResult();
                var          ret           = (ErrorCode)Detect.Detection(faceDetectEnginer, ref imageData, out pDetectResult.lfaceOrient);
                if (ret != ErrorCode.Ok)
                {
                    return;
                }
                var detectResult = (DetectResult)Marshal.PtrToStructure(pDetectResult.lfaceOrient, typeof(DetectResult));
                if (detectResult.nFace == 0)
                {
                    return;
                }
                var faceRect = (FaceRect)Marshal.PtrToStructure(detectResult.rcFace, typeof(FaceRect));
                faceResult.Rectangle    = new Rectangle((int)(faceRect.left * rateW), (int)(faceRect.top * rateH), (int)((faceRect.right - faceRect.left) * rateW), (int)((faceRect.bottom - faceRect.top) * rateH));
                faceResult.RealRetangle = new Rectangle(faceRect.left, faceRect.top, (faceRect.right - faceRect.left), (faceRect.bottom - faceRect.top));
                var faceOrient = (int)Marshal.PtrToStructure(detectResult.lfaceOrient, typeof(int));
                #endregion


                #region 性别识别基本准确 年龄识别误差太大,没什么应用场景
                //ExtraFaceInput faceInput = new ExtraFaceInput()
                //{
                //    lFaceNumber = facesDetect.nFace,
                //    pFaceRectArray = Marshal.AllocHGlobal(Marshal.SizeOf(faceRect)),
                //    pFaceOrientArray = Marshal.AllocHGlobal(Marshal.SizeOf(faceOrient))
                //};
                //Marshal.StructureToPtr(faceRect, faceInput.pFaceRectArray, false);
                //Marshal.StructureToPtr(faceOrient, faceInput.pFaceOrientArray, false);

                //var ageResult = Face.Age.ASAE_FSDK_AgeEstimation_Preview(faceAgeEngine, ref imageData, ref faceInput, out var pAgeRes);
                //var ages = pAgeRes.pResult.ToStructArray<int>(pAgeRes.lFaceNumber);
                //var genderResult = Face.Gender.ASGE_FSDK_GenderEstimation_Preview(faceGenderEngine, ref imageData, ref faceInput, out var pGenderRes);
                //var genders = pGenderRes.pResult.ToStructArray<int>(pGenderRes.lFaceNumber);
                //faceResult.Age = ages[0];
                //faceResult.Gender = genders[0];

                //Marshal.FreeHGlobal(faceInput.pFaceOrientArray);
                //Marshal.FreeHGlobal(faceInput.pFaceRectArray);
                #endregion

                #region 获取人脸特征 160-180毫秒
                var faceFeatureInput = new FaceFeatureInput
                {
                    rcFace  = faceRect,
                    lOrient = faceOrient
                };

                var faceModel = new FaceModel();
                ret = (ErrorCode)Match.ExtractFeature(faceMatchEngine, ref imageData, ref faceFeatureInput, out faceModel);
                #endregion

                if (ret == ErrorCode.Ok)
                {
                    if (registerClicked)
                    {
                        this.registerFeatureData = new byte[faceModel.lFeatureSize];
                        Marshal.Copy(faceModel.pbFeature, registerFeatureData, 0, faceModel.lFeatureSize);
                    }

                    #region 人脸识别(100张人脸) 17-20毫秒
                    bool isdetected = false;
                    foreach (var item in this.Faces.OrderByDescending(i => i.OrderId))
                    {
                        var   fm    = item.FaceModel;
                        float score = 0;
                        Match.FacePairMatch(faceMatchEngine, ref fm, ref faceModel, out score);
                        if (score > 0.5)
                        {
                            item.OrderId  = DateTime.Now.Ticks;
                            faceResult.ID = item.FaceID;
                            isdetected    = true;
                            break;
                        }
                    }
                    this.IsDetected = isdetected;
                    #endregion
                }
            }
            finally
            {
                bitmap.UnlockBits(bmpData);
                if (registerClicked)
                {
                    this.OnCaputurePicture(new CapturePictureEventArgs(bitmap, this.faceResult.RealRetangle, this.registerFeatureData));
                    registerClicked = false;
                }
                else
                {
                    bitmap.Dispose();
                }
            }
        }