Compare Amazon Textract with Tesseract OCR — OCR & NLP Use Case | by Manoj Kukreja | Towards Data Science

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.
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
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:
$ 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 pyenchantimport cv2import pytesseractimport refrom pytesseract import Outputimg_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 FERRARITHE IRISHMANJOJO RABBITJOKERLITTLE WOMENMARRIAGE STORY1917ONCE UPON A TIME...IN HOLLYWOODPARASITE
OCR Output using Amazon Textract OCR:
Both frameworks performed exactly the same. Lets see how handwritten text compares.
img_handwritten = cv2.imread('handwritten.jpg')txt_handwritten=pytesseract.image_to_string(img_handwritten)print(txt_handwritten)
OCR Output using Tesseract OCR:
<>
mhassadeyVWENS YEA) sore
&&. a)OW!NS anéLp Real Estate Group
RKSHIRE | AmbassadorH ATH. AWAY | Real EstatelomeServices
Ky Ie,aa Nim So mul for meetWith me today | Ht wis qed CatchingWp and Vin glad 10 hear Mk Ke wellfor bu. | Lovie Brwoud 10 Seeing YouAA aiv Go| Cheers|
Megan Owens, Realtor 402–689–4984 www.ForSalebyMegan.
MMS
com
OCR Output using Amazon Textract OCR:
Amazon Textract OCR performed marginally better than Tesseract OCR for handwritten text.
Now we will try a busy image.
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.comI 7Your Company LLC Address 123, State, My Country P 111–222–333, F 111–222–334BILL TO:P: 111–222–333, F: 111–222–334 a. zcient@eromplentContact Phone 101–102–103john Doe office ayment Terms ‘ash on DeliveryOffice Road 38P: 111–333–222, F: 122–222–334 Amount Due: $4,170office@example.netNO PRODUCTS / SERVICE QUANTITY / RATE / UNIT AMOUNTHOURS: PRICE1 aye 2 $20 $402 | Steering Wheel 5 $10 $503 | Engine oil 10 $15 $1504 | Brake Pad 24 $1000 $2,400Subtotal $275Tax (10%) $27.5Grand Total $302.5‘THANK YOU FOR YOUR BUSINESS
OCR Output using Amazon Textract OCR:
Amazon Textract identifies tables and forms in documents. This is neat.
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 enchantdict_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 InvalidValid Suggestions[‘THANK’]YOU — Dictionary Check ValidFOR — Dictionary Check ValidYOUR — Dictionary Check ValidBUSINESS. — Dictionary Check Valid
Note that enchant found the first word invalid and was able to provide a alternative suggestion.
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)
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.