Mallow's Blog

Facial Recognition in iOS with AWS Rekognition – II

In our previous blog, we have seen a brief introduction of AWS Rekognition service and its advantages. As I stated before we are about to identify your app’s user based on the picture. For this, you need two things for identifying your app user using their picture,

  1. The current picture of the user. 
  2. Old picture of the user to compare with the current picture.

In this part of this series, we will see how to create a new face collection in AWS Rekognition and how to add users face in it. Let’s get started,

Where to save the old picture and how?

When a new user signs up to your app, you can ask them to upload the valid image as their profile picture. If they upload a valid image, you can save their image to face collection in AWS Rekognition. Face collection? What it is? 

Face collection: 

This is the place where AWS Rekognition will maintain all your user’s face data as mathematical value. When you try to search a user by using their current photo, AWS Rekognition will extract feature vectors(Mathematical value) from the currently uploaded image and will check whether the same kind of feature vectors are available in face collection. You will get a response based on the result of the searching process. 

Let’s see how we can create face collection in AWS Rekognition from your iOS app using AWS Rekognition SDK. Before start creating your face collection you have to setup few things,

  1. Integrate AWS Rekognition SDK using cocoapods or by doing the manual integration. 
  2. Create a new pool ID in AWS. For complete procedure refer to this link.

AWS setup:

Before starting anything in AWS Rekognition we need to do following setup in AppDelgate. You can add this logic in didFinishLaunch(…) method

import AWSCore
import AWSRekognition

// AWS Configuration
let credentialsProvider = AWSCognitoCredentialsProvider(regionType:.EUWest1, identityPoolId: “Your identity pool ID”)
let configuration = AWSServiceConfiguration(region: “Your region”, credentialsProvider:credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration

1. Creating new face collection:

The below code help you to create new face collection in AWS Rekognition.

import AWSRekognition

let colRequest = AWSRekognitionCreateCollectionRequest() // 1
colRequest?.collectionId = “configure_your_face_collection_id_here”  // 2
AWSRekognition.default().createCollection(colRequest!) { (response, error) in  // 3
    if error == nil {
        // face collection is successfully created.
    } else {
        // Request Failed
        print(error)
    }
}

Description:

1. We need to create new AWSRekognitionCreateCollectionRequest to create new face collection in AWS Rekognition.

2. Configure your face collection ID for your new face collection. In future, we need to use this face collection id to search, indexing operation, etc…

3. Start creating new face collection by calling .createCollection with AWSRekognitionCreateCollectionRequest.

2. Saving user’s picture in face collection:

You have successfully created face collection in AWS Rekognition. Next thing is you have to save your user’s face in it. The below code will help you to save your user’s face in your face collection. 

import AWSRekognition
let indexReq = AWSRekognitionIndexFacesRequest() // 1
indexReq?.collectionId = "configure_your_face_collection_id_here"  // 2
indexReq?.externalImageId = “configure_your_user_id” // 3

let image = AWSRekognitionImage() // 4
image?.bytes = imageData // 5

indexReq?.image = image // 6
AWSRekognition.default().indexFaces(indexReq!) { (response, error) in
    if error == nil {
        print("Error occured")
    }
}

Description:

1. For saving a face in the face collection we need to use .indexFaces method in AWS Rekognition SDK using AWSRekognitionIndexFacesRequest.

2. Configure your face collection ID which you configured in Creating new face collection process.

3. Configure the external image ID of the image which you are uploading. You can configure a unique value of your user as external Image ID. Say for example User ID.

4. Create a new AWSRekognitionImage instance

5. Assign your input image data as bytes of AWSRekognitionImage.

6. Assign your AWSRekognitionImage instance as an image value of indexReq.

7. Hit .indexFace function in AWS Rekognition SDK.

In this post, we have seen how to create face collection and adding a face to it. In this process, we have many issues, if your user uploads any celebrity image, the object image, etc. it will accept. But when you search it by using user’s current image you will get nothing. For this, we need to add many validations to make sure your user is uploading their original image. 

In next blog, we will see how to identify your user using user’s current picture and how to add validation while uploading face to face collection.


Karthick S,
iOS Development Team,
Mallow Technologies.

Leave a Reply

%d bloggers like this: