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); }
//检查是否存在人脸,imageDataPtr必须在offInputPtr用完后释放掉 public bool CheckFace(Bitmap bitmap, out AFD_FSDK_FACERES faceRes, out IntPtr offInputPtr, out IntPtr imageDataPtr) { byte[] imageData = BitmapToBmp(bitmap, out int width, out int height, out int pitch); imageDataPtr = Marshal.AllocHGlobal(imageData.Length); Marshal.Copy(imageData, 0, imageDataPtr, imageData.Length); ASVLOFFSCREEN offInput = new ASVLOFFSCREEN(); offInput.u32PixelArrayFormat = 513; offInput.ppu8Plane = new IntPtr[4]; offInput.ppu8Plane[0] = imageDataPtr; offInput.i32Width = width; offInput.i32Height = height; offInput.pi32Pitch = new int[4]; offInput.pi32Pitch[0] = pitch; offInputPtr = Marshal.AllocHGlobal(Marshal.SizeOf(offInput)); Marshal.StructureToPtr(offInput, offInputPtr, false); faceRes = new AFD_FSDK_FACERES(); IntPtr faceResPtr = Marshal.AllocHGlobal(Marshal.SizeOf(faceRes)); int detectResult = FaceAPI.AFD_FSDK_StillImageFaceDetection(detectEngine, offInputPtr, ref faceResPtr); faceRes = (AFD_FSDK_FACERES)Marshal.PtrToStructure(faceResPtr, typeof(AFD_FSDK_FACERES)); MRECT rect = (MRECT)Marshal.PtrToStructure(faceRes.rcFace, typeof(MRECT)); bool ret = faceRes.nFace > 0; imageData = null; //Marshal.FreeHGlobal(imageDataPtr); //这个指针内存泄漏了 //Marshal.FreeHGlobal(faceResPtr); //GC.Collect(); return(ret); }