c# 利用AForge和百度AI开发实时人脸识别
baiduAIFaceIdentify項目是C#語言,集成百度AI的SDK利用AForge開發(fā)的實時人臉識別的小demo,里邊包含了人臉檢測識別,人臉注冊,人臉登錄等功能
人臉實時檢測識別功能
思路是利用AForge打開攝像頭,通過攝像頭獲取到的圖像顯示在winform窗體中AForge的控件中,利用AForge控件中的NewFrame事件獲取要顯示的每一幀的圖像,獲取圖像傳輸?shù)桨俣華I平臺進行人臉檢測,并且將檢測結(jié)果反饋到界面顯示的圖像中。在這個過程中有兩個問題,獲取圖像上傳到百度AI平臺進行分析需要時間,這個時間跟網(wǎng)絡(luò)有關(guān),所以需要單獨一個線程進行人臉識別,第二個問題,百度人臉識別接口開發(fā)者一秒內(nèi)只能掉用2次接口,所以需要控制不是每一幀的圖像都要上傳。所以基于以上思路
首先頁面初始化的時候獲取視頻設(shè)備、啟動一個單獨線程控制1秒內(nèi)人臉檢測的次數(shù):
private void Form1_Load(object sender, EventArgs e){/// 獲取電腦已經(jīng)安裝的視頻設(shè)備videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);if (videoDevices!=null && videoDevices.Count>0){foreach (FilterInfo device in videoDevices){comboBox1.Items.Add(device.Name);}comboBox1.SelectedIndex = 0;}videoSourcePlayer1.NewFrame += VideoSourcePlayer1_NewFrame;// 開發(fā)者在百度AI平臺人臉識別接口只能1秒中調(diào)用2次,所以需要做 定時開始檢測,每個一秒檢測2次ThreadPool.QueueUserWorkItem(new WaitCallback(p => {while (true){IsStart = true;Thread.Sleep(500);}}));}?
其次,在NewFrame的回調(diào)方法中,根據(jù)IsStart判斷是否要開始人臉識別,并且另外啟動一個線程進行人臉識別操作,判斷如果已經(jīng)有識別過的結(jié)構(gòu),根據(jù)返回的人臉的位置,在當前的一幀圖像中繪制方框指示出識別出的人臉位置
private void VideoSourcePlayer1_NewFrame(object sender, ref Bitmap image){try{if (IsStart){IsStart = false;// 在線程池中另起一個線程進行人臉檢測,這樣不會造成界面視頻卡頓現(xiàn)象ThreadPool.QueueUserWorkItem(new WaitCallback(this.Detect), image.Clone());}if (location != null){try{// 繪制方框套住人臉Graphics g = Graphics.FromImage(image);g.DrawLine(new Pen(Color.Black), new System.Drawing.Point(location.left, location.top), new System.Drawing.Point(location.left + location.width, location.top));g.DrawLine(new Pen(Color.Black), new System.Drawing.Point(location.left, location.top), new System.Drawing.Point(location.left, location.top + location.height));g.DrawLine(new Pen(Color.Black), new System.Drawing.Point(location.left, location.top + location.height), new System.Drawing.Point(location.left + location.width, location.top + location.height));g.DrawLine(new Pen(Color.Black), new System.Drawing.Point(location.left + location.width, location.top), new System.Drawing.Point(location.left + location.width, location.top + location.height));g.Dispose();}catch (Exception ex){ClassLoger.Error("VideoSourcePlayer1_NewFrame", ex);}}} catch (Exception ex){ClassLoger.Error("VideoSourcePlayer1_NewFrame1", ex);}}?
人臉注冊。
在一些類似刷臉簽到、刷臉登錄的應(yīng)用場景中,根據(jù)人臉獲取人物信息,前提就是人臉注冊,人臉注冊就是獲取當前攝像頭的一幀圖像,調(diào)用百度AI的人臉注冊接口進行注冊
// 用戶IDstring uid = "1";// 用戶資料,長度限制256Bstring userInfo = textBox6.Text.Trim();// 用戶組IDstring groupId = textBox5.Text.Trim();if (comboBox1.Items.Count <= 0){MessageBox.Show("請插入視頻設(shè)備");return;}try{if (videoSourcePlayer1.IsRunning){BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(videoSourcePlayer1.GetCurrentVideoFrame().GetHbitmap(),IntPtr.Zero,Int32Rect.Empty,BitmapSizeOptions.FromEmptyOptions());var img = BitmapSource2Byte(bitmapSource);var options = new Dictionary<string, object>{{"action_type", "replace"}};var result = client.UserAdd(uid, userInfo, groupId, img, options);if (result.ToString().Contains("error_code")){MessageBox.Show("注冊失敗:" + result.ToString());}else{MessageBox.Show("注冊成功");}}}catch (Exception ex){MessageBox.Show("攝像頭異常:" + ex.Message);}?
人臉登錄
人臉登錄和人臉注冊的方式一樣,只不過調(diào)用的是百度AI的人臉登錄接口
// 用戶IDstring uid = "1";// 用戶資料,長度限制256Bstring userInfo = textBox6.Text.Trim();// 用戶組IDstring groupId = textBox5.Text.Trim();if (comboBox1.Items.Count <= 0){MessageBox.Show("請插入視頻設(shè)備");return;}try{if (videoSourcePlayer1.IsRunning){BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(videoSourcePlayer1.GetCurrentVideoFrame().GetHbitmap(),IntPtr.Zero,Int32Rect.Empty,BitmapSizeOptions.FromEmptyOptions());var img = BitmapSource2Byte(bitmapSource);// 如果有可選參數(shù)//var options = new Dictionary<string, object>{// {"ext_fields", "faceliveness"},// {"user_top_num", 3}//};var result = client.Identify(groupId, img);FaceIdentifyInfo info = JsonHelper.DeserializeObject<FaceIdentifyInfo>(result.ToString());if (info!=null && info.result!=null && info.result.Length>0){textBox7.Text = info.result[0].user_info;}}}catch (Exception ex){MessageBox.Show("攝像頭異常:" + ex.Message);}源碼地址:https://github.com/liemei/baiduAIFaceIdentify
轉(zhuǎn)載于:https://www.cnblogs.com/liemei/p/8318414.html
總結(jié)
以上是生活随笔為你收集整理的c# 利用AForge和百度AI开发实时人脸识别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小度app语音功能
- 下一篇: vue 在已有的购买列表中(数据库返回的