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:
? What would you like to identify? Identify Text ❯ Identify Entities Identify Labels Learn More
? Would you like use the default configuration? Default Configuration
? 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:
- Administrators provide images to be indexed from an S3 bucket
- 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
):
? What would you like to identify? Identify Entities? Would you like use the default configuration? Advanced Configuration? Would you like to enable celebrity detection? Yes? Would you like to identify entities from a collection of images? Yes? How many entities would you like to identify 50? Would you like to allow users to add images to this collection? Yes? Who should have access? Auth and Guest users? 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)
import { Predictions } from '@aws-amplify/predictions';
const response = await Predictions.identify({ entities: { source: { file, }, }});console.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.
import { Predictions } from '@aws-amplify/predictions';
const response = await Predictions.identify({ entities: { source: { bytes: imageArrayBuffer, }, }});console.log({ response });
From Amazon S3 key
import { Predictions } from '@aws-amplify/predictions';
const response = await Predictions.identify({ entities: { source: { key: pathToPhoto, level: 'guest' | 'private' | 'protected', //optional, default is the configured on Storage category }, }});console.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).
import { Predictions } from '@aws-amplify/predictions';
const { entities } = await Predictions.identify({ entities: { source: { file, }, }})for (const { boundingBox, landmarks } of entities) { const { width, // ratio of overall image width height, // ratio of overall image height left, // left coordinate as a ratio of overall image width top // top coordinate as a ratio of overall image height } = boundingBox; for(const landmark of landmarks) { const { type, // string "eyeLeft", "eyeRight", "mouthLeft", "mouthRight", "nose" x, // ratio of overall image width y // ratio of overall image height } = landmark; }}
Detecting celebrities on an image. It will return only celebrities the name and urls with related information.
import { Predictions } from '@aws-amplify/predictions';
const { entities } = await Predictions.identify({ entities: { source: { file, }, celebrityDetection: true // boolean. It will only show detected celebrities }})
for(const { boundingBox, landmarks, metadata } of entities) { const { name, urls } = metadata; // celebrity info // ...}.catch(err => console.log({ err }));
Detecting entities from previously uploaded images (e.g. Advanced Configuration for Identify Entities)
import { Predictions } from '@aws-amplify/predictions';
const { entities } = await Predictions.identify({ entities: { source: { file, }, collection: true }})
for({ boundingBox, metadata } of entities) { const { width, // ratio of overall image width height, // ratio of overall image height left, // left coordinate as a ratio of overall image width top // top coordinate as a ratio of overall image height } = boundingBox; const { externalImageId } = metadata; // this is the object key on S3 from the original image}