private void FaceForm_Load(object sender, EventArgs e) { ptrMainForm = this.Handle; engineContext = new EngineContext(); if (engineContext.IrCameraIdx >= 0) { this.chbLiveness.Checked = false; this.chbLiveness.Visible = false; } if (ActiveEngine()) { registerForm = new RegisterForm(); registerForm.Visible = false; registerForm.ptrParentForm = this.Handle; registerForm.engineContext = this.engineContext; ptrRegisterForm = registerForm.Handle; FaceDataLoad(); for (var i = 0; i < engineContext.FilterInfoCollection.Count; i++) { WinMessageHelper.SendMessage(this.Handle, UserMessage.WM_MESSAGE, IntPtr.Zero, engineContext.FilterInfoCollection[i].Name); } if (engineContext.IrCameraIdx < 0) { WinMessageHelper.SendMessage(this.Handle, UserMessage.WM_MESSAGE, IntPtr.Zero, "未检测到红外摄像头,请启用RGB活体检测。强烈建议使用双目摄像头!!!"); } } }
private void rdbThresholdHigh_CheckedChanged(object sender, EventArgs e) { if (this.rdbThresholdHigh.Checked) { engineContext.Threshold = 0.85f; WinMessageHelper.SendMessage(this.Handle, UserMessage.WM_MESSAGE, IntPtr.Zero, "安全等级置为'高'级"); } }
private void PerformDelete(int userId, int rowIndex) { Data.Domain.FaceFeature faceModel = iFaceDataRepository.GetById(userId); FaceFormLock(); var stuFaceModel = faceModel.CreateStruct(); IntPtr ptrFaceModel = MemoryHelper.Malloc(MemoryHelper.SizeOf <MemberData>()); MemoryHelper.StructureToPtr <MemberData>(stuFaceModel, ptrFaceModel); WinMessageHelper.SendMessage(this.Handle, UserMessage.WM_FACE_DATA_DELETE, (IntPtr)rowIndex, ptrFaceModel); }
private void PerformUpdate(int userId) { Data.Domain.FaceFeature faceModel = iFaceDataRepository.GetById(userId); FaceFormLock(); var stuFaceModel = faceModel.CreateStruct(); IntPtr ptrFaceModel = MemoryHelper.Malloc(MemoryHelper.SizeOf <MemberData>()); MemoryHelper.StructureToPtr <MemberData>(stuFaceModel, ptrFaceModel); string avatarPath = iFaceDataRepository.GetFaceImageFullPath(faceModel.Id, faceModel.FaceImagePath); IntPtr ptrAvatarPath = MemoryHelper.StringToIntPtr(avatarPath); WinMessageHelper.SendMessage(ptrRegisterForm, UserMessage.WM_FACE_DATA_UPDATE, ptrAvatarPath, ptrFaceModel); }
private bool ActiveEngine() { var ret = false; try { var retCode = engineContext.ActivateEngine(); if (retCode == Core.MError.MOK || retCode == Core.MError.MERR_ASF_ALREADY_ACTIVATED) { ret = true; } else { WinMessageHelper.SendMessage(this.Handle, UserMessage.WM_MESSAGE, IntPtr.Zero, retCode.GetFieldDescription()); } } catch (Exception ex) { WinMessageHelper.SendMessage(this.Handle, UserMessage.WM_MESSAGE, IntPtr.Zero, ex.Message); } return(ret); }
/// <summary> /// 图像显示到窗体上,得到每一帧图像,并进行处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void videoSource_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; ASF_MultiFaceInfo multiFaceInfoIr = default(ASF_MultiFaceInfo); //检测人脸,得到Rect框 ASF_MultiFaceInfo multiFaceInfo = engineContext.DetectFace(bitmap); if (multiFaceInfo.Equals(default(ASF_MultiFaceInfo))) { return; } //得到最大人脸 ASF_SingleFaceInfo maxFace = FaceProcessHelper.GetMaxFace(multiFaceInfo); //得到Rect MRECT 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; //根据Rect进行画框 g.DrawRectangle(pen, x, y, width, height); if (trackUnit.message != "" && x > 0 && y > 0) { //将上一帧检测结果显示到页面上 g.DrawString(trackUnit.message, font, brush, x, y + 5); } //保证只检测一帧,防止页面卡顿以及出现其他内存被占用情况 if (isLock == false) { //g.DrawRectangle(pen, x, y, width, height); //if (trackUnit.message != "" && x > 0 && y > 0) //{ // //将上一帧检测结果显示到页面上 // g.DrawString(trackUnit.message, font, brush, x, y + 5); //} isLock = true; //异步处理提取特征值和比对,不然页面会比较卡 ThreadPool.QueueUserWorkItem(new WaitCallback(delegate { if (rect.left != 0 && rect.right != 0 && rect.top != 0 && rect.bottom != 0) { try { var liveness = 1; var livenessIR = -1; if (this.chbLiveness.Checked) { liveness = -1; var livenessInfo = engineContext.GetLivenessScore(bitmap, maxFace); try { liveness = MemoryHelper.PtrToStructure <int>(livenessInfo.isLive); } catch (Exception ex) { liveness = -1; this.Invoke(new Action(delegate { //WinMessageHelper.SendMessage(this.Handle, UserMessage.WM_MESSAGE, IntPtr.Zero, $"liveness => {ex.Message}"); Afw.Core.Helper.SimplifiedLogHelper.WriteIntoSystemLog(nameof(FaceForm), $"videoSource_Paint livenessInfo exception=> {ex.ToString()}"); })); } } if (currentIrImage != null) { //检测人脸,得到Rect框 multiFaceInfoIr = engineContext.DetectFaceIR(currentIrImage); if (multiFaceInfoIr.faceNum > 0) { //得到最大人脸 ASF_SingleFaceInfo maxFaceIR = FaceProcessHelper.GetMaxFace(multiFaceInfoIr); var livenessInfoIR = engineContext.GetLivenessScoreIR(currentIrImage, maxFace); try { livenessIR = MemoryHelper.PtrToStructure <int>(livenessInfoIR.isLive); } catch (Exception ex) { livenessIR = -1; this.Invoke(new Action(delegate { //WinMessageHelper.SendMessage(this.Handle, UserMessage.WM_MESSAGE, IntPtr.Zero, $"liveness IR => {ex.Message}"); Afw.Core.Helper.SimplifiedLogHelper.WriteIntoSystemLog(nameof(FaceForm), $"videoSource_Paint livenessIRInfo exception=> {ex.ToString()}"); })); } } else { livenessIR = -1; } } else { livenessIR = engineContext.IrCameraIdx >= 0 ? -1 : 1; } if (liveness == 1 && livenessIR == 1) { //提取人脸特征 IntPtr feature = engineContext.ExtractFeature(bitmap, maxFace); float similarity = 0f; if (feature == IntPtr.Zero) { return; } //得到比对结果 int result = CompareFeature(feature, out similarity); if (result > -1) { //将比对结果放到显示消息中,用于最新显示 trackUnit.message = $" 员工Id{result} 相似度{similarity}"; this.Invoke(new Action(delegate { var member = iFaceDataRepository.GetById(result); WinMessageHelper.SendMessage(this.Handle, UserMessage.WM_MESSAGE, IntPtr.Zero, $"人脸比对成功,【姓名:{member.Name},雇员号:{member.EmployeeNo}】"); if (this.chbAutoCloseVideo.Checked) { ButtonEnableForImage(); btnStartVideo.Text = "开启摄像头"; engineContext.StopVideoCapture(); this.rdbThresholdHigh.Enabled = true; this.rdbThresholdLow.Enabled = true; this.rdbThresholdMid.Enabled = true; this.dgvFace.Enabled = true; MessageBox.Show($"人脸比对成功,【姓名:{member.Name},雇员号:{member.EmployeeNo}】"); } })); } else { //重置显示消息 trackUnit.message = ""; } MemoryHelper.Free(feature); } else { trackUnit.message = ""; } } catch (Exception ex) { this.Invoke(new Action(delegate { WinMessageHelper.SendMessage(this.Handle, UserMessage.WM_MESSAGE, IntPtr.Zero, ex.Message); })); } finally { isLock = false; } } isLock = false; if (currentIrImage != null) { currentIrImage = null; } })); } } GC.Collect(); }