//获取相似度 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 void Form2_Load(object sender, EventArgs e) { try { #region 初始化人脸跟踪引擎 int detectSize = 40 * 1024 * 1024; IntPtr pMem = Marshal.AllocHGlobal(detectSize); //1-1 //string appId = "4tnYSJ68e8wztSo4Cf7WvbyMZduHwpqtThAEM3obMWbE"; //1-1 // string sdkKey = "Cgbaq34izc8PA2Px26x8qqWLFNmGNL4oukYspKsbMz3z"; //1-n string appId = "34D48RFbU22vTdGVYXncnwTkk3EN6bcW2RYfJc9i9EKE"; //1-n string sdkKey = "9tjAT5hQJcSvzzDaEPqC7rmgqA9FkJFSq7yG2tzSLLzC"; //人脸跟踪引擎初始化 int retCode = AmFaceVerify.AFT_FSDK_InitialFaceEngine(appId, sdkKey, pMem, detectSize, ref traceEngine, 5, 16, maxFaceCount); Console.WriteLine(" Init Result:" + retCode); //获取人脸跟踪引擎版本 IntPtr versionPtr = AmFaceVerify.AFT_FSDK_GetVersion(traceEngine); AFT_FSDK_Version version = (AFT_FSDK_Version)Marshal.PtrToStructure(versionPtr, typeof(AFT_FSDK_Version)); Console.WriteLine("lCodebase:{0} lMajor:{1} lMinor:{2} lBuild:{3} Version:{4} BuildDate:{5} CopyRight:{6}", version.lCodebase, version.lMajor, version.lMinor, version.lBuild, Marshal.PtrToStringAnsi(version.Version), Marshal.PtrToStringAnsi(version.BuildDate), Marshal.PtrToStringAnsi(version.CopyRight)); #endregion this.capture = new Capture(); displayImage = new Thread(new ThreadStart(this.DisplayAddProcess)); displayImage.Start(); displayImage.IsBackground = true; } catch (Exception ex) { Console.WriteLine(ex.Message); LogHelper.WriteErrorLog("Form2_Load", ex.Message + "\n" + ex.StackTrace); } }
//初始化 private void Form1_Load(object sender, EventArgs e) { #region 初始化人脸检测引擎 int detectSize = 40 * 1024 * 1024; IntPtr pMem = Marshal.AllocHGlobal(detectSize); //1-1 //string appId = "4tnYSJ68e8wztSo4Cf7WvbyMZduHwpqtThAEM3obMWbE"; //1-1 //string sdkKey = "Cgbaq34izc8PA2Px26x8qqWTQn2P5vxijaWKdUrdCwYT"; //1-n string appId = "4tnYSJ68e8wztSo4Cf7Wvbz6Y3UFa7sYbRpM78FrKa1b"; //1-n string sdkKey = "ACWCx3meGjZyWCYCUE3gG4NmrhG2q8MzjcZKQHfSL935"; //人脸检测引擎初始化 int retCode = AmFaceVerify.AFD_FSDK_InitialFaceEngine(appId, sdkKey, pMem, detectSize, ref detectEngine, 5, 50, 1); //获取人脸检测引擎版本 IntPtr versionPtr = AmFaceVerify.AFD_FSDK_GetVersion(detectEngine); AFD_FSDK_Version version = (AFD_FSDK_Version)Marshal.PtrToStructure(versionPtr, typeof(AFD_FSDK_Version)); Console.WriteLine("lCodebase:{0} lMajor:{1} lMinor:{2} lBuild:{3} Version:{4} BuildDate:{5} CopyRight:{6}", version.lCodebase, version.lMajor, version.lMinor, version.lBuild, Marshal.PtrToStringAnsi(version.Version), Marshal.PtrToStringAnsi(version.BuildDate), Marshal.PtrToStringAnsi(version.CopyRight)); //Marshal.FreeHGlobal(versionPtr); #endregion #region 初始化人脸识别引擎 int recognizeSize = 40 * 1024 * 1024; IntPtr pMemDetect = Marshal.AllocHGlobal(recognizeSize); //1-1 //string appIdDetect = "4tnYSJ68e8wztSo4Cf7WvbyMZduHwpqtThAEM3obMWbE"; //1-1 //string sdkKeyDetect = "Cgbaq34izc8PA2Px26x8qqWaaBHbPD7wWMcTU6xe8VRo"; //1-n string appIdDetect = "4tnYSJ68e8wztSo4Cf7Wvbz6Y3UFa7sYbRpM78FrKa1b"; //1-n string sdkKeyDetect = "ACWCx3meGjZyWCYCUE3gG4PGWJJfTYJKx22ztQFVkAjG"; //人脸识别引擎初始化 retCode = AmFaceVerify.AFR_FSDK_InitialEngine(appIdDetect, sdkKeyDetect, pMemDetect, recognizeSize, ref regcognizeEngine); //获取人脸识别引擎版本 IntPtr versionPtrDetect = AmFaceVerify.AFR_FSDK_GetVersion(regcognizeEngine); AFR_FSDK_Version versionDetect = (AFR_FSDK_Version)Marshal.PtrToStructure(versionPtrDetect, typeof(AFR_FSDK_Version)); Console.WriteLine("lCodebase:{0} lMajor:{1} lMinor:{2} lBuild:{3} lFeatureLevel:{4} Version:{5} BuildDate:{6} CopyRight:{7}", versionDetect.lCodebase, versionDetect.lMajor, versionDetect.lMinor, versionDetect.lBuild, versionDetect.lFeatureLevel, Marshal.PtrToStringAnsi(versionDetect.Version), Marshal.PtrToStringAnsi(versionDetect.BuildDate), Marshal.PtrToStringAnsi(versionDetect.CopyRight)); #endregion }
//检测人脸、提取特征 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); }
//加载摄像头图像 public void DisplayAddProcess() { try { while (!this._stop) { Image <Bgr, byte> frame = this.capture.QueryFrame(); if (frame != null) { using (Bitmap im = frame.Clone().ToBitmap()) { im.RotateFlip(RotateFlipType.RotateNoneFlipX); Bitmap tempBitmap = im.Clone(new RectangleF(0, 0, im.Width, im.Height), PixelFormat.Format24bppRgb); //this.setPictureBoxControlImage(this.pictureBox1, new Bitmap(tempBitmap)); byte[] imageData = getBGR(tempBitmap, ref width, ref height, ref pitch); 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; AFT_FSDK_FACERES faceRes = new AFT_FSDK_FACERES(); IntPtr offInputPtr = Marshal.AllocHGlobal(Marshal.SizeOf(offInput)); Marshal.StructureToPtr(offInput, offInputPtr, false); IntPtr faceResPtr = Marshal.AllocHGlobal(Marshal.SizeOf(faceRes)); //人脸检测 int detectResult = AmFaceVerify.AFT_FSDK_FaceFeatureDetect(traceEngine, offInputPtr, ref faceResPtr); if (detectResult != 0) { this.setPictureBoxControlImage(this.pictureBox1, new Bitmap(tempBitmap)); continue; } //Console.WriteLine(" detect Result:" + detectResult); object obj = Marshal.PtrToStructure(faceResPtr, typeof(AFT_FSDK_FACERES)); faceRes = (AFT_FSDK_FACERES)obj; //Console.WriteLine(" Face Count:{0}", faceRes.nFace); if (faceRes.nFace > maxFaceCount) { this.setPictureBoxControlImage(this.pictureBox1, new Bitmap(tempBitmap)); continue; } Rectangle[] faces = new Rectangle[faceRes.nFace]; for (int i = 0; i < faceRes.nFace; i++) { MRECT rect = (MRECT)Marshal.PtrToStructure(faceRes.rcFace + Marshal.SizeOf(typeof(MRECT)) * i, typeof(MRECT)); //Console.WriteLine(" left:{0} top:{1} right:{2} bottom:{3}", rect.left, rect.top, rect.right, rect.bottom); faces[i] = new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top); } if (faceRes.nFace > 0) { this.setPictureBoxControlImage(this.pictureBox1, drawFaces(tempBitmap, faces)); // this.setPictureBoxControlImage(this.pictureBox2, drawFaces(tempBitmap, faces)); #region xhz if (IsPhoto) { Task task1 = Task.Run(() => { pictureBox2.Image = drawFaces((Bitmap)tempBitmap.Clone(), (Rectangle[])faces.Clone()); UploadPicture(); }); // task1.Wait(); // Bitmap jpg=new Bitmap((Image)pictureBox2.Image.Clone()); //pictureBox2.Image = CutFace(tempBitmap, faces[0].X,faces[0].Y,faces[0].Width,faces[0].Height); IsPhoto = false; } // Image image = CutFace(bitmap, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top); #endregion } else { this.setPictureBoxControlImage(this.pictureBox1, new Bitmap(tempBitmap)); } Marshal.FreeHGlobal(offInputPtr); imageData = null; Marshal.FreeHGlobal(imageDataPtr); offInput = new ASVLOFFSCREEN(); faceRes = new AFT_FSDK_FACERES(); } } } } catch (Exception ex) { Console.WriteLine(ex.Message); LogHelper.WriteErrorLog("DisplayAddProcess", ex.Message + "\n" + ex.StackTrace); } }