Use AnalyzeDocument with an AWS SDK or CLI - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Use AnalyzeDocument with an AWS SDK or CLI

The following code examples show how to use AnalyzeDocument.

CLI
AWS CLI

To analyze text in a document

The following analyze-document example shows how to analyze text in a document.

Linux/macOS:

aws textract analyze-document \ --document '{"S3Object":{"Bucket":"bucket","Name":"document"}}' \ --feature-types '["TABLES","FORMS"]'

Windows:

aws textract analyze-document \ --document "{\"S3Object\":{\"Bucket\":\"bucket\",\"Name\":\"document\"}}" \ --feature-types "[\"TABLES\",\"FORMS\"]" \ --region region-name

Output:

{ "Blocks": [ { "Geometry": { "BoundingBox": { "Width": 1.0, "Top": 0.0, "Left": 0.0, "Height": 1.0 }, "Polygon": [ { "Y": 0.0, "X": 0.0 }, { "Y": 0.0, "X": 1.0 }, { "Y": 1.0, "X": 1.0 }, { "Y": 1.0, "X": 0.0 } ] }, "Relationships": [ { "Type": "CHILD", "Ids": [ "87586964-d50d-43e2-ace5-8a890657b9a0", "a1e72126-21d9-44f4-a8d6-5c385f9002ba", "e889d012-8a6b-4d2e-b7cd-7a8b327d876a" ] } ], "BlockType": "PAGE", "Id": "c2227f12-b25d-4e1f-baea-1ee180d926b2" } ], "DocumentMetadata": { "Pages": 1 } }

For more information, see Analyzing Document Text with Amazon Textract in the Amazon Textract Developers Guide

Java
SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

import software.amazon.awssdk.core.SdkBytes; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.textract.TextractClient; import software.amazon.awssdk.services.textract.model.AnalyzeDocumentRequest; import software.amazon.awssdk.services.textract.model.Document; import software.amazon.awssdk.services.textract.model.FeatureType; import software.amazon.awssdk.services.textract.model.AnalyzeDocumentResponse; import software.amazon.awssdk.services.textract.model.Block; import software.amazon.awssdk.services.textract.model.TextractException; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class AnalyzeDocument { public static void main(String[] args) { final String usage = """ Usage: <sourceDoc>\s Where: sourceDoc - The path where the document is located (must be an image, for example, C:/AWS/book.png).\s """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String sourceDoc = args[0]; Region region = Region.US_EAST_2; TextractClient textractClient = TextractClient.builder() .region(region) .build(); analyzeDoc(textractClient, sourceDoc); textractClient.close(); } public static void analyzeDoc(TextractClient textractClient, String sourceDoc) { try { InputStream sourceStream = new FileInputStream(new File(sourceDoc)); SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream); // Get the input Document object as bytes Document myDoc = Document.builder() .bytes(sourceBytes) .build(); List<FeatureType> featureTypes = new ArrayList<FeatureType>(); featureTypes.add(FeatureType.FORMS); featureTypes.add(FeatureType.TABLES); AnalyzeDocumentRequest analyzeDocumentRequest = AnalyzeDocumentRequest.builder() .featureTypes(featureTypes) .document(myDoc) .build(); AnalyzeDocumentResponse analyzeDocument = textractClient.analyzeDocument(analyzeDocumentRequest); List<Block> docInfo = analyzeDocument.blocks(); Iterator<Block> blockIterator = docInfo.iterator(); while (blockIterator.hasNext()) { Block block = blockIterator.next(); System.out.println("The block type is " + block.blockType().toString()); } } catch (TextractException | FileNotFoundException e) { System.err.println(e.getMessage()); System.exit(1); } } }
  • For API details, see AnalyzeDocument in AWS SDK for Java 2.x API Reference.

Python
SDK for Python (Boto3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

class TextractWrapper: """Encapsulates Textract functions.""" def __init__(self, textract_client, s3_resource, sqs_resource): """ :param textract_client: A Boto3 Textract client. :param s3_resource: A Boto3 Amazon S3 resource. :param sqs_resource: A Boto3 Amazon SQS resource. """ self.textract_client = textract_client self.s3_resource = s3_resource self.sqs_resource = sqs_resource def analyze_file( self, feature_types, *, document_file_name=None, document_bytes=None ): """ Detects text and additional elements, such as forms or tables, in a local image file or from in-memory byte data. The image must be in PNG or JPG format. :param feature_types: The types of additional document features to detect. :param document_file_name: The name of a document image file. :param document_bytes: In-memory byte data of a document image. :return: The response from Amazon Textract, including a list of blocks that describe elements detected in the image. """ if document_file_name is not None: with open(document_file_name, "rb") as document_file: document_bytes = document_file.read() try: response = self.textract_client.analyze_document( Document={"Bytes": document_bytes}, FeatureTypes=feature_types ) logger.info("Detected %s blocks.", len(response["Blocks"])) except ClientError: logger.exception("Couldn't detect text.") raise else: return response
  • For API details, see AnalyzeDocument in AWS SDK for Python (Boto3) API Reference.

SAP ABAP
SDK for SAP ABAP
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

"Detects text and additional elements, such as forms or tables," "in a local image file or from in-memory byte data." "The image must be in PNG or JPG format." "Create ABAP objects for feature type." "Add TABLES to return information about the tables." "Add FORMS to return detected form data." "To perform both types of analysis, add TABLES and FORMS to FeatureTypes." DATA(lt_featuretypes) = VALUE /aws1/cl_texfeaturetypes_w=>tt_featuretypes( ( NEW /aws1/cl_texfeaturetypes_w( iv_value = 'FORMS' ) ) ( NEW /aws1/cl_texfeaturetypes_w( iv_value = 'TABLES' ) ) ). "Create an ABAP object for the Amazon Simple Storage Service (Amazon S3) object." DATA(lo_s3object) = NEW /aws1/cl_texs3object( iv_bucket = iv_s3bucket iv_name = iv_s3object ). "Create an ABAP object for the document." DATA(lo_document) = NEW /aws1/cl_texdocument( io_s3object = lo_s3object ). "Analyze document stored in Amazon S3." TRY. oo_result = lo_tex->analyzedocument( "oo_result is returned for testing purposes." io_document = lo_document it_featuretypes = lt_featuretypes ). LOOP AT oo_result->get_blocks( ) INTO DATA(lo_block). IF lo_block->get_text( ) = 'INGREDIENTS: POWDERED SUGAR* (CANE SUGAR,'. MESSAGE 'Found text in the doc: ' && lo_block->get_text( ) TYPE 'I'. ENDIF. ENDLOOP. MESSAGE 'Analyze document completed.' TYPE 'I'. CATCH /aws1/cx_texaccessdeniedex. MESSAGE 'You do not have permission to perform this action.' TYPE 'E'. CATCH /aws1/cx_texbaddocumentex. MESSAGE 'Amazon Textract is not able to read the document.' TYPE 'E'. CATCH /aws1/cx_texdocumenttoolargeex. MESSAGE 'The document is too large.' TYPE 'E'. CATCH /aws1/cx_texhlquotaexceededex. MESSAGE 'Human loop quota exceeded.' TYPE 'E'. CATCH /aws1/cx_texinternalservererr. MESSAGE 'Internal server error.' TYPE 'E'. CATCH /aws1/cx_texinvalidparameterex. MESSAGE 'Request has non-valid parameters.' TYPE 'E'. CATCH /aws1/cx_texinvalids3objectex. MESSAGE 'Amazon S3 object is not valid.' TYPE 'E'. CATCH /aws1/cx_texprovthruputexcdex. MESSAGE 'Provisioned throughput exceeded limit.' TYPE 'E'. CATCH /aws1/cx_texthrottlingex. MESSAGE 'The request processing exceeded the limit.' TYPE 'E'. CATCH /aws1/cx_texunsupporteddocex. MESSAGE 'The document is not supported.' TYPE 'E'. ENDTRY.
  • For API details, see AnalyzeDocument in AWS SDK for SAP ABAP API reference.