【问题标题】:Python POST request error "image format unsupported" using Microsoft Face API使用 Microsoft Face API 的 Python POST 请求错误“不支持图像格式”
【发布时间】:2016-09-17 20:38:16
【问题描述】:

我正在尝试发送二进制图像文件来测试 Microsoft Face API。使用 POSTMAN 效果很好,我按预期返回了 faceId。但是,我尝试将其转换为 Python 代码,但它目前给了我这个错误:

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

我读过这个SO post,但它没有帮助。这是我发送请求的代码。我试图模仿邮递员正在做的事情,例如用标题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)

【问题讨论】:

    标签: python azure python-requests azure-api-apps microsoft-cognitive


    【解决方案1】:

    所以 API 端点接受一个字节数组,但也需要输入正文参数为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)
    

    【讨论】:

      猜你喜欢
      • 2016-07-07
      • 2017-11-18
      • 2016-07-28
      • 1970-01-01
      • 2018-04-13
      • 1970-01-01
      • 1970-01-01
      • 2019-10-07
      • 1970-01-01
      相关资源
      最近更新 更多