Compare Amazon Textract with Tesseract OCR — OCR & NLP Use Case
By: Manoj Kukreja
Tags: Machine Learning, Data Science, NLP, Artificial Intelligence, AWS

Compare Amazon Textract with Tesseract OCR — OCR & NLP Use Case

Comparison of two known engines for optical character recognition (OCR) and Naturtal Language Processing

Image by Felix Wolf from Pixabay

What is OCR anyway and why the buzz? Artificial Intelligence (AI) enables entities with Human Intelligence (us) process data at a large scale — faster and cheaper. Unarguably, a large portion of data is saved digitally- easy to read and analyze. However there is a significant portion of data that is stored in physical documents — both type written and hand-written. How to analyze this category of data. This is where fascinating technology of Optical Character Recognition (OCR) comes in. Using OCR you are able to convert documents into text format of data suitable for editing and searching. This is what OCR is able to do.

Image by Gerd Altmann from Pixabay

In the article we will focus on two well know OCR frameworks:

Tesseract OCR — free software, released under the Apache License, Version 2.0 - development has been sponsored by Google since 2006. Amazon Textract OCR — fully managed service from Amazon, uses machine learning to automatically extract text and data

We will compare the OCR capabilities of these two frameworks. Let's start by a simple image as below:

Image by Author — typewritten.jpg

$ git clone https://github.com/mkukreja1/blogs.git

Download and Install Notebook blogs/ocr/OCR.ipynb

!pip install opencv-python

!pip install pytesseract

!pip install pyenchant import cv2

import pytesseract

import re

from pytesseract import Output img_typewritten = cv2.imread('typewritten.jpg')

custom_config = r'--oem 3 --psm 6'

txt_typewritten=pytesseract.image_to_string(img_typewritten, config=custom_config)

print(txt_typewritten)

OCR Output using Tesseract OCR:

BEST PICTURE FORD V FERRARI

THE IRISHMAN

JOJO RABBIT

JOKER

LITTLE WOMEN

MARRIAGE STORY

1917

ONCE UPON A TIME…IN HOLLYWOOD

PARASITE

OCR Output using Amazon Textract OCR:

Both frameworks performed exactly the same. Lets see how handwritten text compares.

Image by Author — handwritten.jpg

img_handwritten = cv2.imread('handwritten.jpg')

txt_handwritten=pytesseract.image_to_string(img_handwritten)

print(txt_handwritten)

OCR Output using Tesseract OCR:

<>

mhassadey

VWENS YEA) sore

&&. a)

OW!NS ané

Lp Real Estate Group

RKSHIRE | Ambassador

H ATH. AWAY | Real Estate

lomeServices

Ky Ie,

aa Nim So mul for meet

With me today | Ht wis qed Catching

Wp and Vin glad 10 hear Mk Ke well

for bu. | Lovie Brwoud 10 Seeing You

AA aiv Go| Cheers|

Megan Owens, Realtor 402–689–4984 www.ForSalebyMegan.

MMS

com

OCR Output using Amazon Textract OCR:

Image by Author

Amazon Textract OCR performed marginally better than Tesseract OCR for handwritten text.

Now we will try a busy image.

Image by Author — invoice-sample.jpg

img_invoice = cv2.imread('invoice-sample.jpg')

custom_config = r'--oem 3 --psm 6'

txt_invoice=pytesseract.image_to_string(img_invoice, config=custom_config)

print(txt_invoice)

OCR Output using Tesseract OCR:

http://mrsinvoice.com

I 7

Your Company LLC Address 123, State, My Country P 111–222–333, F 111–222–334

BILL TO:

P: 111–222–333, F: 111–222–334 a. z

cient@eromplent

Contact Phone 101–102–103

john Doe office ayment Terms ‘ash on Delivery

Office Road 38

P: 111–333–222, F: 122–222–334 Amount Due: $4,170

office@example.net

NO PRODUCTS / SERVICE QUANTITY / RATE / UNIT AMOUNT

HOURS: PRICE

1 aye 2 $20 $40

2 | Steering Wheel 5 $10 $50

3 | Engine oil 10 $15 $150

4 | Brake Pad 24 $1000 $2,400

Subtotal $275

Tax (10%) $27.5

Grand Total $302.5

‘THANK YOU FOR YOUR BUSINESS

OCR Output using Amazon Textract OCR:

Image By Author

Amazon Textract identifies tables and forms in documents. This is neat.

Image By Author

Advanced Features — Spell Checking

Results from an OCR scan are often fed into an NLP model. Therefore, it is important to have a high degree of accuracy of the resulting text. We can handle it two ways:

Pass every work through a spell-checker module like enchant

Option 1 — if spell check failed — Mask/remove the word from the resulting text

Option 2 — if spell check failed — Use spell-checker suggestions and edit the resulting text

Enchant module is very frequently used in Python to check the spelling of words based on dictionary. In addition to spell checking enchant can give suggestions to correct words.

img = cv2.imread('invoice-sample.jpg')

text = pytesseract.image_to_data(img, output_type='data.frame')

text = text[text.conf != -1]

lines = text.groupby('block_num')['text'].apply(list)

print(lines[25])

[‘‘THANK’, ‘YOU’, ‘FOR’, ‘YOUR’, ‘BUSINESS.’]

Note that the first word seems to have an spelling issue.

import enchant

dict_check = enchant.Dict("en_US") for word in lines[25]:

if (dict_check.check(word)):

print(word+ ' - Dictionary Check Valid')

else:

print(word+ ' - Dictionary Check Valid Invalid')

print('Valid Suggestions')

print(dict_check.suggest(word))

‘THANK — Dictionary Check Valid Invalid

Valid Suggestions

[‘THANK’]

YOU — Dictionary Check Valid

FOR — Dictionary Check Valid

YOUR — Dictionary Check Valid

BUSINESS. — Dictionary Check Valid

Note that enchant found the first word invalid and was able to provide a alternative suggestion.

Using Regular Expressions for NLP

My early days in IT (almost 25 years ago) were filled with ups and downs. One day I would learn something new and feel on top of the world. Other days not so much. I remember the day when my manager asked me to work on a pattern-matching problem. I had to do the pattern-matching over data in Oracle. Since I had never done this before, I request him for pointers. The answer I got was was “This can be very easily done using REGEX”. I was a good follower, except there was nothing easy about using REGEX pattern matching). It took me a while to realize that my manager was kidding.

I still fear using REGEX but can’t escape it…..it still is widely used in Data Science especially NLP. Learning REGEX takes time. I have frequently used websites like https://regex101.com/ for practice.

For example if you want to extract all date fields from a document.

d = pytesseract.image_to_data(img, output_type=Output.DICT)

keys = list(d.keys()) date_pattern = '^(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[012])/(19|20)\d\d$' n_boxes = len(d['text'])

for i in range(n_boxes):

if int(d['conf'][i]) > 60:

if re.match(date_pattern, d['text'][i]):

(x, y, w, h) = (d['left'][i], d['top'][i], d['width'][i], d['height'][i])

img_date = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)

print(d['text'][i])

Output: 12/12/2001

You may even highlight the date fields in a given document

cv2.imwrite('aimg_date.png', img_date)

Image by Author

I hope this article was helpful in kick-starting your OCR and NLP knowledge. Topics like these are covered as part of the AWS Big Data Analytics course offered by Datafence Cloud Academy. The course is taught online by myself on weekends.