Image Comparison with AWS Rekognition using JAVA

AWS Rekognition is a service that enables you to add image and video analysis to your application. So this is the place where you’ll get to know how to embodied AWS Rekognition with your JAVA application.

In this blog, I’ll demystify you the CompareFaces API of AWS Rekognition to compare two images provided by your Java application.

To use AWS Rekognition in Java, you have to add dependency of AWS Rekognition in your pom.xml file.

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

Now you’re able to use the Rekognition service in your application, so just import rekognition.model package to use the CompareFaces API. This package provides all the APIs of Rekognition.

Next step is the create an AWS client through which we’ll use the API. To create AWS client there are various steps like, create a client with default credentials or create client by providing credentials with environment or by providing manually, so here we’ll create client by providing credentials manually in our java application.

String awsAccessKey = "SAMPLEACCESSKEY";
String awsSecretKey = "SAMPLESECRETKEY";
AWSCredentials awsCredentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
AmazonRekognition amazonRekognition = new AmazonRekognitionClient(awsCredentials);

Our AWS client is created and we’re ready to hit the Rekognition API to compare images.
The usecase of the CompareFaces API is:

1. Create a request by using CompareFaceRequest
2.
Pass the request to compareFaces() function to get the Result
3.
Fetch matched or unmatched faces from the Result

Step 1: Create a request by using CompareFaceRequest

CompareFacesRequest compareFacesRequest = new CompareFacesRequest()
                .withSourceImage(sourceImage)
                .withTargetImage(targetImage);

CompareFacesRequest is a class which extends AmazonWebServiceRequest and creates a request which contains source and target image.

Step 2 : Pass the request to compareFaces() to get the Result

CompareFacesResult compareFacesResult = amazonRekognition.compareFaces(compareFacesRequest);

Here we pass our created request in compareFaces() function of AmazonRekognition and store the result in CompareFacesResult class.
compareFaces() is the abstract method of the AmazonRekognition and it compares the largest face detected in the source image with each of the 100 largest faces detected in the target image.

In response, the operation returns an array of face matches ordered by similarity score in descending order.
For each face match, the response provides a bounding box of the face, facial landmarks, pose details (pitch, role, and yaw), quality (brightness and sharpness), and confidence value (indicating the level of confidence that the bounding box contains a face).
The response also provides a similarity score, which indicates how closely the faces match and an array of faces that don’t match the source image.

Step 3: Fetch matched and unmatched faces from the Result

List <CompareFacesMatch> faceDetails =                                                 
compareFacesResult.getFaceMatches();                                      
for (CompareFacesMatch match: faceDetails){
  ComparedFace face= match.getFace();
  BoundingBox position = face.getBoundingBox();
  System.out.println("Face at " + position.getLeft().toString()
               + " " + position.getTop()
               + " matches with " + match.getSimilarity().toString()
               + "% confidence.");
 }
List<ComparedFace> uncompared = compareFacesResult.getUnmatchedFaces();
System.out.println("There was " + uncompared.size()
            + " face(s) that did not match");

compareFacesResult.getFaceMatches() provides the array of matched faces and we get it into the List of CompareFacesMatch class and then extract each of the details provided by the compareFaces() in an array of matched faces.
And, compareFacesResult.getUnmatchedFaces() provides the array of unmatched faces and here we get that array into the list and print them.

Hope you’ll get acquainted with Image Comparison in Rekognition using Java.
Thank You!


Knoldus-blog-footer-image

Written by 

Pawan Singh Bisht is the Trainee Software Consultant at Knoldus Software LLP. He has good knowledge of languages C++ and Java, and currently working on Rust. He is always ready to accept new challenges. As a fresher, he always tries to explore the different type of software and tools.

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!