python - Azure Detect faces API,如何将URL图片更改为本 map 片?

标签 python azure face-detection azure-cognitive-services

我使用 azure Detect faces API 但我发现image_url是一张网络图片。 现在我想使用本 map 片,如何将URL图片更改为本 map 片?

代码:

import requests
import matplotlib.pyplot as plt
from PIL import Image
from matplotlib import patches
from io import BytesIO
subscription_key = "XXX"
assert subscription_key
face_api_url = 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect'
image_url = 'https://how-old.net/Images/faces2/main007.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})
faces = response.json()
print(faces)
response = requests.get(image_url)
image = Image.open(BytesIO(response.content))
plt.figure(figsize=(8, 8))
ax = plt.imshow(image, alpha=0.6)
for face in faces:
    fr = face["faceRectangle"]
    fa = face["faceAttributes"]
    origin = (fr["left"], fr["top"])
    p = patches.Rectangle(
        origin, fr["width"], fr["height"], fill=False, linewidth=2, color='b')
    ax.axes.add_patch(p)
    plt.text(origin[0], origin[1], "%s, %d"%(fa["gender"].capitalize(), fa["age"]),
             fontsize=20, weight="bold", va="bottom")
plt.axis("off")
plt.savefig('D:/test.jpg')
plt.show()

最佳答案

对于本地镜像,Content-Type 应为 application/octet-stream multipart/form-data 并且 JSON 应该为空

headers = {
    'Content-type': 'application/octet-stream',
}
body = "" 
#load image
filename = 'C:/testPicutre.JPG'
f = open(filename, "rb")
body = f.read()
f.close()
conn = httplib.HTTPSConnection('api.projectoxford.ai')
conn.request("POST", "/face/v0/detections?%s" % params, body, headers)
response = conn.getresponse("")
data = response.read()
print(data)
conn.close()

关于python - Azure Detect faces API,如何将URL图片更改为本 map 片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50672566/

相关文章:

image - 将素描(绘图)人脸照片与数码彩色照片相匹配

Python selenium打印框架源码

python sqlite3,我必须多久提交一次?

azure - 谁管理 AKS 群集中的节点?

azure - 从ARM模板获取API管理的公共(public)IP地址

objective-c - 将Vision人脸检测VNFaceLandmarkRegion2D点转换为帧坐标以进行缩放

python - 用于检查 Python 属性的类似 get() 的方法

python - Pandas 迭代中每一行的并行处理

c# - 将数百万个项目从一个存储帐户移动到另一个存储帐户

android - 间隔[40 cm; 80cm]