コード例 #1
0
 public static extern int ExtractFeature(IntPtr engine, ref ImageData imageData, ref FaceFeatureInput faceFeatureInput, out FaceModel faceModel);
コード例 #2
0
        /// <summary>
        /// 人脸比对
        /// </summary>
        /// <param name="bitmap">输入图片</param>
        /// <param name="featureDataIndex">需要转换人脸特征的序号</param>
        public static void FaceMatch(Bitmap bitmap, int featureDataIndex = -1)
        {
            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;



            try
            {
                var ret = (ErrorCode)ArcWrapper.Detection(_DEnginer, ref imageData, out var pDetectResult);
                if (ret != ErrorCode.Ok)
                {
                    return;
                }

                var detectResult = Marshal.PtrToStructure <DetectResult>(pDetectResult);

                FaceResults.FaceNumber = detectResult.nFace;
                if (detectResult.nFace == 0)
                {
                    return;
                }


                for (int i = 0; i < detectResult.nFace; i++)
                {
                    IntPtr p        = new IntPtr(detectResult.rcFace.ToInt32() + i * Marshal.SizeOf <FaceRect>());
                    var    faceRect = Marshal.PtrToStructure <FaceRect>(p);
                    FaceResults[i].Rectangle = new Rectangle(faceRect.left, faceRect.top, faceRect.right - faceRect.left, faceRect.bottom - faceRect.top);

                    p = new IntPtr(detectResult.lfaceOrient.ToInt32() + i * Marshal.SizeOf <int>());
                    var faceOrient = Marshal.PtrToStructure <int>(p);

                    var faceFeatureInput = new FaceFeatureInput
                    {
                        rcFace  = faceRect,
                        lOrient = faceOrient
                    };
                    if ((ErrorCode)ArcWrapper.ExtractFeature(_REngine, ref imageData, ref faceFeatureInput, out var faceModel) != ErrorCode.Ok)
                    {
                        continue;
                    }


                    if (featureDataIndex == i)
                    {
                        if (FaceResults[i].FeatureData == null)
                        {
                            FaceResults[i].FeatureData = new byte[faceModel.lFeatureSize];
                        }
                        Marshal.Copy(faceModel.pbFeature, FaceResults[i].FeatureData, 0, faceModel.lFeatureSize);
                    }
                    bool matched = false;
                    foreach (var item in _FaceLib.Items.OrderByDescending(ii => ii.OrderId))
                    {
                        var fm = item.FaceModel;
                        ArcWrapper.Match(_REngine, ref fm, ref faceModel, out float score);
                        if (score > 0.5)
                        {
                            matched              = true;
                            item.OrderId         = DateTime.Now.Ticks;
                            FaceResults[i].ID    = item.ID;
                            FaceResults[i].Score = score;
                            break;
                        }
                    }
                    if (!matched)
                    {
                        FaceResults[i].ID    = "不认识";
                        FaceResults[i].Score = 0;
                    }
                }
            }
            finally
            {
                bitmap.UnlockBits(bmpData);
            }
        }