要解决“Amazon Face Rekognition的边界框坐标始终相同”的问题,您可以使用以下代码示例:
import boto3
# 创建Rekognition客户端
rekognition_client = boto3.client('rekognition')
# 指定图像的S3存储桶和键
bucket = 'your_bucket_name'
image_key = 'your_image_key.jpg'
# 调用DetectFaces API获取人脸边界框坐标
response = rekognition_client.detect_faces(
Image={'S3Object': {'Bucket': bucket, 'Name': image_key}},
Attributes=['DEFAULT']
)
# 解析响应并提取边界框坐标
if response['FaceDetails']:
for face_detail in response['FaceDetails']:
# 访问边界框坐标
bounding_box = face_detail['BoundingBox']
left = bounding_box['Left']
top = bounding_box['Top']
width = bounding_box['Width']
height = bounding_box['Height']
# 打印边界框坐标
print(f"Left: {left}, Top: {top}, Width: {width}, Height: {height}")
else:
print('未检测到人脸')
请确保将上述代码中的your_bucket_name和your_image_key.jpg替换为实际的S3存储桶名称和图像键。此代码将通过Amazon Rekognition的detect_faces方法检测给定图像中的人脸,并提取每个人脸的边界框坐标。