public byte[] GetFaceData(Bitmap bitmap, Face_FD face_FD) { bool isFace = face_FD.CheckFace(bitmap, out AFD_FSDK_FACERES faceRes, out IntPtr offInputPtr, out IntPtr imageDataPtr); if (!isFace) { Error.Log(ErrorType.inputError); return(null); } AFR_FSDK_FaceInput faceinput = new AFR_FSDK_FaceInput(); faceinput.lOrient = (int)Marshal.PtrToStructure(faceRes.lfaceOrient, typeof(int)); MRECT rect = (MRECT)Marshal.PtrToStructure(faceRes.rcFace, typeof(MRECT)); 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 = FaceAPI.AFR_FSDK_ExtractFRFeature(detectEngine, offInputPtr, faceInputPtr, faceModelPtr); if (ret != 0) //返回值为0代表获取成功 { Log.AddLog("获取不到人脸信息。"); return(null); } faceModel = (AFR_FSDK_FaceModel)Marshal.PtrToStructure(faceModelPtr, typeof(AFR_FSDK_FaceModel)); Marshal.FreeHGlobal(faceModelPtr); byte[] byteData = new byte[faceModel.lFeatureSize]; Marshal.Copy(faceModel.pbFeature, byteData, 0, faceModel.lFeatureSize); Marshal.FreeHGlobal(faceInputPtr); return(byteData); }
public float CompareFace(byte[] data, byte[] beData) //返回相似系数 { IntPtr dataPtr = Marshal.AllocHGlobal(data.Length); Marshal.Copy(data, 0, dataPtr, data.Length); AFR_FSDK_FaceModel faceModel = new AFR_FSDK_FaceModel { lFeatureSize = data.Length, pbFeature = dataPtr }; IntPtr beDataPtr = Marshal.AllocHGlobal(beData.Length); Marshal.Copy(beData, 0, beDataPtr, beData.Length); AFR_FSDK_FaceModel beFaceModel = new AFR_FSDK_FaceModel { lFeatureSize = beData.Length, pbFeature = beDataPtr }; IntPtr firstPtr = Marshal.AllocHGlobal(Marshal.SizeOf(faceModel)); Marshal.StructureToPtr(faceModel, firstPtr, false); IntPtr secondPtr = Marshal.AllocHGlobal(Marshal.SizeOf(beFaceModel)); Marshal.StructureToPtr(beFaceModel, secondPtr, false); float result = 0; //大约0.55,是同个人 int ret = FaceAPI.AFR_FSDK_FacePairMatching(detectEngine, firstPtr, secondPtr, ref result); Marshal.FreeHGlobal(dataPtr); Marshal.FreeHGlobal(beDataPtr); Marshal.FreeHGlobal(firstPtr); Marshal.FreeHGlobal(secondPtr); return(result); }