44from flask import Flask , redirect , url_for , request , render_template , flash , send_from_directory
55from werkzeug .utils import secure_filename
66from api import maincall , analyze
7+ import cv2
8+ import numpy as np
9+ from matplotlib import pyplot as plt
10+ from pytesseract import image_to_string
711
12+ def ocr (filename ):
13+ img = cv2 .imread (f'static/images/cheques/{ filename } ' )
14+
15+ max_brightness = 0
16+ gray = cv2 .cvtColor (img , cv2 .COLOR_BGR2GRAY )
17+ canvas = img .copy ()
18+ #ret,thresh = cv2.threshold(gray,199,255,1)
19+ blur = cv2 .medianBlur (img ,5 )
20+ edged = cv2 .Canny (blur , 25 , 70 )
21+ contours ,h = cv2 .findContours (edged ,cv2 .RETR_EXTERNAL ,cv2 .CHAIN_APPROX_SIMPLE )
22+
23+ cv2 .drawContours (img , contours ,- 1 , (0 ,255 ,0 ), 3 )
24+
25+ for cnt in contours :
26+ rect = cv2 .boundingRect (cnt )
27+ x , y , w , h = rect
28+ if w * h > 40000 :
29+ mask = np .zeros (img .shape , np .uint8 )
30+ mask [y :y + h , x :x + w ] = img [y :y + h , x :x + w ]
31+ brightness = np .sum (mask )
32+ if brightness > max_brightness :
33+ brightest_rectangle = rect
34+ max_brightness = brightness
35+
36+ x , y , w , h = brightest_rectangle
37+ crop_img = img [y :y + h , x :x + w ]
38+
39+ edges = cv2 .Canny (crop_img ,101 ,200 )
40+
41+ crop_img = img [y :y + h , x :x + w ]
42+ cheque = {}
43+
44+ cheque ['date' ] = { 'bounds' : (int (h * 0.07 ), int (h * 0.14 ), int (w * 0.75 ),int (w * 0.965 ) )}
45+ cheque ['name' ] = { 'bounds' : (int (h * 0.17 ), int (h * 0.27 ), int (w * 0.07 ),int (w * 0.6 ) )}
46+ cheque ['amt_words1' ] = { 'bounds' : (int (h * 0.28 ), int (h * 0.36 ), int (w * 0.15 ),int (w * 0.72 ) )}
47+ cheque ['amt_words2' ] = { 'bounds' : (int (h * 0.36 ), int (h * 0.45 ), int (w * 0.03 ),int (w * 0.65 ) )}
48+ cheque ['amt_no' ] = { 'bounds' : (int (h * 0.36 ), int (h * 0.45 ), int (w * 0.77 ),int (w * 0.97 ) )}
49+ cheque ['acc_no' ] = { 'bounds' : (int (h * 0.49 ), int (h * 0.55 ), int (w * 0.09 ),int (w * 0.40 ) )}
50+ cheque ['micr_code' ] = { 'bounds' : (int (h * 0.85 ), int (h * 0.95 ), int (w * 0.2 ),int (w * 0.75 ) )}
51+
52+ out = cv2 .cvtColor (edges , cv2 .COLOR_GRAY2RGB )
53+ text = {}
54+
55+ for f in cheque :
56+ y0 , y1 , x0 , x1 = cheque [f ]['bounds' ]
57+ crop_field = crop_img [y0 :y1 ,x0 :x1 ]
58+ cv2 .rectangle (out , (x0 ,y0 ),(x1 ,y1 ), color = (0 ,255 ,0 ), thickness = 3 )
59+ text [f ] = image_to_string (crop_field )
60+ print (f , ':' , text [f ])
61+
62+
63+ cv2 .imwrite (f'static/images/cropped/{ filename } ' ,cv2 .resize (crop_img , (640 ,350 )))
64+ cv2 .imwrite (f'static/images/canny/{ filename } ' ,cv2 .resize (out , (640 ,350 )))
65+ return text
866
967UPLOAD_FOLDER = './static/images/cheques'
1068ALLOWED_EXTENSIONS = {'png' , 'jpg' , 'jpeg' , 'gif' }
@@ -42,6 +100,17 @@ def upload_file():
42100 with open ('./static/data/' + filename [:- 4 ]+ '.txt' ,'w' ) as fil :
43101 for key in field_list :
44102 fil .write (request .form [key ]+ '\n ' )
103+ text_data = ocr (file .filename )
104+ with open ('./static/data/' + filename [:- 4 ]+ '_analyze.txt' ,'w' ) as fil :
105+ fil .write ('1950020' + '\n ' )
106+ fil .write (text_data ['amt_words1' ]+ '\n ' )
107+ fil .write (text_data ['amt_no' ]+ '\n ' )
108+ fil .write ('__/__/____' + '\n ' )
109+ fil .write (text_data ['micr_code' ].split ()[1 ]+ '\n ' )
110+ fil .write (text_data ['micr_code' ].split ()[3 ]+ '\n ' )
111+ fil .write (text_data ['name' ]+ '\n ' )
112+ fil .write (text_data ['acc_no' ]+ '\n ' )
113+ fil .write (('yes' + '\n ' )* 2 )
45114 return redirect (url_for ('gallery' ))
46115 return render_template ('upload.html' )
47116
@@ -65,21 +134,38 @@ def evaluate(chq_num):
65134 user_data = dict ()
66135 for i ,val in enumerate (f .read ().splitlines ()):
67136 user_data [fields [i ]] = val
137+ with open ('./static/data/' + str (chq_num )[:- 4 ]+ '_analyze.txt' ) as f :
138+ eval_data = dict ()
139+ for i ,val in enumerate (f .read ().splitlines ()):
140+ eval_data [fields [i ]] = val
68141 except :
69142 user_data = dict ()
143+ eval_data = dict ()
70144
71145 user_data ['chq_num' ] = chq_num
72- eval_data = analyze (chq_num )
73146 return render_template ('evaluate.html' , eval_data = eval_data , user_data = user_data )
74147
75148@app .route ('/credits' )
76149def credits ():
77150 return render_template ('credits.html' )
78151
79- @app .route ('/' )
152+ @app .route ('/index ' )
80153def index ():
81154 return render_template ('index.html' )
82155
156+ @app .route ('/' ,methods = ['GET' , 'POST' ])
157+ def login ():
158+ if request .method == 'POST' :
159+ if request .
form [
'email' ]
== '[email protected] ' :
160+ if request .form ['password' ] == 'admin123' :
161+ return redirect (url_for ('index' ))
162+ else :
163+ flash ("Invalid Email or Password" )
164+ elif request .form ['password' ] != 'admin123' :
165+ flash ("Invalid Email or Password" )
166+ else :
167+ flash ("Invalid Email or Password" )
168+ return render_template ('login.html' )
169+
83170if __name__ == '__main__' :
84171 app .run (debug = True , host = '0.0.0.0' , port = 8000 )
85-
0 commit comments