Assignment 03 – Course COMPUTER VISION II: APPLICATIONS (PYTHON)
Performing
OCR on Invoice Documents
Situation
Mark works at an office where his job is to go through
the invoices received and record the sender and the amount billed to a
spreadsheet. It takes him only 1 minute to see the invoice and update the
sheet. Still he finds it tedious and wants to automate this task. The main
motivation for him is that he can then play on his system without any
distractions. Will you help Mark in his pursuit of happiness?
Your Task
In this assignment, you will implement a OCR system,
which looks at an image of the invoice and finds the following:
1. The Billing Amount ( 15
Marks )
2.
Sender and
Receiver Email IDs ( 15 Marks )
That's it!
It requires knowledge of OCR to get the data from the
image and then some basic python skills to get the required values from the
recognized text.
The assignment will be manually graded ( so that you
cannot simply look at the image and simply do a print(billing_amount).
More information is given in the respective section.
Import Libraries.
import
pytesseract
import
keras_ocr
import
matplotlib.pyplot as plt
import
cv2
Read and display the Test Image
doc_img
= cv2.imread('../resource/asnlib/publicdata/invoice.jpg', cv2.IMREAD_COLOR)
fig =
plt.figure(figsize=(20, 10))
plt.imshow(doc_img[:,:,::-1])
plt.show()
Perform OCR
You need to write the code for performing OCR on the
input image. You will have to apply the concepts learned in the previous
sections to perform OCR on the above image.
After you perform the OCR -
you have to parse the output so that you only print the email IDs and billing
amount in $
# The next lists will store the outputs:
billing_amount
= []
email_ids
= []
###
### YOUR
CODE HERE
###
#
Pre-processing image for OCR, convert image to gray scale
doc_img_gray=cv2.cvtColor(doc_img,cv2.COLOR_BGR2GRAY)
# OCR
with pytesseract
all_text
= pytesseract.image_to_string(doc_img_gray)
#
Parse to get the required values from
the recognized text
# The
email adresses have the character '@' and the amount has the character '$'
# Split
the OCR text in lines
all_lines
= all_text.splitlines() # all_lines is a list with all lines, each one with
several or none words
# We
have to split each line in words
all_words=[] # Empty list to sawe all the words
for line
in all_lines:
words = line.split()
all_words += words
# Search
for the words with special characters
for word
in all_words:
if(word.find('@')!=-1):
email_ids.append(word)
elif(word.find('$')!=-1):
billing_amount.append(word)
# Print
output