Face recognition of celebrities in Java with Amazon Rekognition
Tags: aws, java, machinelearning
Published: 2021-01-31 17:52:18

As stated on the Amazon Web Services website, Amazon Rekognition is a service that enables you to add image analysis to your applications. With Rekognition, you can detect objects, scenes, and faces in images. 
&nbsp;

Among the most important features offered by Amazon Rekognition are:

* Objects and Scene Detection
* Moderation of Images
* Facial Analysis
* Facial Recognition
* Celebrity Recognition

&nbsp;
In this technical example, we’re going to use Java SDK to show how to develop a simple and useful application that recognizes celebrities' faces in order to demonstrate the capabilities of the service. 

We picked various pictures of recognized celebrities like Cristiano Ronaldo, Celine Dion, and Sting, obtaining favorable results as shown in the infographic:
&nbsp;
![](https://uploads-ssl.webflow.com/5f3acb2672fdcd05b7611500/5f3ebafd31db673db4acd1e4_AWS%2BRekognition%2Bresults%2B(1).jpg)
&nbsp;
For the technical side, we will focus on the most important steps and will omit the less-specific parts (you can get all the code at [Github](https://github.com/jeetkunecoder/aws-rekognition-app)). In this example, we will use the Java SDK. 

## Steps
&nbsp;
**1)** Create an Amazon Rekognition client using the `AmazonRekognitionClientBuilder` including the Region to be used and a `RecognizeCelebritiesRequest` containing the representation in bytes of the picture.

```java
AmazonRekognition amazonRekognition = AmazonRekognitionClientBuilder
  .standard().withRegion(Regions.US_WEST_2).build();   

RecognizeCelebritiesRequest request = new RecognizeCelebritiesRequest()
	.withImage(new Image().withBytes(imageBytes));
```

&nbsp;
**2)** Create a `RecognizeCelebritiesResult` object that will contain the obtained results:
```java
RecognizeCelebritiesResult result = amazonRekognition.recognizeCelebrities(request);
```
&nbsp;

**3)** Display the recognized celebrity information:
```java
List celebs = result.getCelebrityFaces();    
System.out.println(celebs.size() + " celebrity(s) were recognized.\n");    
List metadata = new ArrayList();      

for (Celebrity celebrity: celebs) {  
  // Name        
  System.out.println("Celebrity recognized: " + celebrity.getName());
  metadata.add("Celebrity recognized: " + celebrity.getName());
   
  // Id        
  System.out.println("Celebrity ID: " + celebrity.getId());        
  metadata.add("Celebrity ID: " + celebrity.getId());               

  // Extra info        
  System.out.println("Further information (if available):");       
  metadata.add("Further information (if available):"); 
  
  for (String url : celebrity.getUrls()) {           
    System.out.println(url);          
    metadata.add(url);        
  }
}
```
&nbsp;

Currently, **Amazon Rekognition** only supports JPG and PNG formats, which is a disadvantage compared to Google Cloud Vision that provides support for a wide range of formats such as GIF, BMP, and RAW among others. 
&nbsp;
However, it should be noted that during the tests performed there were no performance issues due to the type of format, and better effectiveness of the tool could be observed when the faces were clearly visible or at a frontal angle when the contrast with the background was notorious.
&nbsp;
Another feature to note is that the **API** was designed to include as many celebrities as possible based on feedback from AWS clients, so new names are constantly being added to the list after some analysis. Taking this into consideration, we did some extra testing using celebrity images from countries like **Panama and Colombia** to diversify the evidence based on a wider cultural spectrum. The results we obtained were as follows:
&nbsp;
![](https://uploads-ssl.webflow.com/5f3acb2672fdcd05b7611500/5f3ebbabd2e2d484cc2ba9d1_download%2B(22).png)
In the first two tests, satisfactory results were observed, the tool was able to recognize Panamanian boxing legend Roberto Durán and the well-known Colombian footballer, James Rodriguez. We did a third test, using a lower resolution image to test the limits of the tool, and did not get favorable results when trying to recognize the great martial artist of all time, Bruce Lee.
&nbsp;
![](https://uploads-ssl.webflow.com/5f3acb2672fdcd05b7611500/5f3ebbbf47cffd5f1cdfe996_download%2B(23).png)
&nbsp;
Taking these results into account, it can be affirmed that the quality and resolution of the image are an aspect to consider when using the tool. If it is desired to obtain favorable results, it is important to ensure the use of images of considerable quality, taking into account that the service still seems to be developing improvements as the **Deep Learning** models are fed.
&nbsp;
Amazon Rekognition it's been in the market since 2016, offering great capabilities like scalability, high availability, and competitive pricing. There’s a great potential for Rekognition to become a standard for image analysis based on Deep Learning algorithms, along with competing services like [Clarifai](https://www.clarifai.com/), [Microsoft Cognitive Services](https://azure.microsoft.com/en-us/services/cognitive-services/), and [Google’s Vision API](https://cloud.google.com/vision/).
&nbsp;
![](https://uploads-ssl.webflow.com/5f3acb2672fdcd05b7611500/5f3ebbd92c410b378490338a_Amazon%2Brekognition.png)
&nbsp;
The near future looks very promising for software development focused on Artificial Intelligence, a market that will definitely open new horizons for the creation of impressive tools that could have a beneficial impact on our society.