[英]Form Recognizer custom model fails with invalid file type `{"error":{"code":"1000","message":"Invalid input file."}}`
I have successfully trained a custom model for key value extraction, however any file or file type I use to evaluate the model is failing to return a result.我已经成功训练了自定义 model 用于键值提取,但是我用来评估 model 的任何文件或文件类型都无法返回结果。 So far I have tried both pdf and png files.
到目前为止,我已经尝试了 pdf 和 png 文件。
I have matched the query provided in the API docs to create my query but it still fails, any suggestions?我已匹配API 文档中提供的查询来创建我的查询,但它仍然失败,有什么建议吗?
import requests
import json
import os
import pathlib
# path of file to evaluate
floc = 'path/to/file'
# extract file type
file_type = pathlib.Path(floc).suffix[1:]
# set headers with file type and our api key
headers = {
'Content-Type': f'application/{file_type}',
'Ocp-Apim-Subscription-Key': os.environ["AZURE_FORM_RECOGNIZER_KEY"]
}
# read in the file as binary to send
files = {'file': open(floc, 'rb')}
# post the file to be analysed
r = requests.post(
f'https://eastus.api.cognitive.microsoft.com/formrecognizer/v2.1/custom/models/{os.environ["MODEL_ID"]}/analyze',
headers=headers,
files=files
)
r
The result is 400
with the following error:结果为
400
,错误如下:
{"error":{"code":"1000","message":"Invalid input file."}}
A very similar query using the layout/analyze
request works perfectly.使用
layout/analyze
请求的非常相似的查询非常有效。 I have also read this question that has the same error but from cURL but it has not helped.我也读过这个有相同错误但来自 cURL 的问题,但它没有帮助。
I have fixed my problem but will leave my answer for any one else.我已经解决了我的问题,但会为其他人留下我的答案。
There were two main problems:有两个主要问题:
files
rather than data
files
而不是data
的 pdfThe fix is found below:修复如下:
import requests
import json
import os
import pathlib
# path of file to evaluate
floc = 'path/to/file'
# extract file type
file_type = pathlib.Path(floc).suffix[1:]
# set headers with file type and our api key
headers = {
'Content-Type': f'application/{file_type}',
'Ocp-Apim-Subscription-Key': os.environ["AZURE_FORM_RECOGNIZER_KEY"]
}
# post the file to be analysed
r = requests.post(
f'{endpoint}/formrecognizer/v2.1/custom/models/{os.environ["MODEL_ID"]}/analyze',
headers=headers,
data=open(floc, 'rb') # send binary of your file
)
r
You can find your own endpoint
value by going on to the Azure instance for your form_recognizer:您可以通过转到 Azure 实例为您的 form_recognizer 找到您自己的
endpoint
值:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.