【问题标题】:Azure form recognizer error: "Failed to download image from input URL."Azure 表单识别器错误:“无法从输入 URL 下载图像。”
【发布时间】:2020-09-26 00:02:09
【问题描述】:

我正在关注 these instructions 以使用 Azure 的布局表单识别器服务 其中有以下代码:

########### Python Form Recognizer Async Layout #############

import json
import time
from requests import get, post

# Endpoint URL
endpoint = r"<Endpoint>"
apim_key = "<Subscription Key>"
post_url = endpoint + "/formrecognizer/v2.0-preview/Layout/analyze"
source = r"<path to your form>"

headers = {
    # Request headers
    'Content-Type': 'application/json',
    'Ocp-Apim-Subscription-Key': apim_key,
}
with open(source, "rb") as f:
    data_bytes = f.read()

try:
    resp = post(url = post_url, data = data_bytes, headers = headers)
    if resp.status_code != 202:
        print("POST analyze failed:\n%s" % resp.text)
        quit()
    print("POST analyze succeeded:\n%s" % resp.headers)
    get_url = resp.headers["operation-location"]
except Exception as e:
    print("POST analyze failed:\n%s" % str(e))
    quit()

我尝试了以下错误的代码:

POST analyze failed:
{"error":{"code":"FailedToDownloadImage","message":"Failed to download image from input URL."}}
POST analyze succeeded:
{'Transfer-Encoding': 'chunked', 'Content-Type': 'application/json; charset=utf-8', 'x-envoy-upstream-service-time': '4', 'apim-request-id': '515e93ee-4db8-4174-92b1-63e5c415c056', 'Strict-Transport-Security': 'max-age=31536000; includeSubDomains; preload', 'x-content-type-options': 'nosniff', 'Date': 'Sat, 06 Jun 2020 20:47:28 GMT'}
POST analyze failed:
'operation-location'

我使用的代码是:

import json
import time
from requests import get, post

在发出请求并验证它是否已加载到变量中之前,我正在阅读 pdf 文件

source = r"data/Invoice_7.pdf" 
with open(source, "rb") as f:
    data_bytes = f.read()

print (data_bytes[0:10])

然后是请求详情:

endpoint = r"https://xxxx.cognitiveservices.azure.com/"

apim_key = "xxxx"
post_url = endpoint + "/formrecognizer/v2.0-preview/Layout/analyze"

headers = {
    # Request headers
    'Content-Type': 'application/json',
    'Ocp-Apim-Subscription-Key': apim_key,
}

最后发出请求:

try:
    resp = post(url = post_url, data = data_bytes, headers = headers)
    print (1)
    if resp.status_code != 202:
        print("POST analyze failed:\n%s" % resp.text)
        #quit()
    print (2)
    print("POST analyze succeeded:\n%s" % resp.headers)
    print (3)
    get_url = resp.headers["operation-location"]
    print (4)
except Exception as e:
    print("POST analyze failed:\n%s" % str(e))
    #quit()

我在每一步都打印一个数字,因为我觉得很奇怪,我得到了失败和成功的请求响应。结果是这样的:

1
POST analyze failed:
{"error":{"code":"FailedToDownloadImage","message":"Failed to download image from input URL."}}
2
POST analyze succeeded:
{'Transfer-Encoding': 'chunked', 'Content-Type': 'application/json; charset=utf-8', 'x-envoy-upstream-service-time': '1', 'apim-request-id': '93a2a162-d14f-496f-ba8a-077bcfd5d3c7', 'Strict-Transport-Security': 'max-age=31536000; includeSubDomains; preload', 'x-content-type-options': 'nosniff', 'Date': 'Sat, 06 Jun 2020 21:00:20 GMT'}
3
POST analyze failed:
'operation-location'

所以代码在这一行失败:

get_url = resp.headers["operation-location"]

响应变量中的文本是:

'{"error":{"code":"FailedToDownloadImage","message":"Failed to download image from input URL."}}'

【问题讨论】:

    标签: python azure azure-cognitive-services form-recognizer


    【解决方案1】:

    As defined in the REST API documentation,需要指定Content-Type。当您将 Content-Type 设置为 application/json 时,您需要通过 JSON 提供可公开访问的源。在您的情况下,您需要将 Content-Type 设置为 application/pdf。当你想让这个动态时,你可以使用 PyPi 包filetype

    顺便说一句,您知道有一个(beta) Python SDK for Form Recognizer, 可以用于您的用例。

    【讨论】:

    • 解决了 pdf 的问题,现在我尝试使用图像为:application/png,我得到这个错误:“code”:“BadArgument”,“message”:“不支持的媒体类型。”
    • @LuisRamonRamirezRodriguez 请查看我链接的documentation。您需要将MIME type 用于Content-Type,例如image/png。我的建议是使用filetype,而不是在脚本中硬编码。
    猜你喜欢
    • 2020-05-12
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-18
    相关资源
    最近更新 更多