abhionair commited on
Commit
567a717
·
verified ·
1 Parent(s): f8bf826

Upload 8 files

Browse files
Procfile.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ web: gunicorn app:app
anpr.py ADDED
@@ -0,0 +1,229 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import cv2
3
+ import matplotlib.pyplot as plt
4
+ import pytesseract as pt
5
+ import os
6
+ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
7
+ from PIL import Image
8
+ import requests
9
+ import torch
10
+ import plotly.express as px
11
+
12
+ # LOAD YOLO MODEL
13
+ INPUT_WIDTH = 640
14
+ INPUT_HEIGHT = 640
15
+ #onnx_file_path = os.path.abspath('./static/models/best.onnx')
16
+ onnx_file_path = os.path.abspath(r'./static/model/best.onnx')
17
+ print(f"Attempting to load ONNX file from: {onnx_file_path}")
18
+ processor = TrOCRProcessor.from_pretrained('microsoft/trocr-base-printed')
19
+ model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-base-printed')
20
+
21
+
22
+
23
+ if not os.path.exists(onnx_file_path):
24
+ print(f"Error: ONNX file not found at {onnx_file_path}")
25
+ else:
26
+ try:
27
+ net = cv2.dnn.readNetFromONNX(onnx_file_path)
28
+ net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
29
+ net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
30
+ except cv2.error as e:
31
+ print(f"Error loading ONNX file: {onnx_file_path}")
32
+ print(f"OpenCV error: {e}")
33
+ raise # Re-raise the exception to halt the program
34
+
35
+
36
+
37
+
38
+
39
+ def get_detections(img,net):
40
+ # CONVERT IMAGE TO YOLO FORMAT
41
+ image = img.copy()
42
+ row, col, d = image.shape
43
+
44
+ max_rc = max(row,col)
45
+ input_image = np.zeros((max_rc,max_rc,3),dtype=np.uint8)
46
+ input_image[0:row,0:col] = image
47
+
48
+ # GET PREDICTION FROM YOLO MODEL
49
+ blob = cv2.dnn.blobFromImage(input_image,1/255,(INPUT_WIDTH,INPUT_HEIGHT),swapRB=True,crop=False)
50
+ net.setInput(blob)
51
+ preds = net.forward()
52
+ detections = preds[0]
53
+
54
+ return input_image, detections
55
+
56
+ def non_maximum_supression(input_image,detections):
57
+ # FILTER DETECTIONS BASED ON CONFIDENCE AND PROBABILIY SCORE
58
+ # center x, center y, w , h, conf, proba
59
+ boxes = []
60
+ confidences = []
61
+
62
+ image_w, image_h = input_image.shape[:2]
63
+ x_factor = image_w/INPUT_WIDTH
64
+ y_factor = image_h/INPUT_HEIGHT
65
+
66
+ for i in range(len(detections)):
67
+ row = detections[i]
68
+ confidence = row[4] # confidence of detecting license plate
69
+ if confidence > 0.4:
70
+ class_score = row[5] # probability score of license plate
71
+ if class_score > 0.25:
72
+ cx, cy , w, h = row[0:4]
73
+
74
+ left = int((cx - 0.5*w)*x_factor)
75
+ top = int((cy-0.5*h)*y_factor)
76
+ width = int(w*x_factor)
77
+ height = int(h*y_factor)
78
+ box = np.array([left,top,width,height])
79
+
80
+ confidences.append(confidence)
81
+ boxes.append(box)
82
+
83
+ # clean
84
+ boxes_np = np.array(boxes).tolist()
85
+ confidences_np = np.array(confidences).tolist()
86
+ # NMS
87
+ index = np.array(cv2.dnn.NMSBoxes(boxes_np,confidences_np,0.25,0.45)).flatten()
88
+
89
+ return boxes_np, confidences_np, index
90
+
91
+ def extract_text_py(image,bbox):
92
+ x,y,w,h = bbox
93
+
94
+ roi = image[y:y+h, x:x+w]
95
+ if 0 in roi.shape:
96
+ return ''
97
+ else:
98
+ roi_bgr = cv2.cvtColor(roi,cv2.COLOR_RGB2BGR)
99
+ gray = cv2.cvtColor(roi_bgr,cv2.COLOR_BGR2GRAY)
100
+ magic_color = apply_brightness_contrast(gray,brightness=40,contrast=70)
101
+ #text = pt.image_to_string(magic_color)
102
+ text = pt.image_to_string(magic_color,lang='eng',config='--psm 6')
103
+ text = text.strip()
104
+
105
+ return text
106
+
107
+ # extrating text
108
+ def extract_text(image,bbox):
109
+ x,y,w,h = bbox
110
+ roi = image[y:y+h, x:x+w]
111
+ #print("roi:",roi)
112
+
113
+ # Use OpenCV to read the image
114
+ img = roi.copy()
115
+ print(img.shape)
116
+ # Convert BGR to RGB
117
+ img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
118
+
119
+ # Create the Plotly Express figure
120
+ #fig = px.imshow(img_rgb)
121
+
122
+ # Update layout and show the figure
123
+ #fig.update_layout(width=100, height=40, margin=dict(l=10, r=10, b=10, t=10))
124
+ #fig.update_xaxes(showticklabels=False).update_yaxes(showticklabels=False)
125
+ #fig.show()
126
+
127
+ image = img_rgb
128
+ if 0 in roi.shape:
129
+ return 'no number'
130
+
131
+ else:
132
+ pixel_values = processor(images=image, return_tensors="pt").pixel_values
133
+ generated_ids = model.generate(pixel_values)
134
+ text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
135
+ text = filter_string(text)
136
+
137
+ return text
138
+
139
+ def filter_string(input_string):
140
+ filtered_chars = [char for char in input_string if char.isalnum() and (char.isupper() or char.isdigit())]
141
+ filtered_string = ''.join(filtered_chars)
142
+ return filtered_string
143
+
144
+
145
+ def drawings(image,boxes_np,confidences_np,index):
146
+ # drawings
147
+ text_list = []
148
+ for ind in index:
149
+ x,y,w,h = boxes_np[ind]
150
+ bb_conf = confidences_np[ind]
151
+ conf_text = 'plate: {:.0f}%'.format(bb_conf*100)
152
+ license_text = extract_text(image,boxes_np[ind])
153
+
154
+
155
+ cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,255),2)
156
+ cv2.rectangle(image,(x,y-30),(x+w,y),(255,0,255),-1)
157
+ cv2.rectangle(image,(x,y+h),(x+w,y+h+30),(0,0,0),-1)
158
+
159
+
160
+ cv2.putText(image,conf_text,(x,y-10),cv2.FONT_HERSHEY_SIMPLEX,0.7,(255,255,255),1)
161
+ cv2.putText(image,license_text,(x,y+h+27),cv2.FONT_HERSHEY_SIMPLEX,0.7,(0,255,0),1)
162
+
163
+ text_list.append(license_text)
164
+
165
+ return image, text_list
166
+
167
+
168
+ # predictions
169
+ def yolo_predictions(img,net):
170
+ ## step-1: detections
171
+ input_image, detections = get_detections(img,net)
172
+ ## step-2: NMS
173
+ boxes_np, confidences_np, index = non_maximum_supression(input_image, detections)
174
+ ## step-3: Drawings
175
+ result_img, text = drawings(img,boxes_np,confidences_np,index)
176
+ return result_img, text
177
+
178
+
179
+ def object_detection(path,filename):
180
+ # read image
181
+ image = cv2.imread(path) # PIL object
182
+ image = np.array(image,dtype=np.uint8) # 8 bit array (0,255)
183
+ result_img, text_list = yolo_predictions(image,net)
184
+ cv2.imwrite('./static/predict/{}'.format(filename),result_img)
185
+ return text_list
186
+
187
+
188
+
189
+ # def OCR(path,filename):
190
+ # img = np.array(load_img(path))
191
+ # cods = object_detection(path,filename)
192
+ # xmin ,xmax,ymin,ymax = cods[0]
193
+ # roi = img[ymin:ymax,xmin:xmax]
194
+ # roi_bgr = cv2.cvtColor(roi,cv2.COLOR_RGB2BGR)
195
+ # gray = cv2.cvtColor(roi_bgr,cv2.COLOR_BGR2GRAY)
196
+ # magic_color = apply_brightness_contrast(gray,brightness=40,contrast=70)
197
+ # cv2.imwrite('./static/roi/{}'.format(filename),roi_bgr)
198
+
199
+
200
+
201
+ # print(text)
202
+ # save_text(filename,text)
203
+ # return text
204
+
205
+
206
+ def apply_brightness_contrast(input_img, brightness = 0, contrast = 0):
207
+
208
+ if brightness != 0:
209
+ if brightness > 0:
210
+ shadow = brightness
211
+ highlight = 255
212
+ else:
213
+ shadow = 0
214
+ highlight = 255 + brightness
215
+ alpha_b = (highlight - shadow)/255
216
+ gamma_b = shadow
217
+
218
+ buf = cv2.addWeighted(input_img, alpha_b, input_img, 0, gamma_b)
219
+ else:
220
+ buf = input_img.copy()
221
+
222
+ if contrast != 0:
223
+ f = 131*(contrast + 127)/(127*(131-contrast))
224
+ alpha_c = f
225
+ gamma_c = 127*(1-f)
226
+
227
+ buf = cv2.addWeighted(buf, alpha_c, buf, 0, gamma_c)
228
+
229
+ return buf
app.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request
2
+ import os
3
+ from anpr import object_detection
4
+ # webserver gateway interface
5
+ app = Flask(__name__)
6
+
7
+ BASE_PATH = os.getcwd()
8
+ UPLOAD_PATH = os.path.join(BASE_PATH,'static/upload/')
9
+
10
+
11
+ @app.route('/',methods=['POST','GET'])
12
+ def index():
13
+ if request.method == 'POST':
14
+ upload_file = request.files['image_name']
15
+ filename = upload_file.filename
16
+ path_save = os.path.join(UPLOAD_PATH,filename)
17
+ upload_file.save(path_save)
18
+ text_list = object_detection(path_save,filename)
19
+
20
+ print(text_list)
21
+
22
+ return render_template('index.html',upload=True,upload_image=filename,text=text_list,no=len(text_list))
23
+
24
+ return render_template('index.html',upload=False)
25
+
26
+
27
+ if __name__ =="__main__":
28
+ app.run(debug=True)
requirements.txt ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ blinker==1.7.0
2
+ brotlipy==0.7.0
3
+ certifi==2021.5.30
4
+ contourpy==1.2.0
5
+ flask==3.0.0
6
+ fonttools==4.47.2
7
+ fsspec==2023.12.2
8
+ geographiclib==1.52
9
+ geopy==2.2.0
10
+ gunicorn==19.9.0
11
+ huggingface-hub==0.20.2
12
+ importlib-metadata==7.0.1
13
+ importlib-resources==6.1.1
14
+ itsdangerous==2.1.2
15
+ Jinja2==3.1.3
16
+ kiwisolver==1.4.5
17
+ MarkupSafe==2.1.3
18
+ matplotlib==3.8.2
19
+ mbstrdecoder==1.1.0
20
+ mpmath==1.3.0
21
+ networkx==3.2.1
22
+ numpy==1.26.3
23
+ opencv-python==4.9.0.80
24
+ packaging==21.3
25
+ pandas==2.2.0
26
+ pillow==10.2.0
27
+ plotly==5.18.0
28
+ pycosat==0.6.3
29
+ pyparsing==3.0.7
30
+ pytesseract==0.3.10
31
+ python-dateutil==2.8.2
32
+ pytz==2022.1
33
+ PyYAML==6.0.1
34
+ regex==2023.12.25
35
+ safetensors==0.4.1
36
+ sympy==1.12
37
+ tenacity==8.2.3
38
+ tokenizers==0.15.0
39
+ torch==2.1.2
40
+ transformers==4.36.2
41
+ typepy==1.3.0
42
+ typing-extensions==4.9.0
43
+ tzdata==2023.4
44
+ werkzeug==3.0.1
45
+ zipp==3.17.0
static/model/best.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:15c5cd92885be9bdab18f8a1d00d37ed037614a0c8a066d30bf62ce513455568
3
+ size 28498982
static/model/best.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:613dd4a3e709b45254dd5f0faa4c229f837ab4ba7f89cfb9b6cc2db4b6f7c890
3
+ size 14327528
templates/index.html ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {% extends 'layout.html' %}
2
+
3
+ {% block body %}
4
+ <div class="container">
5
+ <br><br>
6
+ <form action="#" method="POST" enctype="multipart/form-data">
7
+ <div class="input-group">
8
+ <input type="file" class="form-control" name="image_name" required>
9
+ <input type="submit" value="Upload" class="btn btn-outline-secondary">
10
+ </div>
11
+ </form>
12
+ </div>
13
+
14
+ {% if upload %}
15
+ <div class="container">
16
+ <br><br>
17
+ <table style="margin-bottom: 20px;"> <!-- Added margin-bottom for spacing -->
18
+ <tr style="border: solid black;">
19
+ <th style="font-size: 20px;">Original Image</th>
20
+ <th style="font-size: 20px;">Corresponding bounding box Image</th>
21
+ </tr>
22
+ <tr>
23
+ <td>
24
+ <img class="rounded float-left img-fluid" src="/static/upload/{{ upload_image }}" alt="">
25
+ </td>
26
+ <td>
27
+ <img class="rounded float-right img-fluid" src="/static/predict/{{ upload_image }}" alt="">
28
+ </td>
29
+ </tr>
30
+ </table>
31
+
32
+ <table style="border: solid black; width: 100%;">
33
+ <tr style="border: solid black; text-align: center;">
34
+ <th style="font-size: 26px;">Number Plate of the Car</th>
35
+ </tr>
36
+ <tr style="border: solid black;">
37
+ <td>
38
+ <img class="img-fluid" src="/static/roi/{{ upload_image }}" alt="">
39
+ </td>
40
+ <td style="background-color: yellow;">
41
+ <h1 class="display-2"> {{ text }}</h1>
42
+ </td>
43
+ </tr>
44
+ </table>
45
+ </div>
46
+ {% endif %}
47
+
48
+ {% endblock %}
templates/layout.html ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
+ <title>ANPR Project</title>
8
+ <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
9
+ <script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r" crossorigin="anonymous"></script>
10
+ <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-BBtl+eGJRgqQAUMxJ7pMwbEyER4l1g+O15P+16Ep7Q9Q+zqX6gSbd85u4mG4QzX+" crossorigin="anonymous"></script>
11
+ <!-- Add your CSS files or styles here -->
12
+ </head>
13
+ <body>
14
+ <div class="container">
15
+ <div class="row">
16
+ <div class="col-md-6">
17
+ <div class="col-md-6">
18
+ <h2>Professor:</h2>
19
+ <p>Ravi Kiran</p>
20
+ </div>
21
+ <h2>Members:</h2>
22
+ <ul>
23
+ <li>Sreedhar Budamagunta</li>
24
+ <li>Abhinav Govind Rao</li>
25
+ <li>Praveen Srivastava</li>
26
+ </ul>
27
+ </div>
28
+ <div class="col-md-6">
29
+ <h2>Mentor:</h2>
30
+ <p>Sangeeth</p>
31
+ </div>
32
+ </div>
33
+ </div>
34
+
35
+ <nav class="navbar navbar-dark bg-dark">
36
+ <div class="container">
37
+ <a class="navbar-brand" href="/">
38
+ <h1 class="display-6">IIITH - Cohort 21 - ANPR OCR Group 6 </h1>
39
+ </a>
40
+ </div>
41
+ </nav>
42
+
43
+ {% block body %}
44
+
45
+ {% endblock %}
46
+
47
+
48
+ <footer>
49
+ <hr>
50
+ <h2>References</h2>
51
+ <a href="https://www.iiit.ac.in/" target="_blank">IIIT Hyderabad website</a>
52
+ <br>
53
+ <a href="https://pytorch.org/hub/ultralytics_yolov5/" target="_blank">YOLO5</a>
54
+ <br>
55
+ <a href="https://huggingface.co/docs/transformers/model_doc/trocr" target="_blank">Huggingface TrOCR</a>
56
+ </footer>
57
+
58
+
59
+ <!-- Add your JavaScript files or scripts here -->
60
+
61
+ </body>
62
+ </html>