指定人脸识别模型Specify a face recognition model

本指南介绍如何指定识别模型,以使用 Azure 人脸服务进行人脸检测、识别和相似性搜索。This guide shows you how to specify a face recognition model for face detection, identification and similarity search using the Azure Face service.

人脸服务使用机器学习模型对图像中的人脸执行运算。The Face service 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 recognition model they'd like to use; they can choose the model that best fits their use case.

Azure 人脸服务具有四种可用的识别模型。The Azure Face service has four recognition models available. 我们将继续支持模型 recognition_01(2017 年发布)、recognition_02(2019 年发布)和 recognition_03(2020 年发布),以确保为使用通过这些模型创建的 FaceList 或 PersonGroup 的客户提供后向兼容性 。The models recognition_01 (published 2017), recognition_02 (published 2019), and recognition_03 (published 2020) are continually supported to ensure backwards compatibility for customers using FaceLists or PersonGroup s created with these models. FaceList 或 Persongroup 将始终使用创建它时所用的识别模型,并且将新的人脸添加到其中时,它们将与此模型相关联 。A FaceList or Persongroup will always use the recognition model it was created with, and new faces will become associated with this model when they are added. 创建后无法更改此设置,客户需要将相应的识别模型与相应的 FaceList 或 PersonGroup 一起使用 。This cannot be changed after creation and customers will need to use the corresponding recognition model with the corresponding FaceList or PersonGroup.

你可以根据自己的需要转到较新的识别模型;但是,你将需要使用所选的识别模型来创建新的 FaceList 和 PersonGroup。You can move to later recognition models at your own convenience; however, you will need to create new FaceLists and PersonGroups with the recognition model of your choice.

recognition_04 模型(2021 年发布)是当前可用的最准确的模型。The recognition_04 model (published 2021) is the most accurate model currently available. 如果你是新客户,建议使用此模型。If you're a new customer, we recommend using this model. Recognition_04 将在相似性比较和人员匹配比较方面提供更高的准确性。Recognition_04 will provide improved accuracy for both similarity comparisons and person-matching comparisons. Recognition_04 改进了对佩戴面部遮盖物(外科口罩、N95 口罩、布口罩)的已注册用户的识别。Recognition_04 improves recognition for enrolled users wearing face covers (surgical masks, N95 masks, cloth masks). 现在,你可以构建安全且无缝的用户体验,以便使用最新的 detection_03 模型来检测已注册的用户是否佩戴了面部遮盖物,然后使用最新的 recognition_04 模型来识别他们的身份 。Now you can build safe and seamless user experiences that use the latest detection_03 model to detect whether an enrolled user is wearing a face cover, and then recognize who they are with the latest recognition_04 model. 请注意,每个模型都独立于其他模型运行,并且为一个模型设置的置信度阈值不应在其他识别模型之间进行比较。Note that each model operates independently of the others, and a confidence threshold set for one model is not meant to be compared across the other recognition models.

请继续阅读,了解如何在不同的人脸操作中指定选定的模型,同时避免模型冲突。Read on to learn how to specify a selected model in different Face operations while avoiding model conflicts. 如果你是高级用户,想要确定是否应该切换到最新的模型,可以转到评估不同的模型部分来评估新模型,并使用当前数据集比较结果。If you are an advanced user and would like to determine whether you should switch to 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 concepts of AI face detection and identification. 否则请先阅读以下指南:If you aren't, see these guides first:

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

人脸检测可以识别人脸的视觉特征点,并查找其边界框位置。Face detection identifies the visual landmarks of human faces and finds their bounding-box locations. 它还能提取人脸的特征,并将其存储以在识别时使用。It also extracts the face's features and stores them for use in identification. 所有这些信息构成了人脸的表示形式。All of this information forms the representation of one face.

提取人脸特征时将使用识别模型,因此,可以在执行检测操作时指定模型版本。The recognition model is used when the face features are extracted, so you can specify a model version when performing the Detect operation.

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

  • recognition_01recognition_01
  • recognition_02recognition_02
  • recognition_03recognition_03
  • recognition_04recognition_04

或者,可以指定 returnRecognitionModel 参数(默认为 false)来指示是否应在响应中返回 recognitionModelOptionally, you can specify the returnRecognitionModel parameter (default false) to indicate whether recognitionModel should be returned in response. 因此,Face - Detect REST API 的请求 URL 将如下所示:So, 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]&subscription-key=<Subscription key>

如果使用客户端库,则可以通过传递一个表示版本的字符串来分配 recognitionModel 的值。If you are using the client library, you can assign the value for recognitionModel by passing a string representing the version. 如果不分配该值,将使用 recognition_01 的默认模型版本。If you leave it unassigned, a default model version of recognition_01 will be used. 请查看适用于 .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, true, true, recognitionModel: "recognition_01", returnRecognitionModel: true);

使用指定的模型识别人脸Identify faces with specified model

人脸服务可以从图像中提取人脸数据,并将其与 Person 对象相关联(例如,通过 Add face API 调用),多个 Person 对象可以一起存储在 PersonGroup 中 。The Face service can extract face data from an image and associate it with a Person object (through the Add face API call, for example), and multiple Person objects can be stored together in a PersonGroup. 然后,可以根据 PersonGroup 比较新的人脸(使用 Face - Identify 调用),该组中匹配的人员将被识别。Then, a new face can be compared against a PersonGroup (with the Face - Identify call), and the matching person within that group can be identified.

PersonGroup 中的所有 Person 应有一个唯一的识别模型,在创建该组(使用 [PersonGroup - Create] 或 [LargePersonGroup - Create])时,可以使用 recognitionModel 参数指定此模型。A PersonGroup should have one unique recognition model for all of the Person s, and you can specify this using the recognitionModel parameter when you create the group ([PersonGroup - Create] or [LargePersonGroup - Create]). 如果不指定此参数,则会使用原始的 recognition_01 模型。If you do not specify this parameter, the original recognition_01 model is used. 某个组始终使用创建它时所用的识别模型,将新的人脸添加到该组时,它们将与此模型相关联;创建组后无法更改此行为。A group will always use the recognition model it was created with, and new faces will become associated with this model when they are added to it; this cannot be changed after a group's creation. 若要查看 PersonGroup 是使用哪个模型配置的,请结合设置为 truereturnRecognitionModel 参数使用 [PersonGroup - Get] API。To see what model a PersonGroup is configured with, use the [PersonGroup - Get] API with the returnRecognitionModel parameter set as true.

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

// Create an empty PersonGroup with "recognition_02" model
string personGroupId = "mypersongroupid";
await faceClient.PersonGroup.CreateAsync(personGroupId, "My Person Group Name", recognitionModel: "recognition_02");

在此代码中,创建了 ID 为 mypersongroupidPersonGroup,它设置为使用 recognition_02 模型提取人脸特征。In this code, a PersonGroup with ID mypersongroupid is created, and it is set up to use the recognition_02 model to extract face features.

相应地,需要指定在检测人脸以根据此 PersonGroup 进行比较(通过 人脸 - 检测 API)时要使用的模型。Correspondingly, you need to specify which model to use when detecting faces to compare against this PersonGroup (through the Face - Detect API). 使用的模型应始终为 PersonGroup 的配置相一致;否则,操作将会由于模型不兼容而失败。The model you use should always be consistent with the PersonGroup's configuration; otherwise, the operation will fail due to incompatible models.

无需在 Face - Identify API 中进行更改;只需在检测中指定模型版本。There is no change in the Face - Identify API; you only need to specify the model version in detection.

使用指定的模型查找类似的人脸Find similar faces with specified model

还可以指定一个识别模型来进行类似性搜索。You can also specify a recognition model for similarity search. 可以在使用 [FaceList - Create] API 或 [LargeFaceList - Create] 创建人脸列表时,使用 recognitionModel 来分配模型版本。You can assign the model version with recognitionModel when creating the face list with [FaceList - Create] API or [LargeFaceList - Create]. 如果不指定此参数,则默认使用 recognition_01 模型。If you do not specify this parameter, the recognition_01 model is used by default. 人脸列表始终使用创建它时所用的识别模型,并且将新的人脸添加到该列表时,它们将与此模型相关联;创建后无法对此进行更改。A face list will always use the recognition model it was created with, and new faces will become associated with this model when they are added to the list; you cannot change this after creation. 若要查看人脸列表是使用哪个模型配置的,请结合设置为 truereturnRecognitionModel 参数使用 [FaceList - Get] API。To see what model a face list is configured with, use the [FaceList - Get] API with the returnRecognitionModel parameter set as true.

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

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

此代码使用 recognition_04 模型来提取特征,创建名为 My face collection 的人脸列表。This code creates a face list called My face collection, using the recognition_04 model for feature extraction. 在此人脸列表中搜索与新检测到的人脸类似的人脸时,该人脸一定是使用 recognition_04 模型检测到的(人脸 - 检测 )。When you search this face list for similar faces to a new detected face, that face must have been detected (Face - Detect) using the recognition_04 model. 如上一部分所述,模型需要保持一致。As in the previous section, the model needs to be consistent.

无需在 Face - Find Similar API 中进行更改;只需在检测中指定模型版本。There is no change in the Face - Find Similar API; you only specify the model version in detection.

使用指定的模型验证人脸Verify faces with specified model

[Face - Verify] API 会检查两张人脸是否属于同一个人。The [Face - Verify] API checks whether two faces belong to the same person. 无需在 Verify API 中进行识别模型方面的更改,但只能比较使用同一模型检测到的人脸。There is no change in the Verify API with regard to recognition models, but you can only compare faces that were detected with the same model.

评估不同的模型Evaluate different models

如果要基于自己的数据比较不同识别模型的性能,则需要:If you'd like to compare the performances of different recognition models on your own data, you will need to:

  1. 分别使用 recognition_01、recognition_02、recognition_03 和 recognition_04 创建四个 PersonGroup 。Create four PersonGroups using recognition_01, recognition_02, recognition_03, and recognition_04 respectively.
  2. 使用图像数据来检测人脸,并将这些人脸注册到这四个 PersonGroup 中的 Person 。Use your image data to detect faces and register them to Person s within these four PersonGroup s.
  3. 使用 PersonGroup - Train API 训练 PersonGroup。Train your PersonGroups using the PersonGroup - Train API.
  4. 使用“人脸 - 识别”对全部四个 PersonGroup 都进行测试,然后比较结果。Test with Face - Identify on all four PersonGroup s and compare the results.

如果正常指定了置信度阈值(介于 0 与 1 之间的值,确定模型必须有多大的置信度来识别人脸),可能需要对不同的模型使用不同的阈值。If you normally specify a confidence threshold (a value between zero and one that determines how confident the model must be to identify a face), you may need to use different thresholds for different models. 一个模型的阈值不能与另一个模型共享,并且不一定生成相同的结果。A threshold for one model is not meant to be shared to another and will not necessarily produce the same results.

后续步骤Next steps

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

[Face - Verify]: https://dev.cognitive.azure.cn/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f3039523a(人脸 - 验证) [PersonGroup - 创建]: https://dev.cognitive.azure.cn/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395244 [PersonGroup - Create]: https://dev.cognitive.azure.cn/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395244 [PersonGroup - Get]: https://dev.cognitive.azure.cn/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395246 [PersonGroup Person - Add Face]: https://dev.cognitive.azure.cn/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f3039523b [PersonGroup - Train]: https://dev.cognitive.azure.cn/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395249 [LargePersonGroup - 创建]: https://dev.cognitive.azure.cn/docs/services/563879b61984550e40cbbe8d/operations/599acdee6ac60f11b48b5a9d [LargePersonGroup - Create]: https://dev.cognitive.azure.cn/docs/services/563879b61984550e40cbbe8d/operations/599acdee6ac60f11b48b5a9d [FaceList - Create]: https://dev.cognitive.azure.cn/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f3039524b [FaceList - Get]: https://dev.cognitive.azure.cn/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f3039524c [LargeFaceList - Create]: https://dev.cognitive.azure.cn/docs/services/563879b61984550e40cbbe8d/operations/5a157b68d2de3616c086f2cc