这个打败了我尝试将base64转换为二进制的所有内容,只是在下面继续得到此错误
{"error":{"code":"InvalidImageSize","message":"Image size
is too small or too big."}}
这是我的代码是用python编写的:
import httplib, urllib, base64
import json
import sys
import base64
# require for authentication
key = "YourKey"
# leave as one header so ther three steps can access the same format
# application/octet-stream
headers = {
# Request headers
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': key,
}
# params for detection return FaceId
params = urllib.urlencode({
# Request parameters
'returnFaceId': 'true',
})
with open("C://Python-Windows//random_test//unknowfaces//Adam.jpg", "rb") as
imageFile:
f = imageFile.read()
b = bytearray(f)
print f
#image_64_encode = base64.encodestring(image_read)
"""
body = {
"url":"https://lh6.googleusercontent.com/-
dKxIImkT0to/WApnOYQSIFI/AAAAAAAAAAA/H9IsZ2xGxiE/photo.jpg"
}
"""
#below is for the binary
body = {
f[0]
}
# conn to be use at all three steps
conn = httplib.HTTPSConnection('westus.api.cognitive.microsoft.com')
conn.request("POST", "/face/v1.0/detect?%s" % params, str(body), headers)
response = conn.getresponse()
data = response.read()
print(data)
#Parse json data to print just faceId
somedata = json.loads(data)
faceid = somedata[0]['faceId']
print somedata[0]['faceId']
print " :is what face id has produce ==> " + faceid
conn.close()
不确定我是否已在此胎面上正确使用上述代码,请检查。
感谢您的支持,谢谢
答案 0 :(得分:1)
以下是更改代码的方法:
with open("your-image.jpg", "rb") as imageFile:
image = imageFile.read()
conn = httplib.HTTPSConnection('westus.api.cognitive.microsoft.com')
conn.request("POST", "/face/v1.0/detect?%s" % params, headers=headers, body=image)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()