Skip to main content

Azure Face服务介绍和快速入门

分类:  Azure认知服务 标签:  #Azure #人工智能 #Face 发布于: 2023-06-10 22:22:11

Azure提供的人脸服务是可以应用于很多个应用场景的服务,主要包括如下几个:

  • 人脸检测:这个API可以从图片中检测出人脸,并且返回人脸在图片中的位置,该位置用矩形坐标的形式返回,同时需要注意的是,该API还会返回一系列和人脸相关的属性,例如:头部姿势,性别,年龄,情感,面部的毛发,是否有佩戴眼镜等等。
  • 人脸验证:顾名思义就是用于判断是否一致的,是否是同一个人,可以用于人脸识别认证这一类的应用。
  • 人脸识别:需要区分一下和人脸验证之间的区别,人脸验证时给出一个图片,确实这个图片是不是某人,但是人脸识别,可以从一堆图像中找出某个特定人的所有图片。
  • 找相似: 从图片库中找出和给出图片相似的图片,并非一定是同一个人。
  • 人脸分组:可以通过一些共性,例如都是男性/女性/小孩 将图片进行分组。

微软除了提供了人脸服务,还是提供计算机视觉服务,专门用于图片识别的,但是图片识别所支持的功能是要少于人脸服务的。所以如果需要更强大的,更多的功能还是需要选择人脸服务。

创建人脸服务

创建之前您还是需要一个Azure的订阅,使用Azure订阅登录到Azure Portal之后,在Marketplace里搜索Face, 然后输入相应的信息点击创建,创建成功后转到Keys and Endpoint页面,记录下Key 1EndPointLocation, 如下图:


快速入门

本节的代码可以在如下的位置找到:https://github.com/hylinux/azure-demo/tree/main/dotnet/cognitive-service/Face/QuickStart

使用.net cli工具创建一个新的基于Console的应用,并添加需要的nuget包:

dotnet new console -n QuickStart
cd QuickStart
dotnet add package Microsoft.Azure.CognitiveServices.Vision.Face --prerelease

添加包引用和添加相应的变量和值

打开类Program, 添加如下的包引用:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

using Microsoft.Azure.CognitiveServices.Vision.Face;
using Microsoft.Azure.CognitiveServices.Vision.Face.Models;

定义相应的变量,这些变量用于定义face服务的客户端以及需要使用的模型等等:

//定义人脸服务的key和endpoint
const string SUBSCRIPTION_KEY = "<Your Key>";
const string ENDPOINT = "https://mytestface.cognitiveservices.azure.cn/";

//定义图片地址
const string IMAGE_BASE_URL = "https://csdx.blob.core.windows.net/resources/Face/Images/";

//定义识别模型
const string RECOGNITION_MODEL4 = RecognitionModel.Recognition04;

//定义组
static string personGroupId = Guid.NewGuid().ToString();

在Main 方法里定义如下的方法调用:

static void Main(string[] args)
{

    //定义需要使用的识别模型,需要注意的是Recognition04这个模型是2021年发布的。
    const string RECOGNITION_MODEL4 = RecognitionModel.Recognition04;

    //创建Face服务客户端
    IFaceClient client = Authenticate(ENDPOINT, SUBSCRIPTION_KEY);

    //人脸识别
    DetectFaceExtract(client, IMAGE_BASE_URL, RECOGNITION_MODEL4).Wait();

    //查找相似人脸
    FindSimilar(client, IMAGE_BASE_URL, RECOGNITION_MODEL4).Wait();
    
    //人脸验证
    Verify(client, IMAGE_BASE_URL, RECOGNITION_MODEL4).Wait();

    // Identify - recognize a face(s) in a person group (a person group is created in this example).
    //在一个组里识别人脸。
    IdentifyInPersonGroup(client, IMAGE_BASE_URL, RECOGNITION_MODEL4).Wait();

    LargePersonGroup(client, IMAGE_BASE_URL, RECOGNITION_MODEL4).Wait();

    //分组
    Group(client, IMAGE_BASE_URL, RECOGNITION_MODEL4).Wait();

    FaceListOperations(client, IMAGE_BASE_URL).Wait();
    // Large FaceList - create a large face list, then get data
    LargeFaceListOperations(client, IMAGE_BASE_URL).Wait();

    // <snippet_persongroup_delete>
    // At end, delete person groups in both regions (since testing only)
    Console.WriteLine("========DELETE PERSON GROUP========");
    Console.WriteLine();
    DeletePersonGroup(client, personGroupId).Wait();
    // </snippet_persongroup_delete>

    Console.WriteLine("End of quickstart.");
}

关于其他函数的定义,请参考上面列出的源代码,所有的应用完成后,运行dotnet run, 进行测试, 运行结果如下:

========DETECT FACES========

1 face(s) detected from image `detection1.jpg`.
Face attributes for detection1.jpg:
Rectangle(Left/Top/Width/Height) : 459 124 227 227
Accessories : Glasses
Age : 25
Blur : Low
Emotion : Happiness
Exposure : GoodExposure
FacialHair : No
Gender : Female
Glasses : ReadingGlasses
Hair : Brown
HeadPose : Pitch: -12, Roll: -13.6, Yaw: 5
Makeup : Yes
Noise : Low
Occlusion : EyeOccluded: No  ForeheadOccluded: No   MouthOccluded: No
Smile : 1


========FIND SIMILAR========

1 face(s) detected from image `Family1-Dad1.jpg`
1 face(s) detected from image `Family1-Daughter1.jpg`
1 face(s) detected from image `Family1-Mom1.jpg`
1 face(s) detected from image `Family1-Son1.jpg`
1 face(s) detected from image `Family2-Lady1.jpg`
1 face(s) detected from image `Family2-Man1.jpg`
1 face(s) detected from image `Family3-Lady1.jpg`
1 face(s) detected from image `Family3-Man1.jpg`
1 face(s) detected from image `findsimilar.jpg`

Faces from findsimilar.jpg & ID:f65e9033-9d15-4e56-8910-cc2be43deb79 are similar with confidence: 0.94382.

========VERIFY========

1 face(s) detected from image `Family1-Dad1.jpg `
1 faces detected from image `Family1-Dad1.jpg`.
1 face(s) detected from image `Family1-Dad2.jpg `
1 faces detected from image `Family1-Dad2.jpg`.
1 face(s) detected from image `Family1-Dad3.jpg `
1 faces detected from image `Family1-Dad3.jpg`.
1 face(s) detected from image `Family1-Son1.jpg `
1 faces detected from image `Family1-Son1.jpg`.
Faces from Family1-Dad3.jpg & Family1-Dad1.jpg are of the same (Positive) person, similarity confidence: 0.97198.
Faces from Family1-Son1.jpg & Family1-Dad1.jpg are of different (Positive) persons, similarity confidence: 0.16163.

========IDENTIFY FACES========

Create a person group (7ea40680-1fca-405f-bc06-8922066bb0ef).
Create a person group person 'Family1-Dad'.
Add face to the person group person(Family1-Dad) from image `Family1-Dad1.jpg`
Add face to the person group person(Family1-Dad) from image `Family1-Dad2.jpg`
Create a person group person 'Family1-Mom'.
Add face to the person group person(Family1-Mom) from image `Family1-Mom1.jpg`
Add face to the person group person(Family1-Mom) from image `Family1-Mom2.jpg`
Create a person group person 'Family1-Son'.
Add face to the person group person(Family1-Son) from image `Family1-Son1.jpg`
Add face to the person group person(Family1-Son) from image `Family1-Son2.jpg`
Create a person group person 'Family1-Daughter'.
Add face to the person group person(Family1-Daughter) from image `Family1-Daughter1.jpg`
Add face to the person group person(Family1-Daughter) from image `Family1-Daughter2.jpg`
Create a person group person 'Family2-Lady'.
Add face to the person group person(Family2-Lady) from image `Family2-Lady1.jpg`
Add face to the person group person(Family2-Lady) from image `Family2-Lady2.jpg`
Create a person group person 'Family2-Man'.
Add face to the person group person(Family2-Man) from image `Family2-Man1.jpg`
Add face to the person group person(Family2-Man) from image `Family2-Man2.jpg`

Train person group 7ea40680-1fca-405f-bc06-8922066bb0ef.
Training status: Succeeded.

4 face(s) detected from image `identification1.jpg`
Person 'Family1-Dad' is identified for face in: identification1.jpg - 36f58423-512c-44c9-b374-b1e0b4398564, confidence: 0.96725.
Person 'Family1-Mom' is identified for face in: identification1.jpg - 879bbcbc-9838-4800-bf2e-709158db22da, confidence: 0.96921.
Person 'Family1-Daughter' is identified for face in: identification1.jpg - 56e93e63-9168-4c84-9166-c437c0433749, confidence: 0.93802.
Person 'Family1-Son' is identified for face in: identification1.jpg - 68071f33-c093-4a39-a40b-785d310d8afc, confidence: 0.92886.

建议大家将代码下载下来试着运行一下,有其他问题,请联系我。