/// <summary> /// 人脸识别特征码提取 /// </summary> /// <param name="image"></param> /// <param name="rect"></param> /// <param name="orient"></param> private byte[] RecognizeFace(Bitmap image, MRECT rect, int orient) { IntPtr offInputPtr = ArcFaceDetection.MakeImageInput_ASVLOFFSCREEN(image); AFR_FSDK_FACEINPUT faceInput = new AFR_FSDK_FACEINPUT(); faceInput.lOrient = orient; faceInput.rcFace = rect; //入参 IntPtr faceInputPtr = Marshal.AllocHGlobal(Marshal.SizeOf(faceInput)); Marshal.StructureToPtr(faceInput, faceInputPtr, false); AFR_FSDK_FACEMODEL faceModel = new AFR_FSDK_FACEMODEL(); IntPtr faceModelPtr = Marshal.AllocHGlobal(Marshal.SizeOf(faceModel)); int ret = AFRFunction.AFR_FSDK_ExtractFRFeature(this.faceRecognition.rEngine, offInputPtr, faceInputPtr, faceModelPtr); if (ret == 0) { faceModel = (AFR_FSDK_FACEMODEL)Marshal.PtrToStructure(faceModelPtr, typeof(AFR_FSDK_FACEMODEL)); Marshal.FreeHGlobal(faceModelPtr); byte[] featureContent = new byte[faceModel.lFeatureSize]; Marshal.Copy(faceModel.pbFeature, featureContent, 0, faceModel.lFeatureSize); return(featureContent); } return(null); }
/// <summary> /// 检测特征是否已存在 /// </summary> /// <param name="featureContent"></param> /// <returns></returns> private Tuple <bool, string> IsMatchFeature(byte[] featureContent) { if (this.faceFeatures == null) { this.LoadFaceFeatures(); } //待比较的特征数据 IntPtr f2Ptr = Marshal.AllocHGlobal(featureContent.Length); Marshal.Copy(featureContent, 0, f2Ptr, featureContent.Length); AFR_FSDK_FACEMODEL model2 = new AFR_FSDK_FACEMODEL(); model2.lFeatureSize = featureContent.Length; model2.pbFeature = f2Ptr; IntPtr model2Ptr = Marshal.AllocHGlobal(Marshal.SizeOf(model2)); Marshal.StructureToPtr(model2, model2Ptr, false); foreach (var feature in this.faceFeatures) { //已有的特征数据 IntPtr f1Ptr = Marshal.AllocHGlobal(feature.Feature.Length); Marshal.Copy(feature.Feature, 0, f1Ptr, feature.Feature.Length); AFR_FSDK_FACEMODEL model1 = new AFR_FSDK_FACEMODEL(); model1.lFeatureSize = feature.Feature.Length; model1.pbFeature = f1Ptr; IntPtr model1Ptr = Marshal.AllocHGlobal(Marshal.SizeOf(model1)); Marshal.StructureToPtr(model1, model1Ptr, false); float score = 0; int ret = AFRFunction.AFR_FSDK_FacePairMatching(faceRecognition.rEngine, model1Ptr, model2Ptr, ref score); Console.WriteLine("score:{0}", score); //相似度因子:0.75 if (ret == 0 && score >= 0.75f) { return(new Tuple <bool, string>(true, feature.FileName)); } } return(new Tuple <bool, string>(false, string.Empty)); }