快速入门:使用人脸 REST API 和 cURL 检测图像中的人脸Quickstart: Detect faces in an image using the Face REST API and cURL

在本快速入门中,请使用 Azure 人脸 REST API 和 cURL 检测图像中的人脸。In this quickstart, you will use the Azure Face REST API with cURL to detect human faces in an image.

如果没有 Azure 订阅,请在开始前创建一个试用帐户If you don't have an Azure subscription, create a Trial Account before you begin.

先决条件Prerequisites

编写命令Write the command

将使用如下所示的命令来调用人脸 API 并获取图像中的人脸属性数据。You'll use a command like the following to call the Face API and get face attribute data from an image. 首先,将代码复制到文本编辑器中—在运行它之前,需对命令的某些部分进行更改。First, copy the code into a text editor—you'll need to make changes to certain parts of the command before you can run it.

curl -H "Ocp-Apim-Subscription-Key: <Subscription Key>" "https://api.cognitive.azure.cn/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false&returnFaceAttributes=age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise" -H "Content-Type: application/json" --data-ascii "{\"url\":\"https://upload.wikimedia.org/wikipedia/commons/c/c3/RH_Louise_Lillian_Gish.jpg\"}"

订阅密钥Subscription key

<Subscription Key> 替换为有效的人脸订阅密钥。Replace <Subscription Key> with your valid Face subscription key.

人脸终结点 URLFace endpoint URL

URL https://api.cognitive.azure.cn/face/v1.0/detect 指示要查询的 Azure 人脸终结点。The URL https://api.cognitive.azure.cn/face/v1.0/detect indicates the Azure Face endpoint to query. 可能需更改此 URL 的第一部分,使之与订阅密钥的相应区域匹配。You may need to change the first part of this URL to match the region that corresponds to your subscription key. 有关所有区域终结点的列表,请参阅人脸 API 文档See the Face API docs for a list of all region endpoints.

URL 查询字符串URL query string

人脸终结点 URL 的查询字符串指定要检索的人脸属性。The query string of the Face endpoint URL specifies which face attributes to retrieve. 可能需要更改此字符串,具体取决于预期用途。You may wish to change this string depending on your intended use.

?returnFaceId=true&returnFaceLandmarks=false&returnFaceAttributes=age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise

图像源 URLImage source URL

源 URL 指示将要用作输入的图像。The source URL indicates the image to use as input. 可以更改此字段,使之指向要分析的任何图像。You can change this to point to any image you want to analyze.

https://upload.wikimedia.org/wikipedia/commons/c/c3/RH_Louise_Lillian_Gish.jpg

备注

如果创建的人脸 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.

运行命令Run the command

进行更改以后,请打开命令提示符并输入新的命令。Once you've made your changes, open a command prompt and enter the new command. 应该会在控制台窗口中看到显示为 JSON 数据的人脸信息。You should see the face information displayed as JSON data in the console window. 例如:For example:

[
  {
    "faceId": "49d55c17-e018-4a42-ba7b-8cbbdfae7c6f",
    "faceRectangle": {
      "top": 131,
      "left": 177,
      "width": 162,
      "height": 162
    },
    "faceAttributes": {
      "smile": 0,
      "headPose": {
        "pitch": 0,
        "roll": 0.1,
        "yaw": -32.9
      },
      "gender": "female",
      "age": 22.9,
      "facialHair": {
        "moustache": 0,
        "beard": 0,
        "sideburns": 0
      },
      "glasses": "NoGlasses",
      "emotion": {
        "anger": 0,
        "contempt": 0,
        "disgust": 0,
        "fear": 0,
        "happiness": 0,
        "neutral": 0.986,
        "sadness": 0.009,
        "surprise": 0.005
      },
      "blur": {
        "blurLevel": "low",
        "value": 0.06
      },
      "exposure": {
        "exposureLevel": "goodExposure",
        "value": 0.67
      },
      "noise": {
        "noiseLevel": "low",
        "value": 0
      },
      "makeup": {
        "eyeMakeup": true,
        "lipMakeup": true
      },
      "accessories": [],
      "occlusion": {
        "foreheadOccluded": false,
        "eyeOccluded": false,
        "mouthOccluded": false
      },
      "hair": {
        "bald": 0,
        "invisible": false,
        "hairColor": [
          {
            "color": "brown",
            "confidence": 1
          },
          {
            "color": "black",
            "confidence": 0.87
          },
          {
            "color": "other",
            "confidence": 0.51
          },
          {
            "color": "blond",
            "confidence": 0.08
          },
          {
            "color": "red",
            "confidence": 0.08
          },
          {
            "color": "gray",
            "confidence": 0.02
          }
        ]
      }
    }
  }
]

后续步骤Next steps

在本快速入门中,你编写了一个 cURL 命令,该命令调用 Azure 人脸 API,以便检测图像中的人脸并返回其属性。In this quickstart, you wrote a cURL command that calls the Azure Face API to detect faces in an image and return their attributes. 接下来,请浏览人脸 API 参考文档,以便进行详细的了解。Next, explore the Face API reference documentation to learn more.