[英]AWS textract: UnsupportedDocumentException for PNG and JPG images. Error occurs only in production and not locally
[英]AWS Textract - UnsupportedDocumentException - PDF
我正在使用 boto3(用於 python 的 aws sdk)分析文檔(pdf)以獲取表單鍵:值對。
import boto3
def process_text_analysis(bucket, document):
# Get the document from S3
s3_connection = boto3.resource('s3')
s3_object = s3_connection.Object(bucket, document)
s3_response = s3_object.get()
# Analyze the document
client = boto3.client('textract')
response = client.analyze_document(Document={'S3Object': {'Bucket': bucket, 'Name': document}},
FeatureTypes=["FORMS"])
process_text_analysis('francismorgan-01', '709 Privado M SURESTE.pdf')
我使用分析文檔遵循了 AWS 的文檔,當我運行我的函數時出現錯誤。
botocore.errorfactory.UnsupportedDocumentException: An error occurred (UnsupportedDocumentException) when calling the AnalyzeDocument operation: Request has unsupported document format
我錯過了什么嗎?
AnalyzeDocument是一個同步 API,僅支持 PNG 或 JPG 圖像。
由於您想要處理 PDF 文件,因此您需要使用 Amazon Textract Asynchronous API,例如StartDocumentAnalysis 、 StartDocumentTextDetection
正如文檔所說
StartDocumentAnalysis 可以分析 JPEG、PNG、TIFF 和 PDF 格式的文檔中的文本。 文檔存儲在 Amazon S3 存儲桶中。 使用 DocumentLocation 指定文檔的存儲桶名稱和文件名。
import boto3
client = boto3.client('textract')
response = client.start_document_analysis(
DocumentLocation={
'S3Object': {
'Bucket': 'YOUR_BUCKET_NAME',
'Name': 'YOUR_FILE_KEY_NAME'
}
},
FeatureTypes=["FORMS"]
)
# Get results from asynchronous operation
result = client.get_document_analysis(JobId=response['JobId'])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.