Page updated Jan 16, 2024

Identify entities from images

Set up the backend

If you haven't already done so, run amplify init inside your project and then amplify add auth (we recommend selecting the default configuration).

Run amplify add predictions and select Identify. Then use the following answers:

1? What would you like to identify?
2 Identify Text
3❯ Identify Entities
4 Identify Labels
5 Learn More
6
7? Would you like use the default configuration? Default Configuration
8
9? Who should have access? Auth and Guest users

Now run amplify push which will generate your amplifyconfiguration.json and create resources in the cloud. You can now either add this to your backend or skip and add more features to your app.

Services used: Amazon Rekognition

Advanced configuration

You can enhance your application's ability to identify entities by performing indexing against a pre-defined collection of images and providing them to Amazon Rekognition. This can be done in one of two ways:

  1. Administrators provide images to be indexed from an S3 bucket
  2. Application users upload files to an S3 bucket which are indexed

Amplify configures Lambda Triggers to automatically perform this indexing when the files are uploaded. As Rekognition does not store any faces, but rather the facial features into a vector which are stored in the backend, these images can be deleted after indexing if you choose to do so. However note that by default the Lambda Triggers will remove the index if you delete an image so you will need to modify their behavior if you desire this functionality.

To add this functionality into your application choose advanced when prompted in the Identify flow (if you already enabled Identify Entities you will need to run amplify update predictions):

1? What would you like to identify? Identify Entities
2? Would you like use the default configuration? Advanced Configuration
3? Would you like to enable celebrity detection? Yes
4? Would you like to identify entities from a collection of images? Yes
5? How many entities would you like to identify 50
6? Would you like to allow users to add images to this collection? Yes
7? Who should have access? Auth and Guest users
8? The CLI would be provisioning an S3 bucket to store these images please provide bucket name: mybucket

Note that if you already have an S3 bucket in your project from running amplify add storage it would be reused. To upload images from the CLI for administrator indexing, you can run amplify predictions console and select Identify which will open the S3 bucket location in the AWS console for you to upload your images.

In the sample React application code ahead, you will see that to use this functionality you will need to set collection:true when calling Predictions.identify() and remove celebrityDetection: true. To test this flow, first upload an image to your S3 bucket location protected/predictions/index-faces/admin, and after a few seconds you can send this same image to Predictions.identify() and it will check if that image has been indexed in the Collection.

Working with the API

Predictions.identify({entities: {...}}) => Promise<> Detects entities from an image and potentially related information such as position, faces, and landmarks. Can also identify celebrities and entities that were previously added. This function returns a Promise that returns an object with the entities that was identified.

Input can be sent directly from the browser (using File object or ArrayBuffer object) or an Amazon S3 key from project bucket.

Detect entities directly from image uploaded from the browser. (File object)

1import { Predictions } from '@aws-amplify/predictions';
2
3const response = await Predictions.identify({
4 entities: {
5 source: {
6 file,
7 },
8 }
9});
10console.log({ response });

Detect entities directly from image binary from the browser. (ArrayBuffer object) This technique is useful when you have base64 encoded binary image data, for example, from a webcam source.

1import { Predictions } from '@aws-amplify/predictions';
2
3const response = await Predictions.identify({
4 entities: {
5 source: {
6 bytes: imageArrayBuffer,
7 },
8 }
9});
10console.log({ response });

From Amazon S3 key

1import { Predictions } from '@aws-amplify/predictions';
2
3const response = await Predictions.identify({
4 entities: {
5 source: {
6 key: pathToPhoto,
7 level: 'guest' | 'private' | 'protected', //optional, default is the configured on Storage category
8 },
9 }
10});
11console.log({ response });

The following options are independent of which source is specified. For demonstration purposes it will be used file but it can be used S3 Key as well.

Detecting bounding box of faces from an image with its landmarks (eyes, mouth, nose).

1import { Predictions } from '@aws-amplify/predictions';
2
3const { entities } = await Predictions.identify({
4 entities: {
5 source: {
6 file,
7 },
8 }
9})
10for (const { boundingBox, landmarks } of entities) {
11 const {
12 width, // ratio of overall image width
13 height, // ratio of overall image height
14 left, // left coordinate as a ratio of overall image width
15 top // top coordinate as a ratio of overall image height
16 } = boundingBox;
17
18 for(const landmark of landmarks) {
19 const {
20 type, // string "eyeLeft", "eyeRight", "mouthLeft", "mouthRight", "nose"
21 x, // ratio of overall image width
22 y // ratio of overall image height
23 } = landmark;
24 }
25}

Detecting celebrities on an image. It will return only celebrities the name and urls with related information.

1import { Predictions } from '@aws-amplify/predictions';
2
3const { entities } = await Predictions.identify({
4 entities: {
5 source: {
6 file,
7 },
8 celebrityDetection: true // boolean. It will only show detected celebrities
9 }
10})
11
12for(const { boundingBox, landmarks, metadata } of entities) {
13 const {
14 name,
15 urls
16 } = metadata; // celebrity info
17
18 // ...
19}
20.catch(err => console.log({ err }));

Detecting entities from previously uploaded images (e.g. Advanced Configuration for Identify Entities)

1import { Predictions } from '@aws-amplify/predictions';
2
3const { entities } = await Predictions.identify({
4 entities: {
5 source: {
6 file,
7 },
8 collection: true
9 }
10})
11
12for({ boundingBox, metadata } of entities) {
13 const {
14 width, // ratio of overall image width
15 height, // ratio of overall image height
16 left, // left coordinate as a ratio of overall image width
17 top // top coordinate as a ratio of overall image height
18 } = boundingBox;
19 const { externalImageId } = metadata; // this is the object key on S3 from the original image
20}