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

原文地址:https://www.cnblogs.com/tgzhu/p/6207425.html