Azure form recognizer error: "Failed to download image from input URL."

Azure form recognizer error: "Failed to download image from input URL."

我正在关注 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."}}'

As defined in the REST API documentation, you need to specify the Content-Type. When you set your Content-Type to application/json, you need to provide a public accessible source via JSON. In your case, you need to set the Content-Type to application/pdf. When you want to make this dynamic, you could make use of the PyPi package filetype.

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