2

我正在按照这些说明使用 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."}}'
4

1 回答 1

1

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

顺便说一句,您是否知道有一个用于表单识别器的(测试版)Python SDK,您可以将其用于您的用例。

于 2020-06-07T19:04:07.420 回答