c# 利用AForge和百度AI开发实时人脸识别
baiduAIFaceIdentify項(xiàng)目是C#語(yǔ)言,集成百度AI的SDK利用AForge開(kāi)發(fā)的實(shí)時(shí)人臉識(shí)別的小demo,里邊包含了人臉檢測(cè)識(shí)別,人臉注冊(cè),人臉登錄等功能
人臉實(shí)時(shí)檢測(cè)識(shí)別功能
思路是利用AForge打開(kāi)攝像頭,通過(guò)攝像頭獲取到的圖像顯示在winform窗體中AForge的控件中,利用AForge控件中的NewFrame事件獲取要顯示的每一幀的圖像,獲取圖像傳輸?shù)桨俣華I平臺(tái)進(jìn)行人臉檢測(cè),并且將檢測(cè)結(jié)果反饋到界面顯示的圖像中。在這個(gè)過(guò)程中有兩個(gè)問(wèn)題,獲取圖像上傳到百度AI平臺(tái)進(jìn)行分析需要時(shí)間,這個(gè)時(shí)間跟網(wǎng)絡(luò)有關(guān),所以需要單獨(dú)一個(gè)線程進(jìn)行人臉識(shí)別,第二個(gè)問(wèn)題,百度人臉識(shí)別接口開(kāi)發(fā)者一秒內(nèi)只能掉用2次接口,所以需要控制不是每一幀的圖像都要上傳。所以基于以上思路
首先頁(yè)面初始化的時(shí)候獲取視頻設(shè)備、啟動(dòng)一個(gè)單獨(dú)線程控制1秒內(nèi)人臉檢測(cè)的次數(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;// 開(kāi)發(fā)者在百度AI平臺(tái)人臉識(shí)別接口只能1秒中調(diào)用2次,所以需要做 定時(shí)開(kāi)始檢測(cè),每個(gè)一秒檢測(cè)2次ThreadPool.QueueUserWorkItem(new WaitCallback(p => {while (true){IsStart = true;Thread.Sleep(500);}}));}?
其次,在NewFrame的回調(diào)方法中,根據(jù)IsStart判斷是否要開(kāi)始人臉識(shí)別,并且另外啟動(dòng)一個(gè)線程進(jìn)行人臉識(shí)別操作,判斷如果已經(jīng)有識(shí)別過(guò)的結(jié)構(gòu),根據(jù)返回的人臉的位置,在當(dāng)前的一幀圖像中繪制方框指示出識(shí)別出的人臉位置
private void VideoSourcePlayer1_NewFrame(object sender, ref Bitmap image){try{if (IsStart){IsStart = false;// 在線程池中另起一個(gè)線程進(jìn)行人臉檢測(cè),這樣不會(huì)造成界面視頻卡頓現(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);}}?
人臉注冊(cè)。
在一些類(lèi)似刷臉簽到、刷臉登錄的應(yīng)用場(chǎng)景中,根據(jù)人臉獲取人物信息,前提就是人臉注冊(cè),人臉注冊(cè)就是獲取當(dāng)前攝像頭的一幀圖像,調(diào)用百度AI的人臉注冊(cè)接口進(jìn)行注冊(cè)
// 用戶(hù)IDstring uid = "1";// 用戶(hù)資料,長(zhǎng)度限制256Bstring userInfo = textBox6.Text.Trim();// 用戶(hù)組IDstring groupId = textBox5.Text.Trim();if (comboBox1.Items.Count <= 0){MessageBox.Show("請(qǐng)插入視頻設(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("注冊(cè)失敗:" + result.ToString());}else{MessageBox.Show("注冊(cè)成功");}}}catch (Exception ex){MessageBox.Show("攝像頭異常:" + ex.Message);}?
人臉登錄
人臉登錄和人臉注冊(cè)的方式一樣,只不過(guò)調(diào)用的是百度AI的人臉登錄接口
// 用戶(hù)IDstring uid = "1";// 用戶(hù)資料,長(zhǎng)度限制256Bstring userInfo = textBox6.Text.Trim();// 用戶(hù)組IDstring groupId = textBox5.Text.Trim();if (comboBox1.Items.Count <= 0){MessageBox.Show("請(qǐng)插入視頻設(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开发实时人脸识别的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 小度app语音功能
- 下一篇: C#给字符串赋予字面值——字符串插入、转