无法使用python

时间:2019-06-01 09:47:52

标签: python python-requests face-recognition azure-api-apps

我想通过azure face api发送图像,但是将图像文件转换为base64很简单,并且无法发送请求。

这是代码请求azure face api,正在运行python但代码显示错误

import requests
import json
import base64

subscription_key = 'my_key'
assert subscription_key

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


headers = { 'Ocp-Apim-Subscription-Key': subscription_key }

#data = 'test.jpg'

with open("test.jpg", "rb") as image_file:
    encoded_string = base64.b64encode(image_file.read())
    print(encoded_string)

params = {
    'returnFaceId': 'true',
    'returnFaceLandmarks': 'false',
    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',
}

#body={
#    "url": 'data:image/jpeg;base64' + str(encoded_string)
#}
#data={
#    "data": encoded_string,
#    "contentType": "application/octet-stream",
#}

response = requests.post(face_api_url, params=params, headers=headers, json={"data": "image/JPEG;base64,/"+str(encoded_string)})
print(json.dumps(response.json()))

这是错误“ {” error“:{” code“:” InvalidURL“,” message“:”无效的图片URL。“}}”

这是正确的[{“ faceId”:“ f9fd11a4-8855-4304-af98-f200afcae843”,“ faceRectangle”:{“ top”:621,“ left”:616,“ width”:195,“ height” :195},“ faceAttributes”:{“ smile”:0.0,“ headPose”:{“ pitch”:-11.4,“ roll”:7.7,“ yaw”:5.3},“ gender”:“ male”,“年龄“:29.0,“ facialHair”:{“小胡子”:0.4,“胡须”:0.4,“ side角”:0.1},“眼镜”:“ NoGlasses”,“情感”:{“愤怒”:0.0,“鄙视” :0.0,“令人反感”:0.0,“恐惧”:0.0,“幸福”:0.0,“中立”:0.999,“悲伤”:0.001,“惊喜”:0.0},“ blur”:{“ blurLevel”:“高”,“值”:0.89},“曝光”:{“ exposureLevel”:“ goodExposure”,“值”:0.51},“噪声”:{“ noiseLevel”:“中”,“值”:0.59}, “ makeup”:{“ eyeMakeup”:true,“ lipMakeup”:false},“ accessories”:[],“ occlusion”:{“ foreheadOccluded”:false,“ eyeOccluded”:false,“ mouthOccluded”:false},“头发”:{“秃头”:0.04,“不可见”:假,“ hairColor”:[{“颜色”:“黑色”,“信心”:0.98},{“颜色”:“棕色”,“信心”: 0.87},{“颜色”:“灰色”,“信心”:0.85},{“颜色”:“其他”,“信心”:0.25},{“颜色”:“金发”,“信心”:0.07},{“颜色”:“红色”,“信心”:0.02}]}}},{“ faceId” :“ 6c83b2c8-2cdc-43ea-994c-840932601b1d”,“ faceRectangle”:{“ top”:693,“ left”:1503,“ width”:180,“ height”:180},“ faceAttributes”:{“微笑“:0.003,” headPose“:{” pitch“:-9.0,” roll“:-0.5,” yaw“:-1.5},” gender“:” female“,” age“:58.0,” facialHair“:{ “小胡子”:0.0,“胡须”:0.0,“ side角”:0.0},“眼镜”:“ NoGlasses”,“情感”:{“愤怒”:0.0,“鄙视”:0.001,“厌恶”:0.0, “恐惧”:0.0,“幸福”:0.003,“中立”:0.984,“悲伤”:0.011,“惊喜”:0.0},“模糊”:{“模糊等级”:“高”,“值”:0.83} ,“ exposure”:{“ exposureLevel”:“ goodExposure”,“ value”:0.41},“ noise”:{“ noiseLevel”:“ high”,“ value”:0.76},“ makeup”:{“ eyeMakeup”: false,“ lipMakeup”:false},“ accessories”:[],“ occlusion”:{“ foreheadOccluded”:false,“ eyeOccluded”:false,“ mouthOccluded”:false},“ hair”:{“ bald”:0.06 ,“不可见”:false,“ hairColor”:[{“ color”:“ black”,“ confidence” “:0.99},{”颜色“:”灰色“,”信心“:0.89},{”颜色“:”其他“,”信心“:0.64},{”颜色“:”棕色“,”信心“: 0.34},{“颜色”:“白色”,“置信度”:0.07},{“颜色”:“红色”,“置信度:0.03}]}}}}]]

2 个答案:

答案 0 :(得分:0)

假设您的图片网址不正确。正如我在下面的代码中使用的那样,它为我工作。

import requests
import json

subscription_key = None
assert subscription_key

face_api_url = 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect'

image_url = 'https://upload.wikimedia.org/wikipedia/commons/3/37/Dagestani_man_and_woman.jpg'

headers = { 'Ocp-Apim-Subscription-Key': subscription_key }

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, json={"url": image_url})
print(json.dumps(response.json()))

尽管我建议您使用Face的python SDK,也可以从此处安装它。

pip install cognitive_face

通过下面的操作,面部检测非常容易

import cognitive_face as CF

KEY = '<Subscription Key>'  # Replace with a valid subscription key (keeping the quotes in place).
CF.Key.set(KEY)

BASE_URL = 'https://westus.api.cognitive.microsoft.com/face/v1.0/'  # Replace with your regional Base URL
CF.BaseUrl.set(BASE_URL)

# You can use this example JPG or replace the URL below with your own URL to a JPEG image.
img_url = 'https://raw.githubusercontent.com/Microsoft/Cognitive-Face-Windows/master/Data/detection1.jpg'
faces = CF.face.detect(img_url)
print(faces)

这是样本回复

[{'faceId': '26d8face-9714-4f3e-bfa1-f19a7a7aa240', 'faceRectangle': {'top': 124, 'left': 459, 'width': 227, 'height': 227}}]

希望有帮助。

答案 1 :(得分:0)

我看到您想发送本地图像以通过REST API进行人脸检测。

实际上,您只需更改三个位置的代码即可使其正常工作,如下所示。

  1. 添加标题{"Content-Type": "application/octet-stream"}
  2. 从文件中读取字节,例如data = open('<your filename>', 'rb').read()
  3. data=data方法中,将json={...}用于二进制数据,而不是requests.post用于图像URL

这是我从Bing Image下载的示例图片person_of_interest-5.jpg

enter image description here

我的代码如下。

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}]}}}]