-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocr_sw.py
173 lines (143 loc) · 6.88 KB
/
ocr_sw.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
from flask import Flask, request, render_template
import pytesseract
import os
import cv2
import json
import re
from Levenshtein import distance
from werkzeug.utils import secure_filename
#starting Flask server to get image
app = Flask(__name__, template_folder='templates')
#path to upload images
UPLOAD_FOLDER='UPLOADS/'
ALLOWED_EXTENSIONS = set([ 'png', 'jpg', 'jpeg'])
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route("/")
def index():
return render_template("index.html")
@app.route("/about")
def about():
return render_template("about.html")
#basic dictionary to classify the text extracted from image after OCR processing
dic = {'category': [],
'amount': [],
'score': [],
'tax': []}
#assigning directory for pytesseract
# pytesseract.pytesseract.tesseract_cmd = r'/home/ubuntu/tesseract.exe'
#OCR processing the Image into a string
def ocr_process(img, resolution=450, page_seg_method='3'):
txt=""
txt = "".join([txt, pytesseract.image_to_string(img, lang="eng",config='--psm ' + page_seg_method)])
return txt
#Categorising the Invoices according to some keywords from text extracted from the images
def categories(result_string,dic=dic):
#Categories
dining=re.findall('(server)|(Food)|(Dining)|(order)|(table)|(restaurant)',result_string, re.IGNORECASE)
apparel=re.findall('(shirt)|(pant)|(jeans)|(clothing)|(sleeve)|(men)|(ladies)|(accessories)',result_string,re.IGNORECASE)
medicine=re.findall('(medical)|(pharmacy)|(hospital)|(doctor)',result_string,re.IGNORECASE)
groceries=re.findall('(convinience)|(grocery)|(market)|(supermarket)',result_string,re.IGNORECASE)
transport =re.findall('(travels)|(transport)|(automobiles)|(car)|(bus)|(transportation)',result_string,re.IGNORECASE)
entertainment=re.findall('(movie)|(theatre)|(film)|(books)',result_string,re.IGNORECASE)
#Appending the Categories into the dictionary
if(len(dining)!=0):
dic['category'].append('Food')
elif(len(apparel)!=0):
dic['category'].append('Apparel')
elif(len(medicine)!=0):
dic['category'].append('Medical')
elif(len(groceries)!=0):
dic['category'].append('Groceries')
elif(len(transport)!=0):
dic['category'].append('Transportation')
elif(len(entertainment)!=0):
dic['category'].append('Entertainment')
return dic
#Scoring - removing frequently used words with '%d'
def scoring(regex_expression, item):
item_revised = re.sub('[0-9]{1,15}.{1,15}[0-9]{2}', '%d', item).lower()
#removing most frequently repeated words
item_revised = (item_revised.replace('total', '')
.replace('amount', '')
.replace('balance', '')
.replace('due', '')
.replace('$', '')
.replace('₹','')
.replace('USD', '')
.replace('INR','')
.strip())
#if all the strings are replaced with '%d' scpre=100, even if one extra character the score will become less
score = 100 - distance('%d', item_revised)
return score
#Extracting Total amount and Tax amount from the bill
def amount_parsser(invoice_string, regex_expression, dic=dic):
#comparing using regular expression
target_found = re.findall(regex_expression['regex'], invoice_string, re.IGNORECASE)
tax_found=re.findall(regex_expression['regex_tax'],invoice_string, re.IGNORECASE)
if len(target_found) == 0 :
print('Nothing matched')
return dic
else:
#Total Amount Processing
for ind, item in enumerate(target_found):
if_tax_in_string = 'tax' in item.lower()
if_last_in_string = 'last' in item.lower()
if any([if_tax_in_string, if_last_in_string]):
del(target_found[ind]) #delete the part with tax and last to find Total Amount
else:
target_amount = re.search('[0-9]{1,15}.{1,15}[0-9]{2}', item)
if target_amount is not None:
#extract the amount and score it at the same time
score = scoring(regex_expression, item.lower())
amount = target_amount.group(0).replace(',', '')
#appending values into the dictionary
dic['amount'].append(amount)
dic['score'].append(score)
#Tax Amount Processing
for ind,item in enumerate(tax_found):
if_last_in_string = 'last' in item.lower()
if (if_last_in_string):
del(target_found[ind])
tax_amount=re.search('[0-9]{1,15}.{1,15}[0-9]{2}', item)
if tax_amount is not None:
#extract the amount and score it at the same time
score = scoring(regex_expression, item.lower())
tax = tax_amount.group(0).replace(',', '')
#appending values into the dictionary
dic['tax'].append(tax)
return dic #return updated Dictionary
#Checking allowed extensions of image
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
#Request file, process it and return json file with information
@app.route('/uploader', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
print('No file part')
file = request.files['file']
if file.filename == '':
print('No selected file')
#Getting the Image from Server and Calling the Processing Functions
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
#reading the image
img= cv2.imread(os.path.join(app.config['UPLOAD_FOLDER'], filename))
img=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#calling OCR function
ocr_result=ocr_process(img)
#Regular Expression for finding Total and Tax Amount
totalAmountRegex = {'regex_tax': '(gst[^0-9]{1,30}[0-9,]*\.\d\d)',
'regex':'(?<!Tax )(?<!Sub)(?<!Sub )(Total[^0-9]{1,30}[0-9,]*\.\d\d)'}
#final dictionary that contains updated values
result_dic=categories(ocr_result,dic)
result_dic = amount_parsser(ocr_result, totalAmountRegex, dic)
#converting dictionary into json file
app_json = json.dumps(result_dic, sort_keys=True)
#returning json file back
return app_json
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)