-
Notifications
You must be signed in to change notification settings - Fork 41
/
prediction.py
399 lines (313 loc) · 13.2 KB
/
prediction.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# Imports
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
from os.path import splitext
import uuid
import base64
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import model_from_json
from tensorflow.keras.utils import load_img, img_to_array
from keras.applications.mobilenet_v2 import preprocess_input
from sklearn.preprocessing import LabelEncoder
import cv2
import numpy as np
# Helper functions
class Label:
def __init__(self, cl=-1, tl=np.array([0., 0.]), br=np.array([0., 0.]), prob=None):
self.__tl = tl
self.__br = br
self.__cl = cl
self.__prob = prob
def __str__(self):
return 'Class: %d, top left(x: %f, y: %f), bottom right(x: %f, y: %f)' % (
self.__cl, self.__tl[0], self.__tl[1], self.__br[0], self.__br[1])
def copy(self):
return Label(self.__cl, self.__tl, self.__br)
def wh(self): return self.__br - self.__tl
def cc(self): return self.__tl + self.wh() / 2
def tl(self): return self.__tl
def br(self): return self.__br
def tr(self): return np.array([self.__br[0], self.__tl[1]])
def bl(self): return np.array([self.__tl[0], self.__br[1]])
def cl(self): return self.__cl
def area(self): return np.prod(self.wh())
def prob(self): return self.__prob
def set_class(self, cl):
self.__cl = cl
def set_tl(self, tl):
self.__tl = tl
def set_br(self, br):
self.__br = br
def set_wh(self, wh):
cc = self.cc()
self.__tl = cc - .5 * wh
self.__br = cc + .5 * wh
def set_prob(self, prob):
self.__prob = prob
class DLabel(Label):
def __init__(self, cl, pts, prob):
self.pts = pts
tl = np.amin(pts, axis=1)
br = np.amax(pts, axis=1)
Label.__init__(self, cl, tl, br, prob)
def getWH(shape):
return np.array(shape[1::-1]).astype(float)
def IOU(tl1, br1, tl2, br2):
wh1, wh2 = br1-tl1, br2-tl2
assert((wh1 >= 0).all() and (wh2 >= 0).all())
intersection_wh = np.maximum(np.minimum(br1, br2) - np.maximum(tl1, tl2), 0)
intersection_area = np.prod(intersection_wh)
area1, area2 = (np.prod(wh1), np.prod(wh2))
union_area = area1 + area2 - intersection_area
return intersection_area/union_area
def IOU_labels(l1, l2):
return IOU(l1.tl(), l1.br(), l2.tl(), l2.br())
def nms(Labels, iou_threshold=0.5):
SelectedLabels = []
Labels.sort(key=lambda l: l.prob(), reverse=True)
for label in Labels:
non_overlap = True
for sel_label in SelectedLabels:
if IOU_labels(label, sel_label) > iou_threshold:
non_overlap = False
break
if non_overlap:
SelectedLabels.append(label)
return SelectedLabels
def find_T_matrix(pts, t_pts):
A = np.zeros((8, 9))
for i in range(0, 4):
xi = pts[:, i]
xil = t_pts[:, i]
xi = xi.T
A[i*2, 3:6] = -xil[2]*xi
A[i*2, 6:] = xil[1]*xi
A[i*2+1, :3] = xil[2]*xi
A[i*2+1, 6:] = -xil[0]*xi
[U, S, V] = np.linalg.svd(A)
H = V[-1, :].reshape((3, 3))
return H
def getRectPts(tlx, tly, brx, bry):
return np.matrix([[tlx, brx, brx, tlx], [tly, tly, bry, bry], [1, 1, 1, 1]], dtype=float)
def normal(pts, side, mn, MN):
pts_MN_center_mn = pts * side
pts_MN = pts_MN_center_mn + mn.reshape((2, 1))
pts_prop = pts_MN / MN.reshape((2, 1))
return pts_prop
# Processing functions
#######################################
# Loads a model given a specific path #
#######################################
def load_model(path):
try:
path = splitext(path)[0]
with open('%s.json' % path, 'r') as json_file:
model_json = json_file.read()
model = model_from_json(model_json, custom_objects={})
model.load_weights('%s.h5' % path)
print("Model Loaded successfully...")
return model
except Exception as e:
print(e)
######################################################################################
# Converts colors from BGR (as read by OpenCV) to RGB (so that we can display them), #
# also eventually resizes the image to fit the size the model has been trained on #
######################################################################################
def preprocess_image(image_path,resize=False):
img = cv2.imread(image_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = img / 255
if resize:
img = cv2.resize(img, (224,224))
return img
#########################################################################
# Reconstructs the image from detected pattern into plate cropped image #
#########################################################################
def reconstruct(I, Iresized, Yr, lp_threshold):
# 4 max-pooling layers, stride = 2
net_stride = 2**4
side = ((208 + 40)/2)/net_stride
# one line and two lines license plate size
one_line = (470, 110)
two_lines = (280, 200)
Probs = Yr[..., 0]
Affines = Yr[..., 2:]
xx, yy = np.where(Probs > lp_threshold)
# CNN input image size
WH = getWH(Iresized.shape)
# output feature map size
MN = WH/net_stride
vxx = vyy = 0.5 #alpha
base = lambda vx, vy: np.matrix([[-vx, -vy, 1], [vx, -vy, 1], [vx, vy, 1], [-vx, vy, 1]]).T
labels = []
labels_frontal = []
for i in range(len(xx)):
x, y = xx[i], yy[i]
affine = Affines[x, y]
prob = Probs[x, y]
mn = np.array([float(y) + 0.5, float(x) + 0.5])
# affine transformation matrix
A = np.reshape(affine, (2, 3))
A[0, 0] = max(A[0, 0], 0)
A[1, 1] = max(A[1, 1], 0)
# identity transformation
B = np.zeros((2, 3))
B[0, 0] = max(A[0, 0], 0)
B[1, 1] = max(A[1, 1], 0)
pts = np.array(A*base(vxx, vyy))
pts_frontal = np.array(B*base(vxx, vyy))
pts_prop = normal(pts, side, mn, MN)
frontal = normal(pts_frontal, side, mn, MN)
labels.append(DLabel(0, pts_prop, prob))
labels_frontal.append(DLabel(0, frontal, prob))
final_labels = nms(labels, 0.1)
final_labels_frontal = nms(labels_frontal, 0.1)
assert final_labels_frontal, "No License plate is founded!"
# LP size and type
out_size, lp_type = (two_lines, 2) if ((final_labels_frontal[0].wh()[0] / final_labels_frontal[0].wh()[1]) < 1.7) else (one_line, 1)
TLp = []
Cor = []
if len(final_labels):
final_labels.sort(key=lambda x: x.prob(), reverse=True)
for _, label in enumerate(final_labels):
t_ptsh = getRectPts(0, 0, out_size[0], out_size[1])
ptsh = np.concatenate((label.pts * getWH(I.shape).reshape((2, 1)), np.ones((1, 4))))
H = find_T_matrix(ptsh, t_ptsh)
Ilp = cv2.warpPerspective(I, H, out_size, borderValue=0)
TLp.append(Ilp)
Cor.append(ptsh)
return final_labels, TLp, lp_type, Cor
#####################################################
# Detects a licence plate in an image using a model #
#####################################################
def detect_lp(model, I, max_dim, lp_threshold):
min_dim_img = min(I.shape[:2])
factor = float(max_dim) / min_dim_img
w, h = (np.array(I.shape[1::-1], dtype=float) * factor).astype(int).tolist()
Iresized = cv2.resize(I, (w, h))
T = Iresized.copy()
T = T.reshape((1, T.shape[0], T.shape[1], T.shape[2]))
Yr = model.predict(T)
Yr = np.squeeze(Yr)
L, TLp, lp_type, Cor = reconstruct(I, Iresized, Yr, lp_threshold)
return L, TLp, lp_type, Cor
##############################################################################
# Returns the image of the car (vehicle) and the Licence plate image (LpImg) #
##############################################################################
def get_plate(image_path, Dmax=608, Dmin = 608):
vehicle = preprocess_image(image_path)
ratio = float(max(vehicle.shape[:2])) / min(vehicle.shape[:2])
side = int(ratio * Dmin)
bound_dim = min(side, Dmax)
_ , LpImg, _, cor = detect_lp(wpod_net, vehicle, bound_dim, lp_threshold=0.5)
return vehicle, LpImg, cor
######################################################
# Grabs the contour of each digit from left to right #
######################################################
def sort_contours(cnts,reverse = False):
i = 0
boundingBoxes = [cv2.boundingRect(c) for c in cnts]
(cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes),
key=lambda b: b[1][i], reverse=reverse))
return cnts
###############################################
# Recognizes a single character from an image #
###############################################
def predict_characters_from_model(image):
image = cv2.resize(image,(80,80))
image = np.stack((image,)*3, axis=-1)
prediction = labels.inverse_transform([np.argmax(character_model.predict(image[np.newaxis,:]))])
return prediction
####################################################
# Ties all the steps together in a single function #
####################################################
def lpr_process(input_image_path):
# Get licence plate image
vehicle, LpImg, cor = get_plate(input_image_path)
# Preprocess the LP image
plate_image = cv2.convertScaleAbs(LpImg[0], alpha=(255.0))
gray = cv2.cvtColor(plate_image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(7,7),0)
# Applied inversed thresh_binary
binary = cv2.threshold(blur, 180, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
kernel3 = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
thre_mor = cv2.morphologyEx(binary, cv2.MORPH_DILATE, kernel3)
# Find the contours of the characters using cv2
cont, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# creat a copy version "test_roi" of plat_image to draw bounding box
test_roi = plate_image.copy()
# Initialize a list which will be used to append charater image
crop_characters = []
# Define standard width and height of character
digit_w, digit_h = 30, 60
# Validate found characters
for c in sort_contours(cont):
(x, y, w, h) = cv2.boundingRect(c)
ratio = h/w
if 1 <= ratio <= 3.5: # Only select contour with defined ratio
if h/plate_image.shape[0] >= 0.5: # Select contour which has the height larger than 50% of the plate
# Draw bounding box arroung digit number
cv2.rectangle(test_roi, (x, y), (x + w, y + h), (0, 255,0), 2)
# Sperate number and gibe prediction
curr_num = thre_mor[y:y+h, x:x+w]
curr_num = cv2.resize(curr_num, dsize=(digit_w, digit_h))
_, curr_num = cv2.threshold(curr_num, 220, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
crop_characters.append(curr_num)
# Recognize each character
license_plate_string = ""
for i, character in enumerate(crop_characters):
title = np.array2string(predict_characters_from_model(character))
license_plate_string += title.strip("'[]")
# Print results
if len(license_plate_string) >= 3 :
result = {
"license_plate_number_detection_status": "Successful",
"detected_license_plate_number": license_plate_string,
"input_image_name": input_image_path
}
#print(json.dumps(result))
return vehicle, LpImg, license_plate_string
else:
result = {
"license_plate_number_detection_status": "Failed",
"reason": "Not able to read license plate, it could be blurred or complex image",
"input_image_name": input_image_path
}
#print(json.dumps(result))
return vehicle, LpImg, 'Not able to read license plate'
# Load models
# Load the plate recognition model
wpod_net_path = "models/wpod-net.json"
wpod_net = load_model(wpod_net_path)
# Load the character recognition model
character_net_path = 'models/character_recoginition/MobileNets_character_recognition.json'
character_model = load_model(character_net_path)
print("[INFO] Model loaded successfully...")
# Load the character classes
labels = LabelEncoder()
labels.classes_ = np.load('models/character_recoginition/license_character_classes.npy')
print("[INFO] Labels loaded successfully...")
def process_file(filename):
if filename.endswith(".jpg") or filename.endswith(".jpeg") or filename.endswith(".png"):
vehicle, LpImg, license_plate_string = lpr_process(filename)
else:
license_plate_string = 'This is not a valid image file'
return license_plate_string
def process_base64_image(args_dict):
img_type = args_dict.get('type') or 'jpg'
base64img = args_dict.get('image')
img_bytes = base64.decodebytes(base64img.encode())
filename = os.path.join('/tmp', f'{uuid.uuid4()}.{img_type}')
with open(filename, 'wb') as f:
f.write(img_bytes)
vehicle, LpImg, license_plate_string = lpr_process(filename)
os.remove(filename)
return license_plate_string
def predict(args_dict):
if args_dict.get('image') is not None:
license_plate_string = process_base64_image(args_dict)
else:
filename = os.path.join('dataset/images', args_dict.get('data'))
license_plate_string = process_file(filename)
return {'prediction': license_plate_string}