-
Notifications
You must be signed in to change notification settings - Fork 2
/
tesseract.py
executable file
·354 lines (273 loc) · 12.4 KB
/
tesseract.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import sys
sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages')
import cv2
import numpy as np
import os
from PIL import Image
import pytesseract
import argparse
import sys
import matplotlib.pyplot as plt
import re
import pandas as pd
import csv
#Import utils
#import debug_utils.utils as utils
from ner import nerutils
#For debug
import time
class ocrutils:
'''Class to do basic image preprocessing, extract the ROI from the image and perform OCR using tesseract
Attributes
---------------
self.img : Numpy array
Image to be processed
self.gray_img: Numpy array
Grayscale Image
self.rx_dict: Dict
Python dictionary containing regular expressions to match each of the fields in the doc
self.ret, self.key: Integers
Used to parse the result of OCR
'''
def __init__(self):
self.img = None
self.gray_img = None
self.rx_dict = {'name':re.compile(r'(?P<name>^nam.*)', flags=re.IGNORECASE),
'pan_no':re.compile(r'(?P<pano_no>pan.*)',flags=re.IGNORECASE),
'father_name':re.compile(r'(?P<father_name>father.*)',flags=re.IGNORECASE),
'relationship':re.compile(r'(?P<relationship>relation.*)',flags=re.IGNORECASE),
'residential_addr':re.compile(r'(?P<residential_addr>resident.*)',flags=re.IGNORECASE),
'period_stay':re.compile(r'(?P<period_stay>period.*)',flags=re.IGNORECASE),
'tel_no':re.compile(r'(?P<tel_no>tel.*)',flags=re.IGNORECASE),
'mobile_no':re.compile(r'(?P<mobile_no>mob.*)',flags=re.IGNORECASE),
'email':re.compile(r'(?P<email>e.*ai.*)',flags=re.IGNORECASE)}
self.parse_dict = {'name':[],'pan_no':[],'father_name':[],'relationship':[],'residential_addr':[],'period_stay':[],'tel_no':[],
'mobile_no':[],'email':[]}
#TODO:See if there is a better way to do this
self.ret = False
self.key = None
def load_img(self):
'''Function to load the image and call the function for extracting the required ROI'''
#Load Image
self.img = cv2.imread('../input_image/Loan_application_form_digital_v2.jpg')
# display("Image",self.img)
self.extract_table()
def extract_table(self):
'''
1)Function for extracting the ROI(table) from the document
More specifically the function extracts individual rows from the table and passes it to
self.preprocess_img.
2)Also invokes the function check_ocr from ner.py and stores the result of validation returned
from this function
'''
#Image for drawing contours
#For debug
cnt_img = np.copy(self.img)
#Local variables
box_coord_ls = []
merged_coord_ls = []
threshold = 10
flag = False
# display("Input image",self.img)
self.gray_img = cv2.cvtColor(self.img,cv2.COLOR_BGR2GRAY)
# display("Grayscale image",self.gray_img)
#Assume that the paper is white and the ink is black.
_,thresh_img = cv2.threshold(self.gray_img,240,255,cv2.THRESH_BINARY_INV)
# display("Thresholded image",thresh_img)
#Finding Contours on the image and extracting the largest one, this corresponds to our ROI(table)
contours, hierarchy = cv2.findContours(thresh_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
large_contour = sorted(contours, key=cv2.contourArea, reverse=True)[:1] #Extract only the table
# print("len(contours)",len(contours))
#Creating a mask of the ROI
table_mask = np.zeros((self.img.shape[0],self.img.shape[1]),dtype = np.uint8)
table_mask = cv2.drawContours(table_mask,large_contour,0,(255,255,255),-1)
# display("table_mask",table_mask)
table_img = cv2.bitwise_and(thresh_img,thresh_img,mask=table_mask)
# display("table_img",table_img)
#Performing NER at this stage
# nerutils_obj.process_img(table_img)
#Extract each individual contour from the table and merge neighbouring contours
contours, hierarchy = cv2.findContours(table_img,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cell_contours = sorted(contours,key=cv2.contourArea,reverse=True)[1:35]
for cnt in cell_contours:
x,y,w,h = cv2.boundingRect(cnt)
# if(w >= 300 and w <= 400):
if(w >= 300):
#Merge neighbouring contours
box_coord = np.array([x,y,w,h]).reshape(4,-1)
print("box_coord:",box_coord)
for i in range(0,len(box_coord_ls)):
if(abs((box_coord_ls[i][0,0] + box_coord_ls[i][2,0]) - box_coord[0,0]) < threshold and abs(box_coord_ls[i][1,0] - box_coord[1,0]) < threshold):
flag = True
xmin = box_coord_ls[i][0,0]
ymin = box_coord_ls[i][1,0]
xmax = box_coord_ls[i][0,0] + box_coord_ls[i][2,0] + box_coord[2,0]
ymax = box_coord_ls[i][1,0] + box_coord_ls[i][3,0]
element = np.array([xmin,ymin,xmax-box_coord_ls[i][0,0],ymax-box_coord_ls[i][1,0]]).reshape(4,-1)
box_coord_ls[i] = element
merged_coord = np.array([xmin,ymin,xmax,ymax]).reshape(4,-1)
merged_coord_ls.append(merged_coord)
if(flag == False):
box_coord_ls.append(box_coord)
flag = False
#Draw rectangles
cnt_img = np.copy(self.img)
# display("Input image",self.img)
for coord in box_coord_ls:
ocr_img = self.img[coord[1,0]:coord[1,0] + coord[3,0],coord[0,0]:coord[0,0] + coord[2,0]]
# display("OCR_Image",ocr_img)
#Preprocess the image before passing it to Tesseract
self.preprocess_img(ocr_img)
# cv2.rectangle(cnt_img,(coord[0,0],coord[1,0]),(coord[0,0] + coord[2,0],coord[1,0] + coord[3,0]),(0,0,255),5)
# display("Contour Image",cnt_img)
#Save the final dictionary as a CSV file
# print("self.parse_dict",self.parse_dict)
#Check using NER
dict_cond = nerutils_obj.check_ocr(self.parse_dict)
print("self.parse_dict",self.parse_dict)
print("dict_cond",dict_cond)
#Visualize the output
os.system('clear')
visualize_output_dict("OCR Output",self.parse_dict)
print("\n"*5) #leaving vertical space of 10 lines
visualize_output_dict("Result of validation using NER",dict_cond)
sys.exit(0)
#Filtering out self.parse_dict based on dict_cond values
#Writing self.parse_dict into a CSV file
# with open('../csv_files/'+ str(os.getpid()) + '.csv','w') as csvfile:
# fieldnames = list(self.parse_dict.keys())
# writer = csv.DictWriter(csvfile,fieldnames=fieldnames)
# writer.writeheader()
# writer.writerow(self.parse_dict)
def preprocess_img(self,cnt_img):
'''
Function to carry out preprocessing, before passing the image to Tesseract
Preprocessing image for OCR
Involves the following steps:
1) Scaling to 300 DPI(Ideally)
2) Increase contrast of the image
3) Binarize the image
4) Removing noise
5) Deskew the image (Correct for rotation)
Parameters
--------------------
cnt_img: Numpy array
Image cropped to an individual contour which is to be preprocessed
'''
self.counter = 0 #Counter keeps track of which picture is passed to OCR
self.ret = False
self.key = None
#Conversion to grayscale
gray_img = cv2.cvtColor(cnt_img,cv2.COLOR_BGR2GRAY)
# display("Grayscale image",gray_img)
#Thresholding the image
_,thresh_img = cv2.threshold(gray_img,230,255,cv2.THRESH_BINARY)
# display("thresh_img",thresh_img)
#Split each row into individual boxes and pass each one of them to self.run_tesseract
#Assume that the experiment is carried out in a controlled environment and hence we hardcode the values for coord_ls
coord_ls = [0,362,693,thresh_img.shape[1]]
for i in range(0,len(coord_ls)-1):
box_img = thresh_img[:,coord_ls[i]:coord_ls[i+1]]
#For debug
# display("box_img",box_img)
self.counter = (i + 1)
self.run_tesseract(box_img)
def run_tesseract(self,ocr_img):
'''
Function to run OCR using tesseract on ocr_img
Parameters
---------------
ocr_img: Numpy array
Preprocessed image which is passed to Tesseract
'''
# display("Image before passing to tesseract",ocr_img)
# Define config parameters.
# '-l eng' for using the English language
# '--oem 1' for using LSTM OCR Engine
config = ('-l eng --oem 3')
filename = "{}.png".format(os.getpid())
cv2.imwrite(str(filename),ocr_img)
print("Image saved to disk")
#Apply OCR using pytesseract
text = pytesseract.image_to_string(Image.open(filename),config=config)
#Save text to a file
f = open(str(os.getpid()) + ".txt","w")
f.write(text)
f.close()
print("Output written into text file")
#For debug
f = open(str(os.getpid()) + ".txt","r")
str_read = f.read()
print("str_read:",str_read)
#Remove the image file
if os.path.isfile(str(filename)):
os.remove(str(filename))
else:
print("Error: %s file not found" % myfile)
self.parse_output()
def parse_line(self,line):
'''
Function to parse a line of text using the compiled regular expression
Parameters
---------------------
line: String
Single line of text from the text file(containing OCR output)
'''
for key,rx in self.rx_dict.items():
match = rx.search(line)
if(match):
return key,True
#No matches
return False,False
def parse_output(self):
'''
Reads text file(containing OCR result), invokes the function parse_line to parse the output
using regex and updates the dictionary self.parse_dict based on the result of parse_line
'''
f = open(str(os.getpid()) + ".txt","r")
# for line in f:
line = f.read()
line = line.strip()
# line = line.lower()
line_result = re.findall(r"[0-9a-zA-Z ]+|[0-9a-zA-Z]", str(line))
line_result = ' '.join(map(str, line_result))
print("line_result:",line_result)
# breakpoint()
# print("self.counter:",self.counter)
if(self.counter > 1):
if(self.ret == True):
self.parse_dict[str(self.key)].append(str(line_result))
else:
self.key,self.ret = self.parse_line(line_result)
f.close()
# print("self.parse_dict",self.parse_dict)
#Delete txt files after parsing them
if(os.path.isfile(str(os.getpid()) + ".txt")):
os.remove(str(os.getpid()) + ".txt")
else:
print("Text file not found")
def breakpoint():
inp = input("Waiting for input...")
def display(txt,img):
'''Utility function to display an image with window name txt'''
winname = txt
cv2.namedWindow(winname,cv2.WINDOW_NORMAL)
cv2.imshow(winname,img)
key = cv2.waitKey(0)
if(key & 0xFF == ord('q')):
cv2.destroyAllWindows()
sys.exit()
def visualize_output_dict(txt,op_dict):
'''Utility Function to visualize output'''
color_bold = '\033[1m'
color_blue = '\033[94m'
color_normal = '\033[0m'
print(color_bold + color_blue + str(txt))
print(color_normal)
for element in op_dict:
print("{key}: {value}".format(key=element, value=op_dict[element]))
if __name__ == '__main__':
ocrutils_obj = ocrutils()
nerutils_obj = nerutils()
#Main Function
ocrutils_obj.load_img()