AI(二):人臉識別


       微軟提供的人臉識別服務可檢測圖片中一個或者多個人臉,並為人臉標記出邊框,同時還可獲得基於機器學習技術做出的面部特征預測。可支持的人臉功能有:年齡、性別、頭部姿態、微笑檢測、胡須檢測以及27個面部重要特征點位置等。FaceAPI 提供兩個主要功能: 人臉檢測和識別

目錄:

  • 申請subscription key
  • 示例效果
  • 開發示例
  • AForge.Net

申請訂閱號


示例效果 


  • winform示例版,調用微軟提供的SDK,見下面介紹
  •  微信集成版, 如下圖,開發過程中使用 http 直接調用

開發過程


  • 參見:https://www.azure.cn/cognitive-services/en-us/face-api/documentation/get-started-with-face-api/GettingStartedwithFaceAPIinCSharp
  • 在VS工程的NuGet Package Manager 管理窗口,程序包源:nuget.org, 搜索 Microsoft.ProjectOxford.Face ,進行安裝
  • sdk調用示例代碼: 圖片轉byte[]
     using (Stream s = new MemoryStream(bytes)) { var requiredFaceAttributes = new FaceAttributeType[] { FaceAttributeType.Age, FaceAttributeType.Gender, FaceAttributeType.Smile, FaceAttributeType.FacialHair, FaceAttributeType.HeadPose, FaceAttributeType.Glasses }; var faces = await Utils.FaceClient.DetectAsync(s, returnFaceLandmarks: true, returnFaceAttributes: requiredFaceAttributes); }
  •  也可直接使用http請求,參見:https://dev.projectoxford.ai/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236

  • 參數信息如下:

  •  http post 示例代碼:

    string URL = "你圖片的url"; var client = new HttpClient(); var queryString = HttpUtility.ParseQueryString(string.Empty); // Request headers
                client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "你申請的key"); // Request parameters
                queryString["returnFaceId"] = "true"; queryString["returnFaceLandmarks"] = "false"; queryString["returnFaceAttributes"] = "age,gender,smile"; var uri = "https://api.projectoxford.ai/face/v1.0/detect?" + queryString; HttpResponseMessage response; byte[] byteData = Encoding.UTF8.GetBytes("{\"url\":\"" + URL + "\"}"); using (var content = new ByteArrayContent(byteData)) { content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var task = client.PostAsync(uri, content); response = task.Result; var task1 = response.Content.ReadAsStringAsync(); string JSON = task1.Result; }
  • 人臉識別http參數如下:(注意:要識別出人臉的身份,你必須先定義person,參見 personGroup 、Person介紹 https://www.azure.cn/cognitive-services/en-us/face-api/documentation/face-api-how-to-topics/howtoidentifyfacesinimage

  • 示例代碼
    var client = new HttpClient(); var queryString = HttpUtility.ParseQueryString(string.Empty); client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "XXX"); var uri = "https://api.projectoxford.ai/face/v1.0/identify "; HttpResponseMessage response; byte[] byteData = Encoding.UTF8.GetBytes("{\"faceIds\":[\"XXX\"],\"personGroupId\":\"XXX\",\"maxNumOfCandidatesReturned\":5}"); using (var content = new ByteArrayContent(byteData)) { content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var task = client.PostAsync(uri, content); response = task.Result; var task1 = response.Content.ReadAsStringAsync(); string JSON = task1.Result; }
  • 根據人臉信息識別出身份后,獲取個人信息,參數如下:
  • 示例代碼
    var client = new HttpClient(); var queryString = HttpUtility.ParseQueryString(string.Empty); client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "你申請的key"); var uri = "https://api.projectoxford.ai/face/v1.0/persongroups/你上傳的分組/persons/" + personID; var task = client.GetStringAsync(uri); var  response = task.Result; return JsonConvert.DeserializeObject<Person>(task.Result);
  •  

AForge.Net


  •  AForge.Net是一個專門為開發者和研究者基於C#框架設計的,他包括計算機視覺與人工智能,圖像處理,神經網絡,遺傳算法,機器學習,模糊系統,機器人控制等領域, 我主要使用這個類庫中的vedio 來啟動筆記本攝像頭進行圖片抓拍
  • 類庫下載地址: http://www.aforgenet.com/framework/downloads.html
  • 添加控件: 在工具箱中添加AForge.Control,VideoSourcePlayer就是我們要用的控件
  • 示例代碼如下:聲明變量
    FilterInfoCollection videoDevices; VideoCaptureDevice videoSource; public int selectedDeviceIndex = 0;
  • 啟動攝像頭示例代碼
    videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); selectedDeviceIndex = 0; videoSource = new VideoCaptureDevice(videoDevices[selectedDeviceIndex].MonikerString);//連接攝像頭。
                videoSource.VideoResolution = videoSource.VideoCapabilities[selectedDeviceIndex]; videoSourcePlayer1.VideoSource = videoSource; // set NewFrame event handler
                videoSourcePlayer1.Start();
  •  抓拍示代碼

    if (videoSource == null) return; Bitmap bitmap = videoSourcePlayer1.GetCurrentVideoFrame(); string fileName = string.Format("{0}.jpg", DateTime.Now.ToString("yyyyMMddHHmmssfff")); this.filePath = string.Format("c:\\temp\\{0}", fileName); bitmap.Save(this.filePath, ImageFormat.Jpeg); this.labelControl1.Text = string.Format("存儲目錄:{0}", this.filePath); bitmap.Dispose(); videoDevices.Clear();
  •  窗體關閉事件

    if (this.videoSource != null) { if (this.videoSource.IsRunning) { this.videoSource.Stop(); } }
  •  示例效果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM