/// <summary> /// RGB摄像头Paint事件,同步RGB人脸框,对比人脸框后进行IR活体检测 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void irVideoSource_Paint(object sender, PaintEventArgs e) { if (isDoubleShot && irVideoSource.IsRunning) { //如果双摄,且IR摄像头工作,获取IR摄像头图片 Bitmap irBitmap = irVideoSource.GetCurrentVideoFrame(); if (irBitmap == null) { return; } //得到Rect MRECT rect = new MRECT(); lock (rectLock) { rect = allRect; } float irOffsetX = irVideoSource.Width * 1f / irBitmap.Width; float irOffsetY = irVideoSource.Height * 1f / irBitmap.Height; float offsetX = irVideoSource.Width * 1f / rgbVideoSource.Width; float offsetY = irVideoSource.Height * 1f / rgbVideoSource.Height; //检测IR摄像头下最大人脸 Graphics g = e.Graphics; 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(Pens.Red, x, y, width, height); if (trackIRUnit.message != "" && x > 0 && y > 0) { //将上一帧检测结果显示到页面上 g.DrawString(trackIRUnit.message, font, trackIRUnit.message.Contains("活体") ? blueBrush : yellowBrush, x, y - 15); } //保证只检测一帧,防止页面卡顿以及出现其他内存被占用情况 if (isIRLock == false) { isIRLock = true; //异步处理提取特征值和比对,不然页面会比较卡 ThreadPool.QueueUserWorkItem(new WaitCallback(delegate { if (rect.left != 0 && rect.right != 0 && rect.top != 0 && rect.bottom != 0) { bool isLiveness = false; try { //得到当前摄像头下的图片 if (irBitmap != null) { //检测人脸,得到Rect框 ASF_MultiFaceInfo irMultiFaceInfo = FaceUtil.DetectFace(pVideoIRImageEngine, irBitmap); if (irMultiFaceInfo.faceNum <= 0) { return; } //得到最大人脸 ASF_SingleFaceInfo irMaxFace = FaceUtil.GetMaxFace(irMultiFaceInfo); //得到Rect MRECT irRect = irMaxFace.faceRect; //判断RGB图片检测的人脸框与IR摄像头检测的人脸框偏移量是否在误差允许范围内 if (isInAllowErrorRange(rect.left * offsetX / irOffsetX, irRect.left) && isInAllowErrorRange(rect.right * offsetX / irOffsetX, irRect.right) && isInAllowErrorRange(rect.top * offsetY / irOffsetY, irRect.top) && isInAllowErrorRange(rect.bottom * offsetY / irOffsetY, irRect.bottom)) { int retCode_Liveness = -1; //将图片进行灰度转换,然后获取图片数据 ImageInfo irImageInfo = ImageUtil.ReadBMP_IR(irBitmap); if (irImageInfo == null) { return; } //IR活体检测 ASF_LivenessInfo liveInfo = FaceUtil.LivenessInfo_IR(pVideoIRImageEngine, irImageInfo, irMultiFaceInfo, out retCode_Liveness); //判断检测结果 if (retCode_Liveness == 0 && liveInfo.num > 0) { int isLive = MemoryUtil.PtrToStructure <int>(liveInfo.isLive); isLiveness = (isLive == 1) ? true : false; } if (irImageInfo != null) { MemoryUtil.Free(irImageInfo.imgData); } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { trackIRUnit.message = string.Format("IR{0}", isLiveness ? "活体" : "假体"); if (irBitmap != null) { irBitmap.Dispose(); } isIRLock = false; } } else { trackIRUnit.message = string.Empty; } isIRLock = false; })); } } }