AI(二):人臉識別

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

目錄:web

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

申請訂閱號算法


示例效果 json


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

開發過程api


  • 參見: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 示例代碼:app

    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(); } }
  •  示例效果

相關文章
相關標籤/搜索