Я вижу, что вы хотите отправить локальное изображение для обнаружения лица через REST API.
На самом деле, вам просто нужно изменить код из трех мест, чтобы он работал, как показано ниже.
- Добавить заголовок
{"Content-Type": "application/octet-stream"}
- Чтение байтов из файла, например
data = open('<your filename>', 'rb').read()
- Используйте
data=data
для двоичных данных вместо json={...}
для URL-адреса изображения в методе requests.post
Вот мой пример изображения person_of_interest-5.jpg
, загруженный с Bing Image.
И мой код, как показано ниже.
import requests
subscription_key = '<your key>'
face_api_url = 'https://southeastasia.api.cognitive.microsoft.com/face/v1.0/detect'
headers = {
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': subscription_key,
}
data = open('person_of_interest-5.jpg', "rb").read()
params = {
'returnFaceId': 'true',
'returnFaceLandmarks': 'false',
'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',
}
response = requests.post(face_api_url, params=params, headers=headers, data=data)
print(response.text)
Тогда это работает, и его результат будет ниже.
[{"faceId":"e07cc105-143e-4ee3-9d84-a2830c8ddd07","faceRectangle":{"top":1501,"left":2582,"width":471,"height":471},"faceAttributes":{"smile":0.391,"headPose":{"pitch":-4.3,"roll":41.8,"yaw":20.8},"gender":"male","age":40.0,"facialHair":{"moustache":0.1,"beard":0.1,"sideburns":0.1},"glasses":"NoGlasses","emotion":{"anger":0.016,"contempt":0.008,"disgust":0.01,"fear":0.0,"happiness":0.391,"neutral":0.569,"sadness":0.003,"surprise":0.003},"blur":{"blurLevel":"medium","value":0.36},"exposure":{"exposureLevel":"goodExposure","value":0.53},"noise":{"noiseLevel":"medium","value":0.52},"makeup":{"eyeMakeup":false,"lipMakeup":false},"accessories":[],"occlusion":{"foreheadOccluded":false,"eyeOccluded":false,"mouthOccluded":false},"hair":{"bald":0.18,"invisible":false,"hairColor":[{"color":"gray","confidence":0.96},{"color":"black","confidence":0.95},{"color":"other","confidence":0.6},{"color":"brown","confidence":0.34},{"color":"blond","confidence":0.12},{"color":"red","confidence":0.02}]}}},{"faceId":"a54d8823-0518-4cb6-942c-4d5357316e69","faceRectangle":{"top":451,"left":1168,"width":427,"height":427},"faceAttributes":{"smile":0.0,"headPose":{"pitch":0.9,"roll":0.0,"yaw":1.0},"gender":"male","age":43.0,"facialHair":{"moustache":0.1,"beard":0.1,"sideburns":0.1},"glasses":"ReadingGlasses","emotion":{"anger":0.0,"contempt":0.0,"disgust":0.0,"fear":0.0,"happiness":0.0,"neutral":0.959,"sadness":0.0,"surprise":0.04},"blur":{"blurLevel":"medium","value":0.66},"exposure":{"exposureLevel":"goodExposure","value":0.66},"noise":{"noiseLevel":"high","value":1.0},"makeup":{"eyeMakeup":false,"lipMakeup":false},"accessories":[{"type":"glasses","confidence":1.0}],"occlusion":{"foreheadOccluded":false,"eyeOccluded":false,"mouthOccluded":false},"hair":{"bald":0.02,"invisible":false,"hairColor":[{"color":"brown","confidence":0.99},{"color":"black","confidence":0.94},{"color":"gray","confidence":0.52},{"color":"other","confidence":0.1},{"color":"red","confidence":0.09},{"color":"blond","confidence":0.03}]}}}]