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

总目录地址: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 是具体检测人脸方法。

 

 

 
  1 //----------------------------------------------------------------------------
  2 //  Copyright (C) 2004-2017 by EMGU Corporation. All rights reserved.       
  3 //----------------------------------------------------------------------------
  4 
  5 using System;
  6 using System.Collections.Generic;
  7 using System.ComponentModel;
  8 using System.Data;
  9 using System.Drawing;
 10 using System.Text;
 11 using System.Windows.Forms;
 12 
 13 using Emgu.CV;
 14 using Emgu.CV.Cvb;
 15 using Emgu.CV.UI;
 16 using Emgu.CV.CvEnum;
 17 using Emgu.CV.Structure;
 18 using Emgu.CV.VideoSurveillance;
 19 using FaceDetection;
 20 using Emgu.CV.Cuda;
 21 using AOP.Common;
 22 using System.Drawing.Imaging;
 23 using Baidu.Aip.API;
 24 using System.Threading;
 25 using BaiduAIAPI.Model;
 26 
 27 namespace VideoSurveilance
 28 {
 29     public partial class VideoSurveilance : Form
 30     {
 31 
 32         private static VideoCapture _cameraCapture;
 33 
 34         private static BackgroundSubtractor _fgDetector;
 35         private static Emgu.CV.Cvb.CvBlobDetector _blobDetector;
 36         private static Emgu.CV.Cvb.CvTracks _tracker;
 37 
 38         private static Queue<ImageModel> FacIdentifyQueue = new Queue<ImageModel>();
 39         public Image faceImage;
 40         Thread t1;
 41         public VideoSurveilance()
 42         {
 43             InitializeComponent();
 44             Run();
 45         }
 46 
 47         void Run()
 48         {
 49             try
 50             {
 51                 _cameraCapture = new VideoCapture();
 52             }
 53             catch (Exception e)
 54             {
 55                 MessageBox.Show(e.Message);
 56                 return;
 57             }
 58 
 59             _fgDetector = new Emgu.CV.VideoSurveillance.BackgroundSubtractorMOG2();
 60             _blobDetector = new CvBlobDetector();
 61             _tracker = new CvTracks();
 62 
 63             Application.Idle += ProcessFrame;
 64         }
 65 
 66         void ProcessFrame(object sender, EventArgs e)
 67         {
 68             Mat frame = _cameraCapture.QueryFrame();
 69             Mat smoothedFrame = new Mat();
 70             CvInvoke.GaussianBlur(frame, smoothedFrame, new Size(3, 3), 1); //filter out noises
 71                                                                             //frame._SmoothGaussian(3); 
 72 
 73             #region use the BG/FG detector to find the forground mask
 74             Mat forgroundMask = new Mat();
 75             _fgDetector.Apply(smoothedFrame, forgroundMask);
 76             #endregion
 77 
 78             CvBlobs blobs = new CvBlobs();
 79             _blobDetector.Detect(forgroundMask.ToImage<Gray, byte>(), blobs);
 80             blobs.FilterByArea(100, int.MaxValue);
 81 
 82             float scale = (frame.Width + frame.Width) / 2.0f;
 83             _tracker.Update(blobs, 0.01 * scale, 5, 5);
 84 
 85             long detectionTime;
 86 
 87             List<Rectangle> faces = new List<Rectangle>();
 88             List<Rectangle> eyes = new List<Rectangle>();
 89 
 90             IImage image = (IImage)frame;//这一步是重点
 91             faceImage = frame.Bitmap;
 92             DetectFace.Detect(image
 93              , "haarcascade_frontalface_default.xml", "haarcascade_eye.xml",
 94               faces, eyes,
 95               out detectionTime);
 96 
 97             #region 多人识别
 98            Graphics g1 = Graphics.FromImage(frame.Bitmap);
 99            List<FaceIdentifyModel> tempList = new List<FaceIdentifyModel>();
100            foreach (Rectangle face in faces)
101             {
102                Image rectImage1 = ImageHelper.CaptureImage(frame.Bitmap, face);// 自己封装的方法,通过大图截取矩形框的人脸图片,返回Image 对象
103                FaceIdentifyModel MoreIdentifyInfo = FaceAPI.FaceIdentify(rectImage1, tb_Group.Text.Trim(), 1, 1);
104 MoreIdentifyInfo.rect = face; 105 tempList.Add(MoreIdentifyInfo); 106 }
107 Color color_of_pen1 = Color.Gray; 108 color_of_pen1 = Color.Yellow; 109 Pen pen1 = new Pen(color_of_pen1, 2.0f); 110 111 Font font1 = new Font("微软雅黑", 16, GraphicsUnit.Pixel); 112 SolidBrush drawBrush1 = new SolidBrush(Color.Yellow); 113 114 115 tb_Identify.Text = tempList.ToJson(); 116 foreach (var t in tempList) 117 { 118 g1.DrawRectangle(pen1, t.rect); 119 120 if (t.result != null) 121 { 122 g1.DrawString(t.result[0].user_info.Replace(",", "\r\n"), font1, drawBrush1, new Point(t.rect.X + 20, t.rect.Y - 20)); 123 } 124 125 } 126 #endregion 127 128 imageBox1.Image = frame; 129 imageBox2.Image = forgroundMask; 130 } 131 132 133 134 private void btn_Screenshot_Click(object sender, EventArgs e) 135 { 136 if (faceImage != null) 137 { 138 System.Drawing.Image ResourceImage = faceImage; 139 string fileDir = System.Environment.CurrentDirectory + "\\Snapshot\\"; 140 FileHelper.CreateDir(fileDir); 141 string filePath = fileDir + DateTime.Now.ToString("yyyyMMddHHmmss") + ".png"; 142 ResourceImage.Save(filePath); 143 MessageBox.Show("保存成功!" + filePath); 144 } 145 146 } 147 } 148 }

 

核心代码介绍

获取人脸矩形框,对应的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

 

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

 

 

posted @ 2017-12-07 10:12  linbin524  阅读(9254)  评论(15编辑  收藏  举报