Page updated Jan 16, 2024

Identify text

The following APIs will allow you to identify text (words, tables, pages from a book) from an image.

Set up your backend

Run amplify add predictions, then use the following answers:

1? Please select from one of the categories below (Use arrow keys)
2 `Identify`
3? What would you like to identify? (Use arrow keys)
4 `Identify Text`
5? Provide a friendly name for your resource
6 `identifyText`
7? Would you also like to identify documents? (y/N)
8 `Y`
9? Who should have access? (Use arrow keys)
10 `Auth and Guest users`

Run amplify push to create the resources in the cloud.

Working with the API

Detect text in an image

Amplify will make calls to both Amazon Textract and Amazon Rekognition depending on the type of text you are looking to identify (i.e. image or document).

Passing in TextFormatType.PLAIN as the identify action will yield IdentifyResult, which must be cast into IdentifyTextResult. See below for an example of plain text detection from an image.

1public void detectText(Bitmap image) {
2 Amplify.Predictions.identify(
3 TextFormatType.PLAIN,
4 image,
5 result -> {
6 IdentifyTextResult identifyResult = (IdentifyTextResult) result;
7 Log.i("MyAmplifyApp", identifyResult.getFullText());
8 },
9 error -> Log.e("MyAmplifyApp", "Identify text failed", error)
10 );
11}
1fun detectText(image: Bitmap) {
2 Amplify.Predictions.identify(TextFormatType.PLAIN, image,
3 { result ->
4 val identifyResult = result as IdentifyTextResult
5 Log.i("MyAmplifyApp", identifyResult.fullText)
6 },
7 { Log.e("MyAmplifyApp", "Identify text failed", it) }
8 )
9}
1suspend fun detectText(image: Bitmap) {
2 try {
3 val result = Amplify.Predictions.identify(PLAIN, image)
4 val identifyResult = result as IdentifyTextResult
5 Log.i("MyAmplifyApp", identifyResult.fullText)
6 } catch (error: PredictionsResult) {
7 Log.e("MyAmplifyApp", "Identify text failed", error)
8 }
9}
1public void detectText(Bitmap image) {
2 RxAmplify.Predictions.identify(TextFormatType.PLAIN, image)
3 .subscribe(
4 result -> {
5 IdentifyTextResult identifyResult = (IdentifyTextResult) result;
6 Log.i("MyAmplifyApp", identifyResult.getFullText());
7 },
8 error -> Log.e("MyAmplifyApp", "Identify text failed", error)
9 );
10}

Detect text in a document

Passing in any other format of TextFormatType (i.e. FORM, TABLE or ALL) will yield IdentifyResult, which must be cast into IdentifyDocumentTextResult. See below for an example with TextFormatType.FORM for detecting forms from a document.

1public void detectText(Bitmap image) {
2 Amplify.Predictions.identify(
3 TextFormatType.FORM,
4 image,
5 result -> {
6 IdentifyDocumentTextResult identifyResult = (IdentifyDocumentTextResult) result;
7 Log.i("MyAmplifyApp", identifyResult.getFullText());
8 },
9 error -> Log.e("MyAmplifyApp", "Identify failed", error)
10 );
11}
1fun detectText(image: Bitmap) {
2 Amplify.Predictions.identify(TextFormatType.FORM, image,
3 { result ->
4 val identifyResult = result as IdentifyDocumentTextResult
5 Log.i("MyAmplifyApp", identifyResult.fullText)
6 },
7 { Log.e("MyAmplifyApp", "Identify failed", it) }
8 )
9}
1suspend fun detectText(image: Bitmap) {
2 try {
3 val result = Amplify.Predictions.identify(FORM, image)
4 val identifyResult = result as IdentifyDocumentTextResult
5 Log.i("MyAmplifyApp", identifyResult.fullText)
6 } catch (error: PredictionsException) {
7 Log.e("MyAmplifyApp", "Identify failed", error)
8 }
9}
1public void detectText(Bitmap image) {
2 RxAmplify.Predictions.identify(TextFormatType.FORM, image)
3 .subscribe(
4 result -> {
5 IdentifyDocumentTextResult identifyResult = (IdentifyDocumentTextResult) result;
6 Log.i("MyAmplifyApp", identifyResult.getFullText());
7 },
8 error -> Log.e("MyAmplifyApp", "Identify failed", error)
9 );
10}