指定人脸检测模型Specify a face detection model

本指南介绍如何为 Azure 人脸 API 指定人脸检测模型。This guide shows you how to specify a face detection model for the Azure Face API.

人脸 API 使用机器学习模型针对图像中的人脸执行运算。The Face API uses machine learning models to perform operations on human faces in images. 我们将会根据客户反馈以及研究成果不断改进模型的准确度,并作为模型更新交付改进结果。We continue to improve the accuracy of our models based on customer feedback and advances in research, and we deliver these improvements as model updates. 开发人员可以选择指定想要使用的人脸检测模型版本;他们可以选择最适合其用例的模型。Developers have the option to specify which version of the face detection model they'd like to use; they can choose the model that best fits their use case.

请继续了解如何在特定的人脸操作中指定人脸检测模型。Read on to learn how to specify the face detection model in certain face operations. 每当人脸 API 将人脸图像转换为其他某种形式的数据时,就会使用人脸检测。The Face API uses face detection whenever it converts an image of a face into some other form of data.

如果你不确定是否要切换到最新的模型,请跳转到评估不同的模型部分来评估新模型,并使用当前数据集比较结果。If you aren't sure whether you should use the latest model, skip to the Evaluate different models section to evaluate the new model and compare results using your current data set.

先决条件Prerequisites

你应该熟悉 AI 人脸检测的概念。You should be familiar with the concept of AI face detection. 如果你不熟悉,请参阅人脸检测的概念指南或操作指南:If you aren't, see the face detection conceptual guide or how-to guide:

使用指定的模型检测人脸Detect faces with specified model

人脸检测可以查找人脸的边界框位置,并识别其视觉特征点。Face detection finds the bounding-box locations of human faces and identifies their visual landmarks. 它会提取人脸的特征并存储这些特征,以便稍后在识别操作中使用。It extracts the face's features and stores them for later use in recognition operations.

使用 Face - Detect API 时,可以使用 detectionModel 参数分配模型版本。When you use the Face - Detect API, you can assign the model version with the detectionModel parameter. 可用值为:The available values are:

  • detection_01
  • detection_02

Face - Detect REST API 的请求 URL 将如下所示:A request URL for the Face - Detect REST API will look like this:

https://api.cognitive.azure.cn/face/v1.0/detect[?returnFaceId][&returnFaceLandmarks][&returnFaceAttributes][&recognitionModel][&returnRecognitionModel][&detectionModel] &subscription-key=<Subscription key>

如果使用客户端库,则可以通过传入一个相应的字符串来分配 detectionModel 的值。If you are using the client library, you can assign the value for detectionModel by passing in an appropriate string. 如果不分配该值,API 将使用默认模型版本 (detection_01)。If you leave it unassigned, the API will use the default model version (detection_01). 请查看适用于 .NET 客户端库的以下代码示例。See the following code example for the .NET client library.

string imageUrl = "https://news.microsoft.com/ceo/assets/photos/06_web.jpg";
var faces = await faceClient.Face.DetectWithUrlAsync(imageUrl, false, false, recognitionModel: "recognition_02", detectionModel: "detection_02");

备注

如果创建的人脸 API 的 LocationChina East 2,则需要将 URL https://api.cognitive.azure.cn/face/v1.0/detect 替换为 https://chinaeast2.api.cognitive.azure.cn/face/v1.0/detectIf the Location of the face api you created is China East 2, you need to replace the url https://api.cognitive.azure.cn/face/v1.0/detect with https://chinaeast2.api.cognitive.azure.cn/face/v1.0/detect.

使用指定的模型将人脸添加到 PersonAdd face to Person with specified model

人脸 API 可以从图像中提取人脸数据,并通过 PersonGroup Person - Add Face API 将其关联到 Person 对象。The Face API can extract face data from an image and associate it with a Person object through the PersonGroup Person - Add Face API. 在此 API 调用中,可以像在 Face - Detect 中一样指定检测模型。In this API call, you can specify the detection model in the same way as in Face - Detect.

请查看适用于 .NET 客户端库的以下代码示例。See the following code example for the .NET client library.

// Create a PersonGroup and add a person with face detected by "detection_02" model
string personGroupId = "mypersongroupid";
await faceClient.PersonGroup.CreateAsync(personGroupId, "My Person Group Name", recognitionModel: "recognition_02");

string personId = (await faceClient.PersonGroupPerson.CreateAsync(personGroupId, "My Person Name")).PersonId;

string imageUrl = "https://news.microsoft.com/ceo/assets/photos/06_web.jpg";
await client.PersonGroupPerson.AddFaceFromUrlAsync(personGroupId, personId, imageUrl, detectionModel: "detection_02");

此代码将创建 ID 为 mypersongroupidPersonGroup,并将一个 Person 对象添加到其中。This code creates a PersonGroup with ID mypersongroupid and adds a Person to it. 然后,它使用 detection_02 模型将一个 Face 对象添加到此 Person 对象。Then it adds a Face to this Person using the detection_02 model. 如果未指定 detectionModel 参数,API 将使用默认模型 detection_01If you don't specify the detectionModel parameter, the API will use the default model, detection_01.

备注

无需对 Person 对象中的所有人脸使用相同的检测模型,并且在检测新人脸时,无需使用相同的检测模型来与 Person 对象进行比较(例如,在 Face - Identify API 中)。You don't need to use the same detection model for all faces in a Person object, and you don't need to use the same detection model when detecting new faces to compare with a Person object (in the Face - Identify API, for example).

使用指定的模型将人脸添加到 FaceListAdd face to FaceList with specified model

将人脸添加到现有的 FaceList 对象时,也可以指定检测模型。You can also specify a detection model when you add a face to an existing FaceList object. 请查看适用于 .NET 客户端库的以下代码示例。See the following code example for the .NET client library.

await faceClient.FaceList.CreateAsync(faceListId, "My face collection", recognitionModel: "recognition_02");

string imageUrl = "https://news.microsoft.com/ceo/assets/photos/06_web.jpg";
await client.FaceList.AddFaceFromUrlAsync(faceListId, imageUrl, detectionModel: "detection_02");

此代码将创建名为 My face collectionFaceList,并使用 detection_02 模型将一个 Face 对象添加到其中。This code creates a FaceList called My face collection and adds a Face to it with the detection_02 model. 如果未指定 detectionModel 参数,API 将使用默认模型 detection_01If you don't specify the detectionModel parameter, the API will use the default model, detection_01.

备注

无需对 FaceList 对象中的所有人脸使用相同的检测模型,并且在检测新人脸时,无需使用相同的检测模型来与 FaceList 对象进行比较You don't need to use the same detection model for all faces in a FaceList object, and you don't need to use the same detection model when detecting new faces to compare with a FaceList object.

评估不同的模型Evaluate different models

不同的人脸检测模型已针对不同的任务进行优化。The different face detection models are optimized for different tasks. 有关差异概述,请参阅下表。See the following table for an overview of the differences.

detection_01detection_01 detection_02detection_02
所有人脸检测操作的默认选项。Default choice for all face detection operations. 已于 2019 年 5 月发布,可以选择性地在所有人脸检测操作中使用。Released in May 2019 and available optionally in all face detection operations.
未针对小尺寸人脸、侧视人脸或模糊人脸进行优化。Not optimized for small, side-view, or blurry faces. 已针对小尺寸人脸、侧视人脸或模糊人脸提高了准确度。Improved accuracy on small, side-view, and blurry faces.
如果在检测调用中指定了人脸属性(头部姿势、年龄、表情等),则返回这些属性。Returns face attributes (head pose, age, emotion, and so on) if they're specified in the detect call. 不返回人脸属性。Does not return face attributes.
如果在检测调用中指定了人脸特征点,则返回这些特征点。Returns face landmarks if they're specified in the detect call. 不返回人脸特征点。Does not return face landmarks.

比较 detection_01detection_02 模型性能的最佳方法是针对示例数据集使用它们。The best way to compare the performances of the detection_01 and detection_02 models is to use them on a sample dataset. 我们建议使用每个检测模型针对各种图像(尤其是许多人脸的图像,或者难以辨认的人脸的图像)调用 Face - Detect API。We recommend calling the Face - Detect API on a variety of images, especially images of many faces or of faces that are difficult to see, using each detection model. 请注意每个模型返回的人脸数。Pay attention to the number of faces that each model returns.

后续步骤Next steps

本文已介绍如何指定要与不同人脸 API 配合使用的检测模型。In this article, you learned how to specify the detection model to use with different Face APIs. 接下来,请遵循相应的快速入门开始使用人脸识别。Next, follow a quickstart to get started using face detection.