db46
db46

Reputation: 83

AWS Rekognition - create image from detect-faces bounding box

Currently trying to figure out how to make face crops from bounding boxes (from detect-faces response) and use those crops to search an existing collection using the SearchFacesByImage API

This is mentioned on the SearchFacesByImage documentation.

You can also call the DetectFaces operation and use the bounding boxes in the response to make face crops, which then you can pass in to theSearchFacesByImage operation

I am trying to do this in Python or Node.js in a Lambda function. The input image is an s3 object.

All help greatly appreciated.

Upvotes: 2

Views: 2188

Answers (2)

Milton Bertachini
Milton Bertachini

Reputation: 626

I did this script in java, maybe its help

        java.awt.image.BufferedImage image = ...
    com.amazonaws.services.rekognition.model.BoundingBox target ...


    int x = (int) Math.abs((image.getWidth() * target.getLeft()));
    int y = (int) Math.abs((image.getHeight() *target.getTop()));;
    int w = (int) Math.abs((image.getWidth() * target.getWidth()));
    int h = (int) Math.abs((image.getHeight() * target.getHeight()));

    int finalX = x + w;
    int finalH = y + h;

    if (finalX > image.getWidth())
        w = image.getWidth()-x;

    if (finalH > image.getHeight())
        h = image.getHeight()-y;


    System.out.println(finalX);
    System.out.println(finalH);

    //
    //
    BufferedImage subImage = image.getSubimage(
            x, 
            y, 
            w,
            h);

    //
    //
    String base64 = ImageUtils.imgToBase64String(subImage, "jpg");

Upvotes: 0

R. Patel
R. Patel

Reputation: 63

I have faced the exact same problem. Refer this link from AWS Documentation. Here you will find sample code for python or java both. It will return the Top, Lfet, Width and Height of the bounding box. Remember, the upper-left corner will be considered as (0,0).

Then if you use python, you can crop image with cv2 or PIL.

Here is an example with PIL:

from PIL import Image

img = Image.open( 'my_image.png' )
cropped = img.crop( ( Left, Top, Left + Width, Top + Height ) ) 
cropped.show()

In this code Top, Lfet, Width and Height is a response from code given in the link.

Upvotes: 1

Related Questions