Search code examples
python-3.xazurehttpdata-scienceface-recognition

Json response via HTTPRequest differ then on website


I'm using Microsoft Azure to detect Faces attributes. I uploaded the first image from this page: https://photographylife.com/nikon-24mm-f1-4-image-samples.

When I put it on the API website () I get the complete Json (with faceId, faceRectangle , faceAttributes), however using HTTP request the Json is partial (no faceAttributes).

headers  = {'Ocp-Apim-Subscription-Key': subscription_key, "Content-Type": "application/octet-stream" }
response = requests.post(face_api_url, headers=headers, data=image_data)
response.raise_for_status()
json_analysis = response.json()   
print(json_analysis)

Here, json_analysis has no field FaceAttributes:

[{'faceId': '9bcf9353-1880-4a46-be00-58a992d8e7f8', 'faceRectangle': {'top': 47, 'left': 99, 'width': 65, 'height': 65}}]

What might be the reason?


Solution

  • Without special query parameter, the return only contains faceId and faceRectangle. You need to set returnFaceAttributes to return the designated attributes

    https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceAttributes={attribute}

    Attribute is string type and can be separated with comma, e.g. returnFaceAttributes=age,hair. There is no option in the URL query string to select All attributes.

    https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false&returnFaceAttributes=age,hair

    Sample full request is as follows:

    POST https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false&returnFaceAttributes=age,hair HTTP/1.1
    Host: westcentralus.api.cognitive.microsoft.com
    Content-Type: application/json
    Ocp-Apim-Subscription-Key: ••••••••••••••••••••••••••••••••
    
    {
        "url":"https://cdn.photographylife.com/wp-content/uploads/2010/04/20100419-Nikon-24mm-Test-242-960x638.jpg"
    }
    

    ..and response

    [{
      "faceId": "68629eb6-f483-4c1c-b74f-078ad521c2d7",
      "faceRectangle": {
        "top": 257,
        "left": 349,
        "width": 183,
        "height": 183
      },
      "faceAttributes": {
        "age": 6.0,
        "hair": {
          "bald": 0.01,
          "invisible": false,
          "hairColor": [{
            "color": "brown",
            "confidence": 0.99
          }, {
            "color": "black",
            "confidence": 0.94
          }, {
            "color": "other",
            "confidence": 0.19
          }, {
            "color": "red",
            "confidence": 0.17
          }, {
            "color": "blond",
            "confidence": 0.13
          }, {
            "color": "gray",
            "confidence": 0.06
          }]
        }
      }
    }]
    

    You need to make sure the host you call (westcentralus.api.cognitive.microsoft.com) must use the API key of the Face API resource in the same location. In your case, since you call to the host in West Central US, your Face API resource must be deployed in this location. Otherwise you will receive 401.

    enter image description here