You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何阻止AWS Textract自动校正图像方向以识别横竖混合文本?

问题描述

我需要分析一份包含横向和纵向文本的文档。尝试将图像旋转90度后提交给AWS Textract,但发现后端会在提取文本前自动校正图像方向,导致无法识别纵向文本。请问是否有办法阻止该行为?

测试代码

import boto3
from PIL import Image

textract = boto3.client('textract')

# 分析原始图片
with open('example.jpg', 'rb') as f:
    response = textract.detect_document_text(Document={'Bytes': f.read()})
[block['Text'] for block in response['Blocks'] if block['BlockType'] == 'LINE']
['Most text is horizontal', "Here's another horizontal line"] # 仅识别出横向文本

# 将图片旋转90度
image = Image.open('example.jpg')
image = image.rotate(-90)
image.save('example_rotated.jpg')

# 分析旋转后的图片
with open('example_rotated.jpg', 'rb') as f:
    response = textract.detect_document_text(Document={'Bytes': f.read()})
[block['Text'] for block in response['Blocks'] if block['BlockType'] == 'LINE']
['Most text is horizontal', "Here's another horizontal line"] # 仍然只识别出横向文本

示例图片

包含横纵向文本的示例图片

解决方案

要阻止AWS Textract自动校正图像方向,你需要使用**AnalyzeDocument API**并指定OrientationCorrection参数为"NONE"——detect_document_text API默认会自动校正方向,且不支持关闭该行为。

修改后的代码示例

import boto3

textract = boto3.client('textract')

# 分析旋转后的图片,禁用自动方向校正
with open('example_rotated.jpg', 'rb') as f:
    response = textract.analyze_document(
        Document={'Bytes': f.read()},
        FeatureTypes=['TABLES'],  # 必须指定至少一个FeatureType,可选值为TABLES/FORMS
        OrientationCorrection="NONE"  # 关键参数:禁用方向校正
    )

# 提取所有文本行
extracted_text = [block['Text'] for block in response['Blocks'] if block['BlockType'] == 'LINE']
print(extracted_text)

关键注意点

  • 使用AnalyzeDocument时必须指定至少一个FeatureTypes,否则会触发参数错误。
  • 禁用方向校正后,Textract会严格按照图片的原始方向识别文本,旋转后的纵向文本就能被正常提取。

内容的提问来源于stack exchange,提问作者Jeff Bezos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 11:12:07