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)); } }
public static float compareFaceSimilarity(IntPtr hFDEngine, IntPtr hFREngine, ASVLOFFSCREEN inputImgA, ASVLOFFSCREEN inputImgB) { // Do Face Detect FaceInfo[] faceInfosA = doFaceDetection(hFDEngine, inputImgA); if (faceInfosA.Length < 1) { Console.WriteLine("no face in Image A "); return(0.0f); } FaceInfo[] faceInfosB = doFaceDetection(hFDEngine, inputImgB); if (faceInfosB.Length < 1) { Console.WriteLine("no face in Image B "); return(0.0f); } // Extract Face Feature AFR_FSDK_FACEMODEL faceFeatureA = extractFRFeature(hFREngine, inputImgA, faceInfosA[0]); if (faceFeatureA.pbFeature == IntPtr.Zero) { Console.WriteLine("extract face feature in Image A failed"); return(0.0f); } AFR_FSDK_FACEMODEL faceFeatureB = extractFRFeature(hFREngine, inputImgB, faceInfosB[0]); if (faceFeatureB.pbFeature == IntPtr.Zero) { Console.WriteLine("extract face feature in Image B failed"); faceFeatureA.freeUnmanaged(); return(0.0f); } // calc similarity between faceA and faceB float fSimilScore = 0.0f; IntPtr ret = AFR_FSDKLibrary.AFR_FSDK_FacePairMatching(hFREngine, ref (faceFeatureA), ref (faceFeatureB), ref (fSimilScore)); faceFeatureA.freeUnmanaged(); faceFeatureB.freeUnmanaged(); if (ret.ToInt64() != 0) { Console.WriteLine(String.Format("AFR_FSDK_FacePairMatching failed:ret 0x{0:x}", ret)); return(0.0f); } return(fSimilScore); }
public static void Main(string[] args) { Console.WriteLine("#####################################################"); //init Engine IntPtr pFDWorkMem = Marshal.AllocCoTaskMem(FD_WORKBUF_SIZE); IntPtr pFRWorkMem = Marshal.AllocCoTaskMem(FR_WORKBUF_SIZE); IntPtr hFDEngine = new IntPtr(0); IntPtr ret = AFD_FSDKLibrary.AFD_FSDK_InitialFaceEngine(APPID, FD_SDKKEY, pFDWorkMem, FD_WORKBUF_SIZE, ref (hFDEngine), _AFD_FSDK_OrientPriority.AFD_FSDK_OPF_0_HIGHER_EXT, 32, MAX_FACE_NUM); if (ret.ToInt64() != 0) { Marshal.FreeCoTaskMem(pFDWorkMem); Marshal.FreeCoTaskMem(pFRWorkMem); Console.WriteLine(String.Format("AFD_FSDK_InitialFaceEngine 0x{0:x}", ret)); Environment.Exit(0); } //print FDEngine version AFD_FSDK_Version versionFD = (AFD_FSDK_Version)Marshal.PtrToStructure(AFD_FSDKLibrary.AFD_FSDK_GetVersion(hFDEngine), typeof(AFD_FSDK_Version)); Console.WriteLine(String.Format("{0} {1} {2} {3}", versionFD.lCodebase, versionFD.lMajor, versionFD.lMinor, versionFD.lBuild)); Console.WriteLine(versionFD.Version); Console.WriteLine(versionFD.BuildDate); Console.WriteLine(versionFD.CopyRight); IntPtr hFREngine = new IntPtr(0); ret = AFR_FSDKLibrary.AFR_FSDK_InitialEngine(APPID, FR_SDKKEY, pFRWorkMem, FR_WORKBUF_SIZE, ref (hFREngine)); if (ret.ToInt64() != 0) { AFD_FSDKLibrary.AFD_FSDK_UninitialFaceEngine(hFDEngine); Marshal.FreeCoTaskMem(pFDWorkMem); Marshal.FreeCoTaskMem(pFRWorkMem); Console.WriteLine(String.Format("AFR_FSDK_InitialEngine 0x{0:x}", ret)); Environment.Exit(0); } // print FREngine version AFR_FSDK_Version versionFR = (AFR_FSDK_Version)Marshal.PtrToStructure(AFR_FSDKLibrary.AFR_FSDK_GetVersion(hFREngine), typeof(AFR_FSDK_Version)); Console.WriteLine(String.Format("{0} {1} {2} {3}", versionFR.lCodebase, versionFR.lMajor, versionFR.lMinor, versionFR.lBuild)); Console.WriteLine(versionFR.Version); Console.WriteLine(versionFR.BuildDate); Console.WriteLine(versionFR.CopyRight); //load Image Data ASVLOFFSCREEN inputImgA; ASVLOFFSCREEN inputImgB; if (bUseRAWFile) { String filePathA = "001_640x480_I420.YUV"; int yuv_widthA = 640; int yuv_heightA = 480; int yuv_formatA = ASVL_COLOR_FORMAT.ASVL_PAF_I420; String filePathB = "003_640x480_I420.YUV"; int yuv_widthB = 640; int yuv_heightB = 480; int yuv_formatB = ASVL_COLOR_FORMAT.ASVL_PAF_I420; inputImgA = loadRAWImage(filePathA, yuv_widthA, yuv_heightA, yuv_formatA); inputImgB = loadRAWImage(filePathB, yuv_widthB, yuv_heightB, yuv_formatB); } else { String filePathA = "001.jpg"; String filePathB = "1_9.jpg"; inputImgA = loadImage(filePathA); inputImgB = loadImage(filePathB); } Console.WriteLine(String.Format("similarity between faceA and faceB is {0}", compareFaceSimilarity(hFDEngine, hFREngine, inputImgA, inputImgB))); inputImgA.freeUnmanaged(); inputImgB.freeUnmanaged(); //release Engine AFD_FSDKLibrary.AFD_FSDK_UninitialFaceEngine(hFDEngine); AFR_FSDKLibrary.AFR_FSDK_UninitialEngine(hFREngine); Marshal.FreeCoTaskMem(pFDWorkMem); Marshal.FreeCoTaskMem(pFRWorkMem); Console.WriteLine("#####################################################"); }