/// <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="image">要抽取的人脸特征图像数据</param> /// <param name="faceLocation">人脸位置</param> /// <param name="orient">人脸朝向</param> /// <exception cref="Exception">如果执行失败,则抛出异常,并给出失败结果</exception> /// <returns>如果执行成功,返回特征数据,如果为null,则表示faceLocation的位置不存在人脸</returns> public Feature ExtractFeature(ImageData image, FaceRect faceLocation, OrientCode orient) { var faceRes = new AFR_FSDK_FACEINPUT { rcFace = faceLocation, lOrient = (int)orient }; var result = (ErrorCode)RecognizeWrapper.AFR_FSDK_ExtractFRFeature(Engine, ref image, ref faceRes, out var faceModel); if (result == ErrorCode.Ok) { var data = new byte[faceModel.lFeatureSize]; Marshal.Copy(faceModel.pbFeature, data, 0, faceModel.lFeatureSize); var feature = new Feature { FeatureData = data, Rect = faceLocation }; return(feature); } //采用Tracking定位时,可能出现识别错误的位置,此时返回的错误为Unsupported,遇到这样的情况直接返回null if (result == ErrorCode.Unsupported) { return(null); } throw new FaceException(result); }
public static AFR_FSDK_FACEMODEL extractFRFeature(IntPtr hFREngine, ASVLOFFSCREEN inputImg, FaceInfo faceInfo) { AFR_FSDK_FACEINPUT faceinput = new AFR_FSDK_FACEINPUT(); faceinput.lOrient = faceInfo.orient; faceinput.rcFace.left = faceInfo.left; faceinput.rcFace.top = faceInfo.top; faceinput.rcFace.right = faceInfo.right; faceinput.rcFace.bottom = faceInfo.bottom; AFR_FSDK_FACEMODEL faceFeature = new AFR_FSDK_FACEMODEL(IntPtr.Zero, 0); IntPtr ret = AFR_FSDKLibrary.AFR_FSDK_ExtractFRFeature(hFREngine, ref (inputImg), ref (faceinput), ref (faceFeature)); if (ret.ToInt64() != 0) { Console.WriteLine(String.Format("AFR_FSDK_ExtractFRFeature ret 0x{0:x}", ret)); return(new AFR_FSDK_FACEMODEL(IntPtr.Zero, 0)); } try { return(faceFeature.deepCopy()); } catch (Exception e) { Console.WriteLine(e.ToString()); return(new AFR_FSDK_FACEMODEL(IntPtr.Zero, 0)); } }