获取人脸检测数据Get face detection data

本指南演示如何使用人脸检测功能从给定的图像中提取性别、年龄或姿势等特性。This guide demonstrates how to use face detection to extract attributes like gender, age, or pose from a given image. 本指南中的代码片段是使用 Azure 认知服务人脸 API 客户端库以 C# 编写的。The code snippets in this guide are written in C# by using the Azure Cognitive Services Face API client library. 同样的功能通过 REST API 提供。The same functionality is available through the REST API.

本指南介绍以下操作:This guide shows you how to:

  • 获取图像中人脸的位置和维度。Get the locations and dimensions of faces in an image.
  • 获取图像中各个人脸特征点(例如瞳孔、鼻子、嘴巴)的位置。Get the locations of various face landmarks, such as pupils, nose, and mouth, in an image.
  • 猜测性别、年龄、情绪,以及检测到的人脸的其他特性。Guess the gender, age, emotion, and other attributes of a detected face.

设置Setup

本指南假设你已使用人脸订阅密钥和终结点 URL 构造了名为 faceClientFaceClient 对象。This guide assumes that you already constructed a FaceClient object, named faceClient, with a Face subscription key and endpoint URL. 在此处,可以通过调用 DetectWithUrlAsync(本指南中使用)或 DetectWithStreamAsync 来使用人脸检测功能。From here, you can use the face detection feature by calling either DetectWithUrlAsync, which is used in this guide, or DetectWithStreamAsync. 有关如何设置此功能的说明,请参阅 C# 人脸检测快速入门For instructions on how to set up this feature, see the Detect faces quickstart for C#.

本指南重点介绍有关检测调用的具体信息,例如,可以传递哪些参数,以及可对返回的数据执行哪些操作。This guide focuses on the specifics of the Detect call, such as what arguments you can pass and what you can do with the returned data. 建议仅查询所需功能。We recommend that you query for only the features you need. 每项操作都需要额外的时间来完成。Each operation takes additional time to complete.

获取基本人脸数据Get basic face data

若要查找人脸并获取其在图像中的位置,请在将 returnFaceId 参数设置为 true 的情况下调用该方法。To find faces and get their locations in an image, call the method with the returnFaceId parameter set to true. 此设置为默认设置。This setting is the default.

IList<DetectedFace> faces = await faceClient.Face.DetectWithUrlAsync(imageUrl, true, false, null);

可在返回的 DetectedFace 对象中查询其唯一 ID,矩形提供了人脸的像素坐标。You can query the returned DetectedFace objects for their unique IDs and a rectangle that gives the pixel coordinates of the face.

foreach (var face in faces)
{
    string id = face.FaceId.ToString();
    FaceRectangle rect = face.FaceRectangle;
}

有关如何分析人脸位置和维度的信息,请参阅 FaceRectangleFor information on how to parse the location and dimensions of the face, see FaceRectangle. 通常情况下,此矩形包含眼睛、眉毛、鼻子和嘴巴,Usually, this rectangle contains the eyes, eyebrows, nose, and mouth. 不一定包括头顶、耳朵和下巴。The top of head, ears, and chin aren't necessarily included. 若要使用人脸矩形来裁剪整个头部或获取中景肖像(也许是身份证类型的图像),可朝每个方向拉伸矩形。To use the face rectangle to crop a complete head or get a mid-shot portrait, perhaps for a photo ID-type image, you can expand the rectangle in each direction.

获取人脸特征点Get face landmarks

人脸特征点是人脸上的一组易于查找的点,例如瞳孔或鼻尖。Face landmarks are a set of easy-to-find points on a face, such as the pupils or the tip of the nose. 若要获取人脸特征点数据,请将 returnFaceLandmarks 参数设置为 trueTo get face landmark data, set the returnFaceLandmarks parameter to true.

IList<DetectedFace> faces = await faceClient.Face.DetectWithUrlAsync(imageUrl, true, true, null);

以下代码演示如何检索鼻子和瞳孔的位置:The following code demonstrates how you might retrieve the locations of the nose and pupils:

foreach (var face in faces)
{
    var landmarks = face.FaceLandmarks;

    double noseX = landmarks.NoseTip.X;
    double noseY = landmarks.NoseTip.Y;

    double leftPupilX = landmarks.PupilLeft.X;
    double leftPupilY = landmarks.PupilLeft.Y;

    double rightPupilX = landmarks.PupilRight.X;
    double rightPupilY = landmarks.PupilRight.Y;
}

人脸特征点数据还可用于准确计算人脸的方向。You also can use face landmarks data to accurately calculate the direction of the face. 例如,可以将人脸的旋转角定义为从嘴巴中心到眼睛中心的矢量。For example, you can define the rotation of the face as a vector from the center of the mouth to the center of the eyes. 以下代码计算此矢量:The following code calculates this vector:

var upperLipBottom = landmarks.UpperLipBottom;
var underLipTop = landmarks.UnderLipTop;

var centerOfMouth = new Point(
    (upperLipBottom.X + underLipTop.X) / 2,
    (upperLipBottom.Y + underLipTop.Y) / 2);

var eyeLeftInner = landmarks.EyeLeftInner;
var eyeRightInner = landmarks.EyeRightInner;

var centerOfTwoEyes = new Point(
    (eyeLeftInner.X + eyeRightInner.X) / 2,
    (eyeLeftInner.Y + eyeRightInner.Y) / 2);

Vector faceDirection = new Vector(
    centerOfTwoEyes.X - centerOfMouth.X,
    centerOfTwoEyes.Y - centerOfMouth.Y);

知道人脸的方向后,可以旋转矩形人脸框,使人脸更适当地对齐。When you know the direction of the face, you can rotate the rectangular face frame to align it more properly. 若要裁剪图像中的人脸,可以编程方式旋转图像,使人脸始终朝上。To crop faces in an image, you can programmatically rotate the image so that the faces always appear upright.

获取人脸特性Get face attributes

除了人脸矩形和特征点以外,人脸检测 API 还可以分析人脸的几个概念特性。Besides face rectangles and landmarks, the face detection API can analyze several conceptual attributes of a face. 如需完整列表,请参阅人脸属性概念部分。For a full list, see the Face attributes conceptual section.

若要分析人脸特性,请将 returnFaceAttributes 参数设置为 FaceAttributeType Enum 值的列表。To analyze face attributes, set the returnFaceAttributes parameter to a list of FaceAttributeType Enum values.

var requiredFaceAttributes = new FaceAttributeType[] {
    FaceAttributeType.Age,
    FaceAttributeType.Gender,
    FaceAttributeType.Smile,
    FaceAttributeType.FacialHair,
    FaceAttributeType.HeadPose,
    FaceAttributeType.Glasses,
    FaceAttributeType.Emotion
};
var faces = await faceClient.DetectWithUrlAsync(imageUrl, true, false, requiredFaceAttributes);

然后,获取对返回的数据的引用,并根据需要执行进一步的操作。Then, get references to the returned data and do more operations according to your needs.

foreach (var face in faces)
{
    var attributes = face.FaceAttributes;
    var age = attributes.Age;
    var gender = attributes.Gender;
    var smile = attributes.Smile;
    var facialHair = attributes.FacialHair;
    var headPose = attributes.HeadPose;
    var glasses = attributes.Glasses;
    var emotion = attributes.Emotion;
}

若要详细了解每个属性,请参阅人脸检测和属性概念指南。To learn more about each of the attributes, see the Face detection and attributes conceptual guide.

后续步骤Next steps

本指南介绍了如何使用人脸检测的各项功能。In this guide, you learned how to use the various functionalities of face detection. 接下来,请按深度教程的说明操作,将这些功能集成到应用中。Next, integrate these features into your app by following an in-depth tutorial.