要使用AWS Textract检测行而非块,您可以使用以下代码示例:
import boto3
def detect_lines(image_path):
# 创建Textract客户端
textract_client = boto3.client('textract')
# 以字节数组的形式读取图像文件
with open(image_path, 'rb') as image_file:
image_bytes = image_file.read()
# 调用Textract的DetectDocumentText API
response = textract_client.detect_document_text(Document={'Bytes': image_bytes})
# 提取行
lines = []
for item in response['Blocks']:
if item['BlockType'] == 'LINE':
lines.append(item['Text'])
return lines
# 调用detect_lines函数并打印结果
image_path = 'your_image.jpg' # 替换为您的图像文件路径
lines = detect_lines(image_path)
for line in lines:
print(line)
在上述代码中,我们首先创建了一个Textract客户端对象。然后,我们使用detect_document_text
方法对图像进行文本检测。返回的响应包含了检测到的所有块。我们遍历每个块,并检查其类型是否为'LINE',如果是,则将其文本内容添加到一个列表中。最后,我们返回该列表并打印每一行的文本内容。
请注意,您需要先安装AWS SDK for Python(Boto3)并配置您的AWS凭证,以便访问Textract服务。