0

我正在尝试使用 Microsoft Azure Form Recognizer API 上传发票 pdf 并在其中获取表格信息。

我能够成功发出 POST 请求。

但无法训练模型并收到错误消息“在指定的 Azure blob 容器中找不到有效的 blob。请符合文件格式/尺寸/页面/尺寸要求。'。

但是我在 blob 存储容器中有超过 5 个文件。

我还为 blob 容器提供了共享密钥。你可以找到我写的代码和附加的错误。

"""
Created on Thu Feb 20 16:22:41 2020

@author: welcome
"""

########## Python Form Recognizer Labeled Async Train #############
import json
import time
from requests import get, post

# Endpoint URL
endpoint = r"https://sctesting.cognitiveservices.azure.com"
post_url = endpoint + r"/formrecognizer/v2.0-preview/custom/models"
print(post_url)
source = '<source url from blob storage>'
prefix = "name of the folder"
includeSubFolders = False
useLabelFile = False

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

body =  {
    "source": source,
    "sourceFilter": {
        "prefix": prefix,
        "includeSubFolders": includeSubFolders
    },
    "useLabelFile": useLabelFile
}

try:
    resp = post(url = post_url, json = body, headers = headers)
    if resp.status_code != 201:
        print("POST model failed (%s):\n%s" % (resp.status_code, json.dumps(resp.json())))
        quit()
    print("POST model succeeded:\n%s" % resp.headers)
    get_url = resp.headers["location"]
except Exception as e:
    print("POST model failed:\n%s" % str(e))
    quit() 


n_tries = 15
n_try = 0
wait_sec = 3
max_wait_sec = 60
while n_try < n_tries:
    try:
        resp = get(url = get_url, headers = headers)
        resp_json = resp.json()
        if resp.status_code != 200:
            print("GET model failed (%s):\n%s" % (resp.status_code, json.dumps(resp_json)))
            quit()
        model_status = resp_json["modelInfo"]["status"]
        if model_status == "ready":
            print("Training succeeded:\n%s" % json.dumps(resp_json))
            quit()
        if model_status == "invalid":
            print("Training failed. Model is invalid:\n%s" % json.dumps(resp_json))
            quit()
        # Training still running. Wait and retry.
        time.sleep(wait_sec)
        n_try += 1
        wait_sec = min(2*wait_sec, max_wait_sec)     
    except Exception as e:
        msg = "GET model failed:\n%s" % str(e)
        print(msg)
        quit()
print("Train operation did not complete within the allocated time.")

通过运行上面的代码在 Anaconda 提示符中得到输出

 POST model succeeded:
{'Content-Length': '0', 'Location': 'https://sctesting.cognitiveservices.azure.com/formrecognizer/v2.0-preview/custom/models/30b7d99b-fc57-466d-a59b-c0d9738c03ac', 'x-envoy-upstream-service-time': '379', 'apim-request-id': '18cbec13-8129-45de-8685-83554e8b35d4', 'Strict-Transport-Security': 'max-age=31536000; includeSubDomains; preload', 'x-content-type-options': 'nosniff', 'Date': 'Thu, 20 Feb 2020 19:35:47 GMT'}
Training failed. Model is invalid:
{"modelInfo": {"modelId": "30b7d99b-fc57-466d-a59b-c0d9738c03ac", "status": "invalid", "createdDateTime": "2020-02-20T19:35:48Z", "lastUpdatedDateTime": "2020-02-20T19:35:50Z"}, "trainResult": {"trainingDocuments": [], "errors": [{"code": "2014", "message": "No valid blobs found in the specified Azure blob container. Please conform to the document format/size/page/dimensions requirements."}]}}      
4

4 回答 4

0

确保 Blob 存储容器中的文件符合此处的要求:https ://docs.microsoft.com/en-us/azure/cognitive-services/form-recognizer/overview#custom-model

如果您的文件看起来不错,还请检查您使用的是哪种 SAS 令牌。如果您使用的是策略定义的 SAS 令牌,则可能会出现错误消息,在这种情况下,请尝试切换到具有明确权限的 SAS 令牌,如下所述:https ://stackoverflow.com/a/60235222/12822344

于 2020-02-20T20:11:51.293 回答
0

如果您使用 from 识别器标签工具来做同样的事情,那会起作用吗?您是否将文件放在 Azure blob 的根目录或子目录中?

于 2020-02-20T20:07:11.690 回答
0

尝试Source Filter从身体中取出。它应该工作。

于 2020-06-11T09:00:04.550 回答
0

你没有指定来源。当您在所选存储帐户的菜单中时,您需要生成共享访问签名 (SAS)。如果该存储帐户中有容器,则需要在 URL 中包含容器名称。前任。如果您有一个名为“train”的容器:“www....windows.net/?sv=.....” ---> “www....windows.net/train?sv=... ……”。否则你可以尝试使用“前缀”字符串,但我发现它有问题。

此外,您还没有包含您的订阅密钥。

https://docs.microsoft.com/en-us/azure/cognitive-services/form-recognizer/quickstarts/python-train-extract

于 2020-05-25T20:03:34.470 回答