基于Emgu CV+百度人脸识别,实现视频动态 人脸抓取与识别

2023-06-28,,

总目录地址:AI 系列 总目录 

需要最新源码,或技术提问,请加QQ群:538327407

我的各种github 开源项目和代码:https://github.com/linbin524

背景

目前AI 处于风口浪尖,作为 公司的CTO,也作为自己的技术专研,开始了AI之旅,在朋友圈中也咨询 一些大牛对于AI 机器学习框架的看法,目前自己的研究方向主要开源的 AI 库,如:Emgu CV、TensorFlow、CNTK 等等,针对 四大平台 百度AI、阿里ET、腾讯AI、科大讯飞AI 做结合。

PS:笔者的主打语言是C#,目前项目主导系统都是基于Net 系列下开发而成的。主要负责公司软件系统架构设计, 鉴于朋友圈中各位技术大牛无私分享,也是鉴于自己再专研时候遇到不少坑,希望把相关研究心得发出,为大家在coding 中减少 麻烦。稍后会把自己开源框架在博客中建立系列教程,插件化模式 自动服务 (都在实际项目中使用)。

选择的理由:四大平台目前 AI 团队算是全世界最牛的一群人,而且资金背景雄厚(AI 很烧钱!),现在四大平台 的AI有一些已经相对成熟,依靠于人家的技术,做自己的应用,应该是中小企业 在物联网行业前进的一个方向吧。

四大平台AI入口

1、百度AI https://ai.baidu.com/customer(百度AI 产品:阿波罗无人驾驶开源平台 )

2、腾讯AI https://ai.qq.com/hr/youtu.shtml(腾讯AI 产品:腾讯觅影)

3、讯飞AI http://startups.xfyun.cn/(讯飞:语音)

4、阿里云ET  https://et.aliyun.com/index(阿里:城市大脑)

一、需求

1、目前我们需要通过摄像头 精准的捕获视频监控中人脸(要求精度高)

2、批量捕获人脸进行 人脸识别

3、在人脸矩形框中实时显示人员相关信息

二、技术难点

1、百度人脸识别 只是需要通过Image 图片通过开发接口发送,返回识别人物,所以前提还是要先做到人脸在视频中动态捕捉。

2、视频播放需要高效流畅播放,请求接口的效率是否影响视频监控友好展示。

三、技术选型

1、为了解决 视频中人脸动态捕捉,选用Emgu CV  是开源 框架 Open CV 的Net 版本,可以人脸精准抓取

emgu CV 官网:http://www.emgu.com/

对于Emgu CV 的详细信息,请百度。

2、百度人脸识别,接口完善,人脸识别精准度高,可以很好的做到人脸识别。

百度人脸识别接口文档:http://ai.baidu.com/docs#/Face-Csharp-SDK/top

四、技术准备

1、到emgu cv 的官网下载 最新的emgu CV 版本,因为版本间差异较大,所以在百度搜索时候,会发现很多文章代码是无法直接copy的。

2、安装emgu cv 的最版本

请仔细查看 solution 文件夹,里面有对应solution 文件,(在windowDeskTop文件夹里面 )选择Emgu.CV.sln打开,就可以看到Emgu.CV.Example(Emgu.CV 的Demo)。

3、我们需要将 FaceDetection(人脸精准查找Demo)和VideoSurveilance(视频动态抓捕实现矩形框)两个Demo 做结合

4、到百度开发者中心注册成为开发者,并且获取开发者。

5、下载百度AI 开发平台 对应的SDK(已经封装过,可以减少开发工作量),或者直接进行Api对接。

五、实现

先上实现效果,相关个人信息如头像、还有姓名和电话都被我处理过了。

1、准备好摄像头,连接没问题就开始敲代码了。

2、在百度人脸库建立自己的人脸素材,这个要求要精准,比较适合就是员工的工牌相片,就是一寸或者两寸的照片那种。

详细方法 百度提供SDK 已经有了,就不多作介绍了。其中 自定义的 Uid(用户id,用于唯一码)、group (人才组)、userInfo(用户信息,作为人脸识别返回信息显示)比较关键。

sdk 下载地址:http://ai.baidu.com/sdk,下载完成将如图中 dll 引用到自己的类库中。

以下方法是我简单改造过了。

我做了一个简单的人脸库添加 工具,可以进行简单人脸库操作

3、选择VideoSurveilance 做改造,上代码。

其中

void ProcessFrame(object sender, EventArgs e) 方法 是关键,里面就是具体操作人脸识别具体应用。

将 FaceDetection 中的相关内容copy 到VideoSurveilance 项目中,其中 haarcascade_frontalface_default.xml 用于 人脸检测,DetectFace.cs 是具体检测人脸方法。
 
 //----------------------------------------------------------------------------
// Copyright (C) 2004-2017 by EMGU Corporation. All rights reserved.
//---------------------------------------------------------------------------- using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms; using Emgu.CV;
using Emgu.CV.Cvb;
using Emgu.CV.UI;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.VideoSurveillance;
using FaceDetection;
using Emgu.CV.Cuda;
using AOP.Common;
using System.Drawing.Imaging;
using Baidu.Aip.API;
using System.Threading;
using BaiduAIAPI.Model; namespace VideoSurveilance
{
public partial class VideoSurveilance : Form
{ private static VideoCapture _cameraCapture; private static BackgroundSubtractor _fgDetector;
private static Emgu.CV.Cvb.CvBlobDetector _blobDetector;
private static Emgu.CV.Cvb.CvTracks _tracker; private static Queue<ImageModel> FacIdentifyQueue = new Queue<ImageModel>();
public Image faceImage;
Thread t1;
public VideoSurveilance()
{
InitializeComponent();
Run();
} void Run()
{
try
{
_cameraCapture = new VideoCapture();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
return;
} _fgDetector = new Emgu.CV.VideoSurveillance.BackgroundSubtractorMOG2();
_blobDetector = new CvBlobDetector();
_tracker = new CvTracks(); Application.Idle += ProcessFrame;
} void ProcessFrame(object sender, EventArgs e)
{
Mat frame = _cameraCapture.QueryFrame();
Mat smoothedFrame = new Mat();
CvInvoke.GaussianBlur(frame, smoothedFrame, new Size(, ), ); //filter out noises
//frame._SmoothGaussian(3); #region use the BG/FG detector to find the forground mask
Mat forgroundMask = new Mat();
_fgDetector.Apply(smoothedFrame, forgroundMask);
#endregion CvBlobs blobs = new CvBlobs();
_blobDetector.Detect(forgroundMask.ToImage<Gray, byte>(), blobs);
blobs.FilterByArea(, int.MaxValue); float scale = (frame.Width + frame.Width) / 2.0f;
_tracker.Update(blobs, 0.01 * scale, , ); long detectionTime; List<Rectangle> faces = new List<Rectangle>();
List<Rectangle> eyes = new List<Rectangle>(); IImage image = (IImage)frame;//这一步是重点
faceImage = frame.Bitmap;
DetectFace.Detect(image
, "haarcascade_frontalface_default.xml", "haarcascade_eye.xml",
faces, eyes,
out detectionTime); #region 多人识别
Graphics g1 = Graphics.FromImage(frame.Bitmap);
List<FaceIdentifyModel> tempList = new List<FaceIdentifyModel>();
foreach (Rectangle face in faces)
{
Image rectImage1 = ImageHelper.CaptureImage(frame.Bitmap, face);// 自己封装的方法,通过大图截取矩形框的人脸图片,返回Image 对象
FaceIdentifyModel MoreIdentifyInfo = FaceAPI.FaceIdentify(rectImage1, tb_Group.Text.Trim(), 1, 1);
MoreIdentifyInfo.rect = face;
tempList.Add(MoreIdentifyInfo);
}
Color color_of_pen1 = Color.Gray;
color_of_pen1 = Color.Yellow;
Pen pen1 = new Pen(color_of_pen1, 2.0f); Font font1 = new Font("微软雅黑", 16, GraphicsUnit.Pixel);
SolidBrush drawBrush1 = new SolidBrush(Color.Yellow); tb_Identify.Text = tempList.ToJson();
foreach (var t in tempList)
{
g1.DrawRectangle(pen1, t.rect); if (t.result != null)
{
g1.DrawString(t.result[0].user_info.Replace(",", "\r\n"), font1, drawBrush1, new Point(t.rect.X + 20, t.rect.Y - 20));
} }
#endregion imageBox1.Image = frame;
imageBox2.Image = forgroundMask;
} private void btn_Screenshot_Click(object sender, EventArgs e)
{
if (faceImage != null)
{
System.Drawing.Image ResourceImage = faceImage;
string fileDir = System.Environment.CurrentDirectory + "\\Snapshot\\";
FileHelper.CreateDir(fileDir);
string filePath = fileDir + DateTime.Now.ToString("yyyyMMddHHmmss") + ".png";
ResourceImage.Save(filePath);
MessageBox.Show("保存成功!" + filePath);
} }
}
}

核心代码介绍

获取人脸矩形框,对应的xml 文件要放在根目录下(winform就是 在bin文件夹中)

DetectFace.Detect(image
, "haarcascade_frontalface_default.xml", "haarcascade_eye.xml",
faces, eyes,
out detectionTime);

faces 就是返回的 人脸检测内容, foreach (Rectangle face in faces) 对它进行动态获取人脸,在视频中画出来就可以了 // 调用百度人脸识别接口,该方法 SDK 已经有了,我做了一些简单的封装,就是把截取到的矩形头像发送给百度去识别,这个识别是基于自己在百度建立的人脸库
 FaceIdentifyModel MoreIdentifyInfo = FaceAPI.FaceIdentify(rectImage1, tb_Group.Text.Trim(), 1, 1);//人脸识别 一个人的识别效果比较好  

完成上述工作,人脸识别就完成了,测试过,只要人脸库中的素材清晰,识别基本在99% 左右。

源码地址:https://github.com/linbin524/AI_Project/tree/master

读后感觉不错,有收获可以微信请作者喝杯咖啡,读后有疑问请加微信,拉群研讨,注明来意

基于Emgu CV+百度人脸识别,实现视频动态 人脸抓取与识别的相关教程结束。

《基于Emgu CV+百度人脸识别,实现视频动态 人脸抓取与识别.doc》

下载本文的Word格式文档,以方便收藏与打印。