Microsoft Face API验证(面对面)是否向下?总是说错误的请求,文档控制台显示错误

时间:2018-10-25 16:11:21

标签: python azure microsoft-cognitive azure-cognitive-services

这是我一直在尝试的:

subscription_key = "***"
assert subscription_key

face_api_url = 'https://southeastasia.api.cognitive.microsoft.com/face/v1.0/verify'


headers = {'Ocp-Apim-Subscription-Key': subscription_key,'Content-Type':'application/json'}
params = {
    "faceId1": "a1cadf80-d780-4b6a-8cef-717548a07e51",
    "faceId2": "05113848-2c22-4116-8a30-5cde938eec61"
}


import requests
from pprint import pprint
response  = requests.post(face_api_url, headers=headers, params=params)
faces = response.json()
pprint(faces)

我总是得到这个输出

{'error': {'code': 'BadArgument', 'message': 'Request body is invalid.'}}

此外,我已经尝试了API测试控制台,但它总是导致面对面的错误(没有尝试其他错误) 这是文档的链接,您可以在其中获得API测试控制台的链接。 https://southeastasia.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f3039523a

1 个答案:

答案 0 :(得分:0)

基于Verify API,我们可以知道面部应该在 body 部分中,而不是在 params

subscription_key = "xxxx"
assert subscription_key
import json
face_api_url = 'https://westus.api.cognitive.microsoft.com/face/v1.0/verify'
headers = {'Ocp-Apim-Subscription-Key': subscription_key,'Content-Type':'application/json'}
faces = {
    "faceId1": "xxxxxxxx",
    "faceId2": "xxxxxx"
}

body = json.dumps(faces)
import requests
from pprint import pprint
response  = requests.post(face_api_url, headers=headers,data=body)
result = response.json()
pprint(result)

测试结果:

enter image description here

我们还可以使用python SDK轻松做到这一点

import cognitive_face as CF
KEY = 'xxxxxx'  # Replace with a valid subscription key (keeping the quotes in place).
CF.Key.set(KEY)

BASE_URL = 'https://{location}.api.cognitive.microsoft.com/face/v1.0'  # Replace with your regional Base URL
CF.BaseUrl.set(BASE_URL)
result = CF.face.verify("faceId1","faceId2")
print(result)