Json 通过 HTTP 请求的响应与网站上的不同
Json response via HTTPRequest differ then on website
我正在使用 Microsoft Azure 检测面孔属性。
我从这个页面上传了第一张图片:
https://photographylife.com/nikon-24mm-f1-4-image-samples。
当我把它放在 API 网站上时 () 我得到了完整的 Json(带有 faceId、faceRectangle、faceAttributes),但是使用 HTTP 请求 Json 是部分的(无面部属性)。
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)
这里,json_analysis没有字段FaceAttributes:
[{'faceId': '9bcf9353-1880-4a46-be00-58a992d8e7f8', 'faceRectangle': {'top': 47, 'left': 99, 'width': 65, 'height': 65}}]
可能是什么原因?
没有特殊的查询参数,return只包含faceId
和faceRectangle
。你需要设置returnFaceAttributes
到return指定的属性
https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceAttributes={attribute}
属性为字符串类型,可以用逗号分隔,例如returnFaceAttributes=age,hair
。在URL查询字符串到select所有属性中没有选项。
https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false&returnFaceAttributes=age,hair
完整请求示例如下:
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"
}
..和回应
[{
"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
}]
}
}
}]
您需要确保您调用的主机(westcentralus.api.cognitive.microsoft.com)必须使用相同位置的 Face API 资源的 API 密钥。在您的情况下,由于您呼叫美国中西部的主机,因此您的 Face API 资源必须部署在此位置。否则你会收到 401。
我正在使用 Microsoft Azure 检测面孔属性。 我从这个页面上传了第一张图片: https://photographylife.com/nikon-24mm-f1-4-image-samples。
当我把它放在 API 网站上时 () 我得到了完整的 Json(带有 faceId、faceRectangle、faceAttributes),但是使用 HTTP 请求 Json 是部分的(无面部属性)。
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)
这里,json_analysis没有字段FaceAttributes:
[{'faceId': '9bcf9353-1880-4a46-be00-58a992d8e7f8', 'faceRectangle': {'top': 47, 'left': 99, 'width': 65, 'height': 65}}]
可能是什么原因?
没有特殊的查询参数,return只包含faceId
和faceRectangle
。你需要设置returnFaceAttributes
到return指定的属性
https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceAttributes={attribute}
属性为字符串类型,可以用逗号分隔,例如returnFaceAttributes=age,hair
。在URL查询字符串到select所有属性中没有选项。
https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false&returnFaceAttributes=age,hair
完整请求示例如下:
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"
}
..和回应
[{
"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
}]
}
}
}]
您需要确保您调用的主机(westcentralus.api.cognitive.microsoft.com)必须使用相同位置的 Face API 资源的 API 密钥。在您的情况下,由于您呼叫美国中西部的主机,因此您的 Face API 资源必须部署在此位置。否则你会收到 401。