//获取相似度 private void button3_Click(object sender, EventArgs e) { float similar = 0f; AFR_FSDK_FACEMODEL localFaceModels = new AFR_FSDK_FACEMODEL(); IntPtr firstFeaturePtr = Marshal.AllocHGlobal(firstFeature.Length); Marshal.Copy(firstFeature, 0, firstFeaturePtr, firstFeature.Length); localFaceModels.lFeatureSize = firstFeature.Length; localFaceModels.pbFeature = firstFeaturePtr; IntPtr secondFeaturePtr = Marshal.AllocHGlobal(secondFeature.Length); Marshal.Copy(secondFeature, 0, secondFeaturePtr, secondFeature.Length); AFR_FSDK_FACEMODEL localFaceModels2 = new AFR_FSDK_FACEMODEL(); localFaceModels2.lFeatureSize = secondFeature.Length; localFaceModels2.pbFeature = secondFeaturePtr; IntPtr firstPtr = Marshal.AllocHGlobal(Marshal.SizeOf(localFaceModels)); Marshal.StructureToPtr(localFaceModels, firstPtr, false); IntPtr secondPtr = Marshal.AllocHGlobal(Marshal.SizeOf(localFaceModels2)); Marshal.StructureToPtr(localFaceModels2, secondPtr, false); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); int result = AmFaceVerify.AFR_FSDK_FacePairMatching(regcognizeEngine, firstPtr, secondPtr, ref similar); stopwatch.Stop(); setControlText(this.label1, "相似度:" + similar.ToString() + " 耗时:" + stopwatch.ElapsedMilliseconds.ToString() + "ms"); //this.label1.Text = "相似度:" + similar.ToString() + " 耗时:" + stopwatch.ElapsedMilliseconds.ToString() + "ms"; localFaceModels = new AFR_FSDK_FACEMODEL(); Marshal.FreeHGlobal(firstFeaturePtr); Marshal.FreeHGlobal(secondFeaturePtr); Marshal.FreeHGlobal(firstPtr); Marshal.FreeHGlobal(secondPtr); localFaceModels2 = new AFR_FSDK_FACEMODEL(); }
//检测人脸、提取特征 private byte[] detectAndExtractFeature(Image imageParam, int firstSecondFlg) { byte[] feature = null; try { Console.WriteLine(); Console.WriteLine("############### Face Detect Start #########################"); int width = 0; int height = 0; int pitch = 0; Bitmap bitmap = new Bitmap(imageParam); byte[] imageData = getBGR(bitmap, ref width, ref height, ref pitch); //GCHandle hObject = GCHandle.Alloc(imageData, GCHandleType.Pinned); //IntPtr imageDataPtr = hObject.AddrOfPinnedObject(); IntPtr 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; AFD_FSDK_FACERES faceRes = new AFD_FSDK_FACERES(); IntPtr offInputPtr = Marshal.AllocHGlobal(Marshal.SizeOf(offInput)); Marshal.StructureToPtr(offInput, offInputPtr, false); IntPtr faceResPtr = Marshal.AllocHGlobal(Marshal.SizeOf(faceRes)); //Marshal.StructureToPtr(faceRes, faceResPtr, false); Console.WriteLine("StartTime:{0}", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.ffff")); Stopwatch watchTime = new Stopwatch(); watchTime.Start(); //人脸检测 int detectResult = AmFaceVerify.AFD_FSDK_StillImageFaceDetection(detectEngine, offInputPtr, ref faceResPtr); watchTime.Stop(); if (firstSecondFlg == 1) { setControlText(this.label5, String.Format("检测耗时:{0}ms", watchTime.ElapsedMilliseconds)); //this.label5.Text = String.Format("检测耗时:{0}ms", watchTime.ElapsedMilliseconds); } else if (firstSecondFlg == 2) { setControlText(this.label2, String.Format("检测耗时:{0}ms", watchTime.ElapsedMilliseconds)); //this.label2.Text = String.Format("检测耗时:{0}ms", watchTime.ElapsedMilliseconds); } object obj = Marshal.PtrToStructure(faceResPtr, typeof(AFD_FSDK_FACERES)); faceRes = (AFD_FSDK_FACERES)obj; Console.WriteLine(" Face Count:{0}", faceRes.nFace); for (int i = 0; i < faceRes.nFace; i++) { MRECT rect = (MRECT)Marshal.PtrToStructure(faceRes.rcFace + Marshal.SizeOf(typeof(MRECT)) * i, typeof(MRECT)); int orient = (int)Marshal.PtrToStructure(faceRes.lfaceOrient + Marshal.SizeOf(typeof(int)) * i, typeof(int)); if (i == 0) { Image image = CutFace(bitmap, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top); if (firstSecondFlg == 1) { this.pictureBox3.Image = image; } else if (firstSecondFlg == 2) { this.pictureBox4.Image = image; } } Console.WriteLine(" left:{0} top:{1} right:{2} bottom:{3} orient:{4}", rect.left, rect.top, rect.right, rect.bottom, orient); } Console.WriteLine(" EndTime:{0}", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.ffff")); Console.WriteLine("############### Face Detect End #########################"); if (faceRes.nFace > 0) { Console.WriteLine(); Console.WriteLine("############### Face Recognition Start #########################"); AFR_FSDK_FACEINPUT faceResult = new AFR_FSDK_FACEINPUT(); int orient = (int)Marshal.PtrToStructure(faceRes.lfaceOrient, typeof(int)); faceResult.lfaceOrient = orient; faceResult.rcFace = new MRECT(); MRECT rect = (MRECT)Marshal.PtrToStructure(faceRes.rcFace, typeof(MRECT)); faceResult.rcFace = rect; IntPtr faceResultPtr = Marshal.AllocHGlobal(Marshal.SizeOf(faceResult)); Marshal.StructureToPtr(faceResult, faceResultPtr, false); AFR_FSDK_FACEMODEL localFaceModels = new AFR_FSDK_FACEMODEL(); IntPtr localFaceModelsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(localFaceModels)); //Marshal.StructureToPtr(localFaceModels, localFaceModelsPtr, false); watchTime.Start(); int extractResult = AmFaceVerify.AFR_FSDK_ExtractFRFeature(regcognizeEngine, offInputPtr, faceResultPtr, localFaceModelsPtr); Marshal.FreeHGlobal(faceResultPtr); Marshal.FreeHGlobal(offInputPtr); watchTime.Stop(); if (firstSecondFlg == 1) { setControlText(this.label3, String.Format("抽取特征耗时:{0}ms", watchTime.ElapsedMilliseconds)); //this.label3.Text = String.Format("抽取特征耗时:{0}ms", watchTime.ElapsedMilliseconds); } else if (firstSecondFlg == 2) { setControlText(this.label4, String.Format("抽取特征耗时:{0}ms", watchTime.ElapsedMilliseconds)); //this.label4.Text = String.Format("抽取特征耗时:{0}ms", watchTime.ElapsedMilliseconds); } object objFeature = Marshal.PtrToStructure(localFaceModelsPtr, typeof(AFR_FSDK_FACEMODEL)); Marshal.FreeHGlobal(localFaceModelsPtr); localFaceModels = (AFR_FSDK_FACEMODEL)objFeature; feature = new byte[localFaceModels.lFeatureSize]; Marshal.Copy(localFaceModels.pbFeature, feature, 0, localFaceModels.lFeatureSize); localFaceModels = new AFR_FSDK_FACEMODEL(); Console.WriteLine("############### Face Recognition End #########################"); } bitmap.Dispose(); imageData = null; Marshal.FreeHGlobal(imageDataPtr); offInput = new ASVLOFFSCREEN(); faceRes = new AFD_FSDK_FACERES(); //Marshal.FreeHGlobal(faceResPtr); } catch (Exception e) { LogHelper.WriteErrorLog("detect", e.Message + "\n" + e.StackTrace); } return(feature); }