Azure 用python在请求体中传递图像

Azure 用python在请求体中传递图像,azure,rest,face-detection,azure-rest-api,Azure,Rest,Face Detection,Azure Rest Api,目前我正在尝试Azure提供的人脸检测API。我目前正在使用Rest调用检测图像中的人脸。 更多详细信息可在此处找到: 下面是用于检测图像中人脸的示例代码: import json, os, requests subscription_key = os.environ['FACE_SUBSCRIPTION_KEY'] assert subscription_key face_api_url = os.environ['FACE_ENDPOINT'] + '/face/v1.0/detect'

目前我正在尝试Azure提供的人脸检测API。我目前正在使用Rest调用检测图像中的人脸。 更多详细信息可在此处找到:

下面是用于检测图像中人脸的示例代码:

import json, os, requests
subscription_key = os.environ['FACE_SUBSCRIPTION_KEY']
assert subscription_key

face_api_url = os.environ['FACE_ENDPOINT'] + '/face/v1.0/detect'

image_url = 'https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/faces.jpg'

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

params = {
'detectionModel': 'detection_02',
'returnFaceId': 'true'
}

response = requests.post(face_api_url, params=params,
                     headers=headers, json={"url": image_url})
print(json.dumps(response.json()))
现在我想要的不是从github或任何公共URL传递图像,而是从本地机器传递图像。例如,我的本地机器上有一个图像,位于“/home/ubuntu/index.png”


如果您能为我提供帮助,我们将不胜感激。

将POST请求的内容类型标题设置为“应用程序/八位字节流”,并将图像添加到HTTP正文中:

导入操作系统
导入请求
导入json
subscription\u key=os.environ['FACE\u subscription\u key']
face_api_url=os.environ['face_ENDPOINT']+'/face/v1.0/detect'
headers={'Content-Type':'application/octet-stream',
“Ocp Apim订阅密钥”:订阅密钥}
使用open('/home/ubuntu/index.png',rb')作为img:
res=requests.post(face\u api\u url,headers=headers,data=img)
res.为_状态提高_()
faces=res.json()
打印(面)

API参考资料:

我想有两种方法可以检测人脸:1。使用URL 2进行检测。使用流检测更多详细信息: