2017-04-17 178 views
0

這一次打我嘗試一切如BASE64將其轉換成二進制不斷得到下面InvalidImageSize微軟面對API 1.0

{"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() 

不知道我是否有意在上面的代碼正確的上面的代碼,所以請檢查。

我會感謝你的支持表示感謝

回答

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()