Page updated Jan 16, 2024

Identify text

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? (Use arrow keys)
2❯ Identify Text
3 Identify Entities
4 Identify Labels
5 Learn More
6
7? Would you also like to identify documents? Yes
8? Who should have access? Auth and Guest users

Now run amplify push which will generate your aws-exports.js 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 (default for plain text) and Amazon Textract (default for documents)

Working with the API

Detect text in an input image. Input can be sent directly from the browser or an Amazon S3 key from project bucket.

1Predictions.identify({
2 text: {
3 source: {
4 file
5 }
6 }
7})
8.then(response => console.log({ response }))
9.catch(err => console.log({ err }));

Identify image stored in Amazon S3

1Predictions.identify({
2 text: {
3 source: {
4 key: pathToPhoto,
5 level?: 'public | private | protected', //optional, default is the configured on Storage category
6 }
7 }
8})
9.then((response) => console.log({ response }))
10.catch(err => console.log({ err }));

The following options are independent of which source is specified. For demonstration purposes we will reference a file but it can be an S3 Key as well. Predictions.identify({text : {...}}) can detect unstructured text PLAIN, structured text from tables TABLE or text from forms FORM.

Identify plain text

For detecting plain text, you can see the whole detected text, the lines detected, the position of each line of text, and each word.

1Predictions.identify({
2 text: {
3 source: {
4 file
5 },
6 format: "PLAIN",
7 }
8})
9.then(response => {
10 const {
11 text: {
12 fullText, // String
13 lines, // Array of String ordered from top to bottom
14 linesDetailed: [
15 {
16 /* array of
17 text, // String
18 boundingBox: {
19 width, // ratio of overall image width
20 height, // ratio of overall image height
21 left, // left coordinate as a ratio of overall image width
22 top // top coordinate as a ratio of overall image height
23 },
24 polygon // Array of { x, y } coordinates as a ratio of overall image width and height
25 */
26 }
27 ],
28 words // Array of objects that contains { text, boundingBox, polygon}
29 }
30 } = response
31})
32.catch(err => console.log({ err }));

Identify structured forms

For detecting structured forms (documents, tables, etc.) from an image, keyValues will return a string of the entity found in the image as well as metadata such as selected checkboxes or the relative location in the image using a boundingBox.

1Predictions.identify({
2 text: {
3 source: {
4 file
5 },
6 format: "FORM",
7 }
8})
9.then(response => {
10 const {
11 text: {
12 // same as PLAIN +
13 keyValues // Array of { key (String), value: { text (String), selected (boolean)}, polygon, boundingBox }
14 }
15 } = response
16})
17.catch(err => console.log({ err }));

For example the below image would return keyValues with "Test" or "Checked" as a key, and true since they are selected. The location of these elements would be returned in the boundingBox value.

Image

Identify structured tables

For detecting structured tables from an image

1Predictions.identify({
2 text: {
3 source: {
4 file
5 },
6 format: "TABLE",
7 }
8})
9.then(response => {
10 const {
11 text: {
12 // same as PLAIN +
13 tables : [
14 {
15 size: { rows, columns },
16 table // Matrix Array[ Array ] of size rows
17 // each element of the array contains { text, boundingBox, polygon, selected, rowSpan, columnSpan}
18 }
19 ]
20 }
21 } = response
22})
23.catch(err => console.log({ err }));

For detecting tables and forms on the image just select format "ALL"

1Predictions.identify({
2 text: {
3 source: {
4 file
5 },
6 format: "ALL",
7 }
8})
9.then(response => {
10 const {
11 text: {
12 // same as PLAIN + FORM + TABLE
13 }
14 } = response
15})
16.catch(err => console.log({ err }));

Identify Entities from a photo

Setup your backend with the following: 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 aws-exports.js 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.

For application users, when they upload images with Storage.put() they will specify a prefix which the Lambda functions perform indexing like so:

1Storage.put('test.jpg', file, {
2 level: 'protected',
3 customPrefix: {
4 protected: 'protected/predictions/index-faces/',
5 }
6});

In the sample React application code below, you will see that to use this functionality you will need to set collection:true when calling Predictions.identify() and remove celebrityDetection: true. The flow is that you will first upload an image to S3 with the PredictionsUpload function (which is connected to a button in the app) 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.