AWS Rekognition -Recognising Faces in Photos using Java

Family ToddIs that my Wife in that Photo?

AWS Rekognition is a remarkable service from AWS that allows you to search images and recognise faces, features, and also do comparisons between images. I have been testing it out and I wanted to create a very basic application that would take a photo of me, and then see what other photos I was also in. I have done this in the simplest way possible to show you how easy this new service is to use from AWS. In just a few lines of code I was able to look at a photo and see if I was in it!

So here goes. Here is the code, written in Java, contained in a simple main method:

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.rekognition.AmazonRekognitionClient;
import com.amazonaws.services.rekognition.model.*;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.List;

public class IndexAndListFacesExample {

    public static final String S3_BUCKET = "mybucketname_nospaces_otherwiseitwill_not_work";

    public static void main(String[] args) {
        AWSCredentials credentials = new ProfileCredentialsProvider().getCredentials();
        AmazonRekognitionClient amazonRekognition = new AmazonRekognitionClient(credentials);
        CompareFacesRequest compareFacesRequest = new CompareFacesRequest();
        compareFacesRequest.setSourceImage(getImageUtil(S3_BUCKET, "sarah.jpg"));
        compareFacesRequest.setTargetImage(getImageUtil(S3_BUCKET, "family.jpg"));
        CompareFacesResult compareFacesResult = amazonRekognition.compareFaces(compareFacesRequest);
        List<CompareFacesMatch> matchingFaces  = compareFacesResult.getFaceMatches();
        matchingFaces.forEach(m -> System.out.println(m.getFace().getConfidence()));
    }
    private static Image getImageUtil(String bucket, String key) {
        return new Image()
                .withS3Object(new S3Object()
                        .withBucket(bucket)
                        .withName(key));
    }
}

In the classpath you will require the AWS SDK. I used a simple Maven dependency for this project.


<dependency>
       <groupId>com.amazonaws</groupId>
       <artifactId>aws-java-sdk</artifactId>
       <version>1.11.526</version>
</dependency>

So how does it work? Well it is fairly self explanatory I hope. I first set up my AWSCredentials object. This assumes that you have the AWS CLI installed and configured. If you do not. Then please check the AWS instructions found here.

Then an AWSRekognitionClient is created along with a CompareFacesRequest. The Request has a source and target object set which are image objects located in a bucket. CAUTION. Bucket names must not have periods in the name. I wasted two days thinking my code was faulty because of that undocumented ‘feature’. I ran the application using the US-EAST -1 region for the bucket.

CompareFacesRequest compareFacesRequest = new CompareFacesRequest();
compareFacesRequest.setSourceImage(getImageUtil(S3_BUCKET, "sarah.jpg"));
compareFacesRequest.setTargetImage(getImageUtil(S3_BUCKET, "family.jpg"));

Sarah ToddThe source image is the image that contains the face you are looking for, so in my case, a nice picture of my wife. The target is then the image that you want to search to see if you are in it.

Once you have set up the request with the target and source image, you can then invoke the service using the compareFaces method.


CompareFacesResult compareFacesResult = amazonRekognition.compareFaces(compareFacesRequest);
List matchingFaces  = compareFacesResult.getFaceMatches();

This method returns a CompareFacesResult which contains a List of any matches. Although it would be impressive if you turned out to be in the same photo more than once!

When I iterate through the list of matches (I compared my wife’s face with a photo containing the entire family), then unsurprisingly I found a match. she was, after all, in the family photo. For each match it then also provides a confidence score. In the example, it was 99.99972% sure she was in the picture, so I will take that as a yes.

As you can see, it is a simple API to work with, and you could adapt this to process whole sets of pictures rather than just one single picture, but you get the idea, and this is a nice simple example to you started.

 

 

Add a Comment

Your email address will not be published. Required fields are marked *