AWS提供了Rekognition服务,其中包括自定义标签识别功能。以下是一个使用Python SDK(boto3)进行AWS自定义标签识别的示例代码:
import boto3
def detect_custom_labels(image_path, min_confidence):
# 创建Rekognition客户端
client = boto3.client('rekognition')
# 读取图像字节流
with open(image_path, 'rb') as image:
image_bytes = image.read()
# 发起自定义标签识别请求
response = client.detect_custom_labels(
Image={'Bytes': image_bytes},
MinConfidence=min_confidence
)
# 解析响应结果
custom_labels = response['CustomLabels']
for label in custom_labels:
print('标签名:', label['Name'])
print('置信度:', label['Confidence'])
print('---------------------------')
# 指定图像路径和最小置信度
image_path = '/path/to/image.jpg'
min_confidence = 70
# 调用自定义标签识别函数
detect_custom_labels(image_path, min_confidence)
在上述代码中,我们使用boto3
库创建了一个Rekognition客户端,并通过detect_custom_labels
函数发起自定义标签识别请求。该函数接收图像路径和最小置信度作为参数,并打印出识别结果。
请注意,你需要安装boto3
库,并配置好AWS凭证(Access Key和Secret Access Key)才能成功运行该代码。
下一篇:AWS自定义IAM策略问题