我正在一个小项目中使用 Microsoft Face API,我试图检测本地系统中 .jpg 文件中的人脸(例如,存储在目录 D:\Image\abc.jpg)
示例代码,如其documentation所示,对于来自在线资源的 url 效果很好,但它似乎不适用于本地路径地址。我尝试执行以下操作:
dict = "{ 'url' : 'file:///D:/Image/abc.jpg'}"
但是好像不行。似乎有一个Java方法(使用 FileEntity )。我想知道是否有Python的方法。我是编码新手。我真的希望有人能帮助我解决这个问题。我正在使用Python3。
最佳答案
Microsoft 无法从本地文件系统读取文件,因为它们不像从 URL 访问图像那样在 Internet 上公开。因此,您需要将文件发送到 Microsoft Face API 以便他们访问。
import requests
headers = {'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': <token>}
url = 'https://api.projectoxford.ai/face/v1.0/detect'
# Gets the binary file data so we can send it to MCS
data = open('file:///D:/Image/abc.jpg', 'rb')
requests.post(url, headers=headers, data=data)
关于python - 通过Python添加Microsoft Face API的本地路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40714481/