Face recognition app made with Amazon Rekognition and Java


This time, I would like to make a simple application using Amazon's Rekognition face recognition service. When you hear face recognition, you may have a difficult image, but if you use this Rekoginition, you can easily make it, so please try using it through a sample.

Only the excerpted source is posted in the article, so if you are interested, I will post the source I made this time in here. please refer.


--You have an AWS account. --The access key and secret key of the IAM user who has the policy of "Amazon Rekognition Full Access" must be acquired.

Operating environment

App screen

This is the screen of the app I made this time.


Development flow

  1. Create a collection to store the comparison source image (meta data)
  2. Store the comparison source image in the collection
  3. Take a picture with the camera at the time of authentication and upload
  4. Display the authentication result

Create a collection

Collection creation

ListCollectionsResult listCollectionsResult = rekognitionClient.listCollections(new ListCollectionsRequest());
if (!listCollectionsResult.getCollectionIds().contains(COLLECTION_ID)) {
    rekognitionClient.createCollection(new CreateCollectionRequest().withCollectionId(COLLECTION_ID));

Instead of simply creating a collection, I try to create one if it doesn't exist.

Rekognition API Contents
listCollections The list of collections will be returned.
createCollection Create a collection.

Store the comparison source image in the collection

Store comparison source image

IndexFacesRequest indexFacesRequest = new IndexFacesRequest()
              .withImage(new Image().withBytes(ByteBuffer.wrap(multipartFile.getBytes())));

IndexFacesResult indexFacesResult = rekognitionClient.indexFaces(indexFacesRequest);
Rekognition API Contents
indexFaces Store your face photo in the collection.
Property Description Available characters
collectionId Name of the created collection a-zA-Z0-9_.-
externalImageId Arbitrary data a-zA-Z0-9_.-:
image Comparison source image (binary)

This time, we will manage the ID that uniquely identifies the user in externalImageId.

Take a picture with the camera at the time of authentication and upload

SearchFacesByImageRequest searchFacesByImageRequest = new SearchFacesByImageRequest()
          .withImage(new Image().withBytes(ByteBuffer.wrap(multipartFile.getBytes())))

try {
    SearchFacesByImageResult searchFacesByImageResult = rekognitionClient.searchFacesByImage(searchFacesByImageRequest);
} catch (InvalidParameterException e) {
    //Error handling

Please note that InvalidParameterException will occur if the face cannot be recognized from the uploaded image.

Rekognition API Contents
searchFacesByImage Search from the collection based on the face photo to compare.
Property Description Available characters
collectionId Name of the created collection a-zA-Z0-9_.-
image Comparison source image (binary)
faceMatchThreshold Similarity threshold. 70 by default%
maxFaces Number of cases to be acquired from matching faces in descending order of similarity

In this sample, we tried to get the one with the highest similarity from the ones with 90% or more similarity.

Display authentication result

SearchFacesByImageResult can be obtained in the following format, so get externalFaceId from it.

   "FaceMatches": [ 
         "Face": { 
            "BoundingBox": { 
               "Height": number,
               "Left": number,
               "Top": number,
               "Width": number
            "Confidence": number,
            "ExternalImageId": "string",  <--this
            "FaceId": "string",
            "ImageId": "string"
         "Similarity": number
   "FaceModelVersion": "string",
   "SearchedFaceBoundingBox": { 
      "Height": number,
      "Left": number,
      "Top": number,
      "Width": number
   "SearchedFaceConfidence": number

If it matches the ID entered on the screen, authentication is OK, otherwise it is NG.

Reference site

-I tried face recognition with Amazon Rekognition

Recommended Posts

Face recognition app made with Amazon Rekognition and Java
Face recognition app with OpenCV + Android Studio
Use java with MSYS and Cygwin
Distributed tracing with OpenCensus and Java
I made a shopify app @java
Use JDBC with Java and Scala.
Output PDF and TIFF with Java 8
Run an application made with Java8 with Java6
Encrypt with Java and decrypt with C #
How to make an app with a plugin mechanism [C # and Java]
Monitor Java applications with jolokia and hawtio
Link Java and C ++ code with SWIG
Let's try WebSocket with Java and javascript!
[Java] Reading and writing files with OpenCSV
Install Java8 with Yum on Amazon Linux
Vending machine made with Java (domain driven)
I made a rock-paper-scissors app with android
Java development with Codenvy: Console app debug
Refactored GUI tools made with Java8 + JavaFX in 2016
Simple obstacle racing made with processing for Java
Build and test Java + Gradle applications with Wercker
Try to link Ruby and Java with Dapr
Publish the app made with ruby on rails
JSON with Java and Jackson Part 2 XSS measures
[Android] Despaired story with App UserId and SharedUserId
Prepare a scraping environment with Docker and Java
KMS) Envelope encryption with openssl and java decryption
Encrypt / decrypt with AES256 in PHP and Java
[Java] Convert and import file values with OpenCSV
[Review] Reading and writing files with java (JDK6)