---
title: "Extracting Content with AWS OCR Service Textract"
description: "Stop stressing your eyes, and learn how to extract content from images with AWS OCR Textract Service in this step-by-step tutorial!"
canonical: "https://adamtheautomator.com/aws-ocr/"
---

# Extracting Content with AWS OCR Service Textract

> Stop stressing your eyes, and learn how to extract content from images with AWS OCR Textract Service in this step-by-step tutorial!

Source: https://adamtheautomator.com/aws-ocr/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Extracting Content with AWS OCR Service Textract](https://adamtheautomator.com/wp-content/uploads/2022/04/Extracting-Content-with-AWS-OCR-Service-Textract.jpg)

# Extracting Content with AWS OCR Service Textract

[![](https://secure.gravatar.com/avatar/31b81fca75dbee0e688880e229577076d71f16e2a297c257566284fcf416c7d5?s=192&d=mm&r=g)Victory Nwani](https://adamtheautomator.com/author/victory-nwani/)20 April 20227 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[AWS](/tag/aws/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Extracting Text from an Image Document](#extracting-text-from-an-image-document)
*   [Extracting Content from an Expense Document](#extracting-content-from-an-expense-document)
*   [Extracting Data with AWS OCR Textract via the AWS Console](#extracting-data-with-aws-ocr-textract-via-the-aws-console)
*   [Conclusion](#conclusion)

Are you tired of eyeballing through images to extract information? Undoubtedly, the manual process can be a pain. So why not go with Optical Character Recognition (OCR) server, like [AWS OCR Textract](https://docs.aws.amazon.com/textract/latest/dg/what-is.html)?

AWS OCR Textract analyzes texts and even handwritings in images and automatically converts them to machine-readable text. And in this tutorial, you’ll learn to take advantage of the AWS OCR Textract service and be more productive when extracting information from images.

Ready? Read on and let AWS OCR Textract do the extracting!

## Prerequisites

This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have the following:

*   An AWS account – A [free tier](https://aws.amazon.com/free/?all-free-tier.sort-by=item.additionalFields.SortRank&all-free-tier.sort-order=asc&awsf.Free%20Tier%20Types=*all&awsf.Free%20Tier%20Categories=*all) account is \]available.
*   [AWS CLI](https://aws.amazon.com/cli/) installed and configured on your computer – The tutorial uses AWS CLI 2.3.6 version.
*   [JQ](https://stedolan.github.io/jq/download/) installed on your computer – The tutorial uses JQ CLI tool 1.6.

## Extracting Text from an Image Document

In the past, OCR was commonly used by scanners to extract characters while scanning through documents. But now, OCR is also used by applications to extract data from image documents, such as ID cards, invoices, and receipts.

You can use the AWS OCR Textract service through the AWS Console, AWS CLI, [Textract API](https://docs.aws.amazon.com/textract/latest/dg/API_Reference.html), and even programmatically through supported client SDKs. But in this tutorial, you’ll extract content from images via the AWS CLI.

> _AWS Free Tier allows you to analyze 1000 pages per month for free. And since Textract is offered through AWS public cloud as a managed service, Textract provides more benefits over other OCR services._

1\. Launch your computer’s terminal and execute the command below to create (`mkdir`) and change (`cd`) into a new directory. You can name the directory as you prefer, but the directory is called _textract-extraction_ in this demo.

The new directory will contain the images whose text you will extract using Textract.

```bash
mkdir textract-extraction && cd textract-extraction
```

2\. Next, execute the [`wget`](https://linuxize.com/post/wget-command-examples/) command below to download a public image named `work-foodsign.jpg`. The image has a handwritten sentence, which you’ll extract using the Textract service.

```bash
wget https://raw.githubusercontent.com/adam-the-automator/Scripts/main/aws-ocr/Job-Search-2.jpg
```

Related:[How to Download Files with Python Wget](https://adamtheautomator.com/python-wget/)

3\. Open the image in your media viewer to view the image and confirm the handwritten text.

![Viewing the Downloaded Public Image. Source: https://www.uhaul.com/Blog/2014/03/22/finding-a-job-in-a-new-city/](https://adamtheautomator.com/wp-content/uploads/2022/04/image-scaled.jpeg)

Viewing the Downloaded Public Image. Source: [https://www.uhaul.com/Blog/2014/03/22/finding-a-job-in-a-new-city/](https://www.uhaul.com/Blog/2014/03/22/finding-a-job-in-a-new-city/)

4\. Now, execute the [`textract`](https://docs.aws.amazon.com/cli/latest/reference/textract/index.html) command below to extract (`[detect-document-text](https://docs.aws.amazon.com/cli/latest/reference/textract/detect-document-text.html))` the text from the `work-foodsign.jpg` image and outputs the extracted data in JSON format (`--output json`).

The –document flag contains a list with a Byte whose value is the base64 representation of the work-foodsign.jpg file.

> _Textract’s_ [_DetectDocumentText_](https://docs.aws.amazon.com/cli/latest/reference/textract/detect-document-text.html) _operation analyzes a document and extracts all characters._

```bash
aws textract detect-document-text --document Bytes=$( base64 ./work-foodsign.jpg ) --output json 
```

> _There is no need to manage an additional infrastructure for OCR. Powerful instances are used and managed by AWS, reducing extraction time._

Once the command completes, you’ll see the printed JSON output below, which contains the following fields:

*   A **DocumentMetadata** object – Contains a **Pages** field to indicate how many pages Textract scanned from the document.
    
*   A **Blocks** array – Contains objects that hold each layer’s data scanned from the input document.
    
*   **DetectDocumentTextModelVersion** – contains a value that specifies the version of the text model.
    

The Pages object below shows the geometric properties of the items detected within the document. As you see, the Geometry object contains the BoundingBox object and Polygon array, which contains values that describe the shape of the scanned image document.

Note the first highlighted value within the IDs array, as you will use it to identify the next layer scanned in the document.

![Viewing the first objects within the JSON output of the Job-Search-2.jpg image](https://adamtheautomator.com/wp-content/uploads/2022/04/image-305.png)

Viewing the first objects within the JSON output of the Job-Search-2.jpg image

5\. Finally, scroll down the JSON output until you reach the next object within the **Blocks** array.

The next object has a BlockType set to LINE, which indicates the result of the first line scanned in the image. The value of the highlighted Ids field (scanned line’s ID) corresponds with the first value that you noted previously (step four).

At this point, you can scroll further down the JSON output to find objects containing other lines scanned from the image document.

![Viewing the JSON object within the second detection layer of the Job-Search-2.jpg image](https://adamtheautomator.com/wp-content/uploads/2022/04/image-306.png)

Viewing the JSON object within the second detection layer of the Job-Search-2.jpg image

## Extracting Content from an Expense Document

You’ve previously extracted all text from an image document, and that’s cool. But what if you need to extract more useful information instead of all texts? The `analyze-expense` ( [AnalyzeExpense](https://docs.aws.amazon.com/cli/latest/reference/textract/analyze-expense.html) operation) analyzes and extracts expense-focused contents in an image document, like receipts.

To see how the `analyze-expense` command works, you’ll extract a sample invoice then filter the output using [JQ](https://stedolan.github.io/jq/). JQ is a command-line tool used for working with JSON data.

> _Textract returns a large amount of data from a document scan that requires a tool such as JQ to retrieve only useful values from the JSON output. These useful values can be but are not limited to, `Text`, `ValueDetection`, or `Confidence`._

1\. Run the `wget` command below to download another image (`sample-reciept.png`) that contains a printed invoice.

```bash
wget https://raw.githubusercontent.com/adam-the-automator/Scripts/main/aws-ocr/sample-reciept.png
```

2\. Open the image in your media viewer to view the image and the figures it contains.

![Viewing a downloaded boutique invoice. Source: makereceipt.com](https://adamtheautomator.com/wp-content/uploads/2022/04/image-307.png)

Viewing a downloaded boutique invoice. Source: [makereceipt.com](https://makereceipt.com/)

3\. Execute the command below to analyze and extract the invoice figures (`analyze-expense`) from the image document (`sample-reciept.png`).

The parameters in the below command are similar to the previous detect-document-text command in the “Extracting Text from an Image Document” section (step four). But this time, the command stores the JSON output to a JSON file (–output json > expense-analysis.json).

```bash
aws textract analyze-expense --document Bytes=$( base64 ./sample-reciept.png ) --output json > expense-analysis.json
```

The root element of the JSON output from the analyze-expense command contains two fields:

*   A `DocumentMetadata` object – Contains keys with details of the document that Textract scanned, such as the Pages key.
    
*   An `ExpenseDocuments` array – Contains objects with the financial relationships analyzed from the document. The `ExpenseDocuments` array contains a `SummaryFields` array with objects that hold a summary of the invoice, such as the invoice name and address.
    

The ExpenseDocuments array also contains a LineItemsGroups array with objects that hold the document’s financial relationships, like items purchased and their prices.

4\. Now, execute the `jq` command below to retrieve the first object (`.ExpenseDocuments[0]`) within the `SummaryFields[0]` array from the `expense-analysis.json` file.

The first object is expected to be the shop’s name (Boutique) as it’s the first item on the invoice.

```bash
jq '.ExpenseDocuments[0].SummaryFields[0]' expense-analysis.json
```

The JSON response below shows a Type object specifying VENDOR\_NAME as the object’s type and a ValueDetection object with Boutique as the Text value. At the same time, the Geometry object contains an object and array that describes the line’s shape.

![Viewing the summary-fields object filtered using JQ from the boutique invoice](https://adamtheautomator.com/wp-content/uploads/2022/04/image-308.png)

Viewing the summary-fields object filtered using JQ from the boutique invoice

5\. Finally, execute the `jq` command below against the `expense-analysis.json` file to retrieve an object’s `Type` and `ValueDetection` keys.

The total cost of goods or services rendered is important data included in all invoices. Textract includes the total cost in an object within the SummaryFields array.

The objects in the SummaryFields array are piped (|) to the JQ .select function until it finds an object whose Text property value is TOTAL (select(.Type.Text == “TOTAL”).

```bash
jq '.ExpenseDocuments[0].SummaryFields[] | select(.Type.Text == "TOTAL") | {Type, ValueDetection }' expense-analysis.json
```

## Extracting Data with AWS OCR Textract via the AWS Console

By now, you already have a sound understanding of how to extract content via the AWS CLI. But did you know AWS Console lets you perform the same tasks in a GUI? The AWS console provides more features when using Textract, as you can natively search, filter, and download a ZIP file of the scan result.

> _Remember that you can only perform either the [AnalyzeDocument](https://docs.aws.amazon.com/cli/latest/reference/textract/analyze-document.html), [AnalyzeIdentity](https://docs.aws.amazon.com/cli/latest/reference/textract/analyze-id.html), or [AnalyzeEx](https://docs.aws.amazon.com/cli/latest/reference/textract/analyze-expense.html)[pense](https://docs.aws.amazon.com/cli/latest/reference/textract/analyze-expense.html) operations in AWS Console._

1\. Execute the `wget` command below to download an image (`sample-journal.png`) that contains a journal. You’ll extract the image file’s raw text and table via the AWS console.

```bash
wget https://raw.githubusercontent.com/adam-the-automator/Scripts/main/aws-ocr/sample-journal.jpg
```

2\. Next, open your favorite web browser and log in to the [AWS Console](https://aws.amazon.com/console/).

Search textract, and select the Amazon Textract service from the result, as shown below. Your browser redirects the page to the Amazon Textract dashboard (step three).

![Searching for the Textract service on AWS console](https://adamtheautomator.com/wp-content/uploads/2022/04/image-309.png)

Searching for the Textract service on AWS console

3\. Click the **Try Amazon Textract** button to use Textract via the AWS Console. You’ll get to a page where you can select documents to extract content from.

![Viewing the Textract service page](https://adamtheautomator.com/wp-content/uploads/2022/04/image-310.png)

Viewing the Textract service page

4\. Now, click the **Choose File** button and select the _sample-journal.png_ file you downloaded in step one.

The gray characters at the left section highlight all scanned characters, while a preview of the results is shown in the right section. Although the right section previews all raw text extracted, you can further filter the results (step five).

![Viewing the RAW text output of the sample-journal image](https://adamtheautomator.com/wp-content/uploads/2022/04/image-311.png)

Viewing the RAW text output of the sample-journal image

5\. Click the **Tables** tab at the right section to view only the tabular characters extracted from the tables within the image document.

![Viewing the Textract filter tabs](https://adamtheautomator.com/wp-content/uploads/2022/04/image-312.png)

Viewing the Textract filter tabs

Below, you can see the Tables tab displays a preview of the tabular data extracted from the input document.

![Viewing the table data extracted from the sample-journal.png image document](https://adamtheautomator.com/wp-content/uploads/2022/04/image-313.png)

Viewing the table data extracted from the _sample-journal.png_ image document

6\. Finally, click the **Download** **results** button to download a ZIP file containing all information extracted from the image document (`sample-journal.png`).

![Downloading the Textract results in a ZIP file](https://adamtheautomator.com/wp-content/uploads/2022/04/image-314.png)

Downloading the Textract results in a ZIP file

The files included in the ZIP file are:

*   A \*_.json_ file – Contains the result of the AnalyzeDocument operation in a JSON format.
    
*   A \*_.txt_ file – Contains all characters scanned from the image document.
    
*   Two \*_.csv_ files – Contain the key-value and table data filtered out of the image document.
    

## Conclusion

This tutorial aimed to teach you how AWS OCR Textract can lessen manual tasks. You’ve learned to extract content from different image documents, from expense to tabular-focused data. As you’ve realized, the Textract service lets you choose either method (AWS CLI or AWS Console) when extracting content.

At this point, through the AWS Console, you can now analyze image documents right from the convenience of your mobile device or tablet.

Since you’ve only extracted contents from image documents locally, why not try uploading a file to an [S3](https://adamtheautomator.com/upload-file-to-s3/) bucket and use that file as the input document for Textract?

Related:[How To Upload Local Files To S3 With The AWS CLI](https://adamtheautomator.com/upload-file-to-s3/)

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Faws-ocr%2F&text=Extracting%20Content%20with%20AWS%20OCR%20Service%20Textract)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Faws-ocr%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Faws-ocr%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2022/05/HMaster-Your-Data-with-AWS-Quicksight.jpg)

### [Make Data-Driven Decisions with AWS QuickSight Analytics](/aws-quicksight/)

Struggling to make sense of your data? Discover how AWS QuickSight transforms data into actionable insights for smarter decision-making.

![](https://adamtheautomator.com/wp-content/uploads/2023/11/aws-network-access-analyzer-2.jpg)

### [Getting Started with AWS Network Access Analyzer](/aws-network-access-analyzer/)

Unlock the power of AWS Network Access Analyzer. Master network security with step-by-step guidance in this tutorial today!

![](https://adamtheautomator.com/wp-content/uploads/2023/11/ngrok-remote.jpg)

### [Hosting an Ngrok Remote Web App on AWS](/ngrok-remote/)

Learn how to run the Ngrok remote web application on AWS with the FastAPI Python endpoint in Linux through this ATA Learning tutorial!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
