/// <summary> /// 获取多个人脸检测结果中面积最大的人脸 /// </summary> /// <param name="multiFaceInfo">人脸检测结果</param> /// <returns>面积最大的人脸信息</returns> public static ASF_SingleFaceInfo GetMaxFace(AFIC_FSDK_FACERES multiFaceInfo) { ASF_SingleFaceInfo singleFaceInfo = new ASF_SingleFaceInfo(); singleFaceInfo.faceRect = new RECT(); singleFaceInfo.faceOrient = 1; int maxArea = 0; int index = -1; for (int i = 0; i < multiFaceInfo.nFace; i++) { RECT rect = multiFaceInfo.rcFace; int area = (rect.right - rect.left) * (rect.bottom - rect.top); if (maxArea <= area) { maxArea = area; index = i; } } if (index != -1) { singleFaceInfo.faceRect = multiFaceInfo.rcFace; } return(singleFaceInfo); }
/// <summary> ///图像显示到窗体上,得到每一帧图像,并进行处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void videoSourcePlayer_Paint(object sender, PaintEventArgs e) { if (videoSource.IsRunning) { Bitmap bitmap = videoSource.GetCurrentVideoFrame(); if (bitmap == null) { return; } Graphics g = e.Graphics; float offsetX = videoSource.Width * 1f / bitmap.Width; float offsetY = videoSource.Height * 1f / bitmap.Height; AFIC_FSDK_FACERES faceInfo = new AFIC_FSDK_FACERES(); int result = IDCardUtil.FaceDataFeatureExtraction(pEngine, true, bitmap, ref faceInfo); if (result == 0 && faceInfo.nFace > 0) { string message = ""; if (isShow && isRead) { float pSimilarScore = 0; int pResult = 0; float threshold = 0.82f; float.TryParse(scoreText.Text, out threshold); result = IDCardUtil.FaceIdCardCompare(ref pSimilarScore, ref pResult, pEngine, threshold); if (result == 0) { if (threshold > pSimilarScore) { msgLabel.ForeColor = Color.Red; msgLabel.Text = "人证核验失败"; } else { msgLabel.ForeColor = Color.Green; msgLabel.Text = "人证核验成功"; } } message = "相似度:" + pSimilarScore; } else { msgLabel.ForeColor = Color.Red; msgLabel.Text = "请放置身份证"; } ASF_SingleFaceInfo maxFace = IDCardUtil.GetMaxFace(faceInfo); RECT rect = maxFace.faceRect; float x = rect.left * offsetX; float width = rect.right * offsetX - x; float y = rect.top * offsetY; float height = rect.bottom * offsetY - y; using (Pen pen = new Pen(Color.Red)) { g.DrawRectangle(pen, x, y, width, height); } } } }
private void CompareTest(string sfzImg, string paiZhaoImg) { Bitmap Bitmap = new Bitmap(paiZhaoImg); int res = IDCardUtil.IdCardDataFeatureExtraction(pEngine, Image.FromFile(sfzImg)); if (res == 0) { AFIC_FSDK_FACERES faceInfo = new AFIC_FSDK_FACERES(); int result2 = IDCardUtil.FaceDataFeatureExtraction(pEngine, false, Bitmap, ref faceInfo); if (result2 == 0 && faceInfo.nFace > 0) { float pSimilarScore = 0; int pResult = 0; float threshold = 0.82f; int result3 = IDCardUtil.FaceIdCardCompare(ref pSimilarScore, ref pResult, pEngine, threshold); if (result3 == 0) { Console.WriteLine("相似度:" + pSimilarScore); if (pSimilarScore >= GetFaceThreshold()) { timer.Close(); MessageBox.Show("活体,相似度" + pSimilarScore + ",人脸验证通过!"); App.Current.Dispatcher.Invoke((Action)(() => { this.Close(); CameraHelper.CloseDevice(); Console.WriteLine("关闭摄像头"); int retCode = ASIDCardFunctions.ArcSoft_FIC_UninitialEngine(pEngine); Console.WriteLine("UninitEngine Result:" + retCode); Window_Closed(); })); } else { MessageBox.Show("相似度" + pSimilarScore + ",人脸验证未通过!"); timer.Start(); } } else { MessageBox.Show("识别失败,请重试!"); timer.Start(); } } else { MessageBox.Show("未检测到人脸,请重试!"); timer.Start(); } } else { MessageBox.Show("1" + Convert.ToString(res)); } }
private void videoSourcePlayer1_Paint(object sender, PaintEventArgs e) { if (closeForm) { this.Close(); return; } if (videoSourcePlayer1.IsRunning) { Bitmap bitmap = videoSourcePlayer1.GetCurrentVideoFrame(); if (bitmap == null) { return; } Graphics g = e.Graphics; float offsetX = videoSourcePlayer1.Width * 1f / bitmap.Width; float offsetY = videoSourcePlayer1.Height * 1f / bitmap.Height; AFIC_FSDK_FACERES faceInfo = new AFIC_FSDK_FACERES(); int result = IDCardUtil.FaceDataFeatureExtraction(pEngine, true, bitmap, ref faceInfo); if (result == 0 && faceInfo.nFace > 0) { float pSimilarScore = 0; int pResult = 0; float threshold = 0.82f; result = IDCardUtil.FaceIdCardCompare(ref pSimilarScore, ref pResult, pEngine, threshold); if (result == 0) { if (threshold <= pSimilarScore) { verifyCounts++; this.textBox1.Text = "成功核对; " + verifyCounts.ToString(); //this.textBox1.Text = deviceVideo.VideoResolution.FrameSize.Width.ToString() + " " + deviceVideo.VideoResolution.FrameSize.Height.ToString(); //定时关闭启动 if (!timer.Enabled && verifyCounts > 8) { formReturnValue = FACEMATCHED; timer.Enabled = true; } } } ASF_SingleFaceInfo maxFace = IDCardUtil.GetMaxFace(faceInfo); RECT rect = maxFace.faceRect; float x = rect.left * offsetX; float width = rect.right * offsetX - x; float y = rect.top * offsetY; float height = rect.bottom * offsetY - y; using (Pen pen = new Pen(Color.Red)) { g.DrawRectangle(pen, x, y, width, height); } } } }
static void start(Img imgInfo) { var image = GetImageFromBase64(imgInfo.srcImg.Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "") .Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", "")); // Image image = Image.FromFile("C:/xxx/1.jpg"); int result2 = IDCardUtil.IdCardDataFeatureExtraction(pEngine, image); if (result2 != 0) { Console.WriteLine("idCard失败"); allSockets.ToList().ForEach(s => s.Send("idCard失败")); } var bitmap = GetImageFromBase64(imgInfo.faceImg.Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "") .Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", "")); // Bitmap bitmap = new Bitmap(@"C:\xxx\xxx.bmp"); if (bitmap == null) { return; } float offsetX = image.Width * 1f / bitmap.Width; float offsetY = image.Height * 1f / bitmap.Height; AFIC_FSDK_FACERES faceInfo = new AFIC_FSDK_FACERES(); int result = IDCardUtil.FaceDataFeatureExtraction(pEngine, false, bitmap, ref faceInfo); if (result == 0 && faceInfo.nFace > 0) { float pSimilarScore = 0; int pResult = 0; float threshold = 0.82f; result = IDCardUtil.FaceIdCardCompare(ref pSimilarScore, ref pResult, pEngine, threshold); if (result == 0) { Console.WriteLine(pSimilarScore); allSockets.ToList().ForEach(s => s.Send("相似度:" + pSimilarScore.ToString())); if (threshold > pSimilarScore) { Console.WriteLine("失败"); } else { Console.WriteLine("成功"); } } } else { allSockets.ToList().ForEach(s => s.Send("不存在人脸")); Console.WriteLine("不存在人脸"); } }
/// <summary> /// 人脸特征提取 /// </summary> /// <param name="hFICEngine">FIC 引擎Handle</param> /// <param name="isVideo">人脸数据类型 1-视频 0-静态图片</param> /// <param name="bitmap">人脸图像原始数据</param> /// <returns>人脸检测结果</returns> public static int FaceDataFeatureExtraction(IntPtr hFICEngine, bool isVideo, Bitmap bitmap, ref AFIC_FSDK_FACERES faceRes) { lock (locks) { int result = -1; if (bitmap != null) { ASVLOFFSCREEN offInput = ImageUtil.ReadBmp(bitmap); IntPtr offInputPtr = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASVLOFFSCREEN>()); MemoryUtil.StructureToPtr(offInput, offInputPtr); IntPtr faceResPtr = MemoryUtil.Malloc(MemoryUtil.SizeOf <AFIC_FSDK_FACERES>()); result = ASIDCardFunctions.ArcSoft_FIC_FaceDataFeatureExtraction(hFICEngine, isVideo, offInputPtr, faceResPtr); faceRes = MemoryUtil.PtrToStructure <AFIC_FSDK_FACERES>(faceResPtr); MemoryUtil.Free(offInput.ppu8Plane[0]); MemoryUtil.Free(offInputPtr); MemoryUtil.Free(faceResPtr); } return(result); } }