1

我試圖發送一個二進制圖像文件來測試Microsoft Face API。使用POSTMAN完美地工作,我得到了預期的faceId。不過,我嘗試轉型,爲Python代碼和它目前給我這個錯誤:使用Microsoft Face API的Python POST請求錯誤「不受支持的圖像格式」

{"error": {"code": "InvalidImage", "message": "Decoding error, image format unsupported."}} 

我讀這SO post,但它並不能幫助。這是我發送請求的代碼。我試圖模仿POSTMAN正在做的事情,比如標題爲application/octet-stream,但它不起作用。有任何想法嗎?

url = "https://api.projectoxford.ai/face/v1.0/detect" 

headers = { 
    'ocp-apim-subscription-key': "<key>", 
    'content-type': "application/octet-stream", 
    'cache-control': "no-cache", 
} 

data = open('IMG_0670.jpg', 'rb') 
files = {'IMG_0670.jpg': ('IMG_0670.jpg', data, 'application/octet-stream')} 

response = requests.post(url, headers=headers, files=files) 

print(response.text) 

回答

4

所以API端點取一個字節數組,但還需要輸入體PARAM作爲data,不files。無論如何,下面的代碼適用於我。

url = "https://api.projectoxford.ai/face/v1.0/detect" 

headers = { 
    'ocp-apim-subscription-key': "<key>", 
    'Content-Type': "application/octet-stream", 
    'cache-control': "no-cache", 
} 

data = open('IMG_0670.jpg', 'rb').read() 

response = requests.post(url, headers=headers, data=data) 

print(response.text) 
相關問題