如何使用 AWS Rekognition return 图像与边界框?

How to return image with bounding box using AWS Rekognition?

当我将图像上传到 s3 存储桶并调用 AWS Rekognition detect_labels 我得到检测到的标签的字典,如下所示

{'Labels': [{'Name': 'Plant', 'Confidence': 99.70314025878906, 'Instances': [], 'Parents':[]},{'Name':'Flower','Confidence':98.37027740478516,'Instances':[],'Parents':[{'Name': 'Plant'}]}

但这里我需要 return 带有识别对象边界框的图像,如何实现?

您必须使用 rekognition.detect_labels 方法对图像执行对象检测。然后您可以使用 BoundingBox 属性 of Labels 列表来检索获取边界框的坐标,下面的代码可以是一个好的开始。

import boto3
import io
from PIL import Image, ImageDraw, ImageFont

file_name = 'plant.jpg'
# Get Rekognition client
rek_client = boto3.client('rekognition')
with open(file_name, 'rb') as im:
    # Read image bytes
    im_bytes = im.read()
    # Upload image to AWS 
    response = rek_client.detect_labels(Image={'Bytes': im_bytes})
    # Get default font to draw texts
    image = Image.open(io.BytesIO(im_bytes))
    font = ImageFont.truetype('arial.ttf', size=80)
    draw = ImageDraw.Draw(image)
    # Get all labels
    w, h = image.size
    for label in response['Labels']:
        name = label['Name']
        # Draw all instancex box, if any
        for instance in label['Instances']:
            bbox = instance['BoundingBox']
            x0 = int(bbox['Left'] * w) 
            y0 = int(bbox['Top'] * h)
            x1 = x0 + int(bbox['Width'] * w)
            y1 = y0 + int(bbox['Height'] * h)
            draw.rectangle([x0, y0, x1, y1], outline=(255, 0, 0), width=10)
            draw.text((x0, y1), name, font=font, fill=(255, 0, 0))

    image.save('labels.jpg')

来自DetectLabels API documentation

DetectLabels returns bounding boxes for instances of common object labels in an array of Instance objects. An Instance object contains a BoundingBox object, for the location of the label on the image. It also includes the confidence by which the bounding box was detected.

这在 Detecting labels documentation

中有详细说明

Amazon Rekognition Image and Amazon Rekognition Video can return the bounding box for common object labels such as cars, furniture, apparel or pets. Bounding box information isn't returned for less common object labels. You can use bounding boxes to find the exact locations of objects in an image, count instances of detected objects, or to measure an object's size using bounding box dimensions.

简而言之,并非所有标签都返回边界框信息。只有当标签是具有边界框信息的 'common object' 时,@Allan Chua 的代码才会在图像中绘制边界框。在您提供的示例 API 响应中,none 个标签具有边界框信息。