DEV Community

Cover image for Getting started with Optical Character Recognition using Python and AWS

Getting started with Optical Character Recognition using Python and AWS

sehmimhaque profile image sehmimhaque ・3 min read

In this blog we will use AWS Textract to scan and extract the texts of a document from a picture and get a JSON output response. We will also use AWS lambda function with Python to build a backend.

Alt Text

If you've read my previous blog Serverless AWS Textract Document Scanner you can see that we created an endpoint to run AWS Textract using Node.js. There were however, a few problems that I ran into that only got fixed by switching to python. For example, one of the biggest problems I faced while using node.js to run my app was the request time being too long. Sometimes it would take up to 30 seconds for a response finish. As you can tell, that is terrible. So I decided to switch my stack from node.js to Python and now the average time to run requests dropped from 25s to 2s. Pretty nice right? I know. Let's see how we can do this.

1. Setting up Backend with Serverless using Python

Assuming you already know how serverless works, we can continue with AWS Textract and the flow it follows. If you're not familiar with serverless with node please don't jump the gun, go checkout some tuts here.

Okay. Let's quickly setup our serverless

sls create --template aws-python --path myService
Enter fullscreen mode Exit fullscreen mode

Make sure you have the following dependencies installed.

  1. boto3
  2. json


Something things to keep in mind before continuing

  • Make sure you have proper authorization for this task,
  • check your region and
  • make sure the bucket url is accurate.

2. Now once AWS SDK is configured, we can write code for Textract

import json
import boto3

def textractAnalyzer(event, context):
    document = json.loads(event['body'])['fileKey']
    client = boto3.client('textract')

    #process using S3 object
    response = client.detect_document_text(
        Document={'S3Object': {'Bucket': bucket, 'Name': document}})

    #Get the text blocks

    # All Text By Line
    texts_by_line = dataPurifierByLine(blocks)

    return {
        'statusCode': 200,
        'body': json.dumps({
            "fileKey": document,
            "textByLine": texts_by_line,
            "texTractblocks" : blocks ## Full response from textract

def dataPurifierByLine(blocks):
    result = []
    for block in blocks:
        if block['BlockType'] == "LINE":
            entry = {
                "line": block['Text'],
                "confidence": block['Confidence']
    return result
Enter fullscreen mode Exit fullscreen mode

The following code finds a file with the specific key in s3/public/** and then runs Textract analysis on it.

3. Deploy the Code

sls deploy
find the endpoint, for me it looks like this
alt text

4. For our next step, we will drop a file manually on the bucket so we can use it for testing.

  1. Go to S3,
  2. the navigate to /public
  3. and then upload a img file

alt text

Im using this old receipt

alt text

5. Finally, Test it on post man.


    "fileKey" : "public/demo.jpeg"
Enter fullscreen mode Exit fullscreen mode
  • If it gives you timeout error, change the function time out to 30s on .yml file.
  • You can see the type of data we get back. For this demo I'm gonna take every line and add them together in an array.

Your response should look something like this

    "fileKey": "public/demo.jpeg",
    "textByLine": [
            "line": "01/027 APPROVED - THANK YOU",
            "confidence": 99.5232162475586
    "texTractblocks": [
            "BlockType": "PAGE",
            "Geometry": {
                "BoundingBox": {
                    "Width": 0.8844140768051147,
                    "Height": 0.8354079723358154,
                    "Left": 0.048781704157590866,
                    "Top": 0.15526676177978516
                "Polygon": [
                        "X": 0.07131516188383102,
                        "Y": 0.1597394049167633
                        "X": 0.9331957697868347,
                        "Y": 0.15526676177978516
                        "X": 0.9245083928108215,
                        "Y": 0.9906747341156006
                        "X": 0.048781704157590866,
                        "Y": 0.9588059782981873
            "Id": "9b384b8d-dcb8-4596-8511-af18659a9787",
            "Relationships": [
                    "Type": "CHILD",
                    "Ids": [
Enter fullscreen mode Exit fullscreen mode

That's it!

Next Step

Next week I will continue on with this app and build a front end for it using Flutter and AWS Amplify.

  • We will setup AWS Amplify suing Flutter,
  • Setup our camera to take pictures.
  • Once that's done we will confirm and send the picture to the S3 store,
  • Which will trigger our lambda function and send us the response back to our front-end.

Discussion (0)

Editor guide