要使用AWS Rekognition进行文本检测,您需要首先创建一个Amazon Rekognition客户端。以下是一个使用AWS SDK for Python(Boto3)的示例代码:
import boto3
# 创建Rekognition客户端
rekognition_client = boto3.client('rekognition', region_name='us-west-2')
# 定义要检测的图像和特性
image = {
'S3Object': {
'Bucket': 'your_bucket_name',
'Name': 'your_image_name.jpg'
}
}
features = ['TEXT_DETECTION']
# 调用Rekognition的detect_text函数进行文本检测
response = rekognition_client.detect_text(Image=image, Features=features)
# 处理返回的结果
text_detections = response['TextDetections']
for text in text_detections:
print('Detected text: ' + text['DetectedText'])
print('Confidence: ' + str(text['Confidence']))
print('Id: {}'.format(text['Id']))
print('Parent Id: {}'.format(text['ParentId']))
print('Type: {}'.format(text['Type']))
print()
在上述代码中,您需要将your_bucket_name
替换为存储图像的S3存储桶的名称,将your_image_name.jpg
替换为要进行文本检测的图像的名称。然后,通过调用detect_text
函数执行文本检测,并处理返回结果。
请注意,您需要确保已经正确配置并设置了AWS凭据,以便Boto3可以连接到您的AWS帐户。