-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecognition.py
150 lines (118 loc) · 5.71 KB
/
recognition.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
import cv2
import numpy as np
from skimage import measure
from imutils import perspective
import imutils
from data_utils import order_points, convert2Square, draw_labels_and_boxes
from detect import detectNumberPlate
from model import CNN_Model
from skimage.filters import threshold_local
ALPHA_DICT = {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E', 5: 'F', 6: 'G', 7: 'H', 8: 'K', 9: 'L', 10: 'M', 11: 'N', 12: 'P',
13: 'R', 14: 'S', 15: 'T', 16: 'U', 17: 'V', 18: 'X', 19: 'Y', 20: 'Z', 21: '0', 22: '1', 23: '2', 24: '3',
25: '4', 26: '5', 27: '6', 28: '7', 29: '8', 30: '9', 31: "Background"}
class E2E(object):
def __init__(self):
self.image = np.empty((28, 28, 1))
self.detectLP = detectNumberPlate()
self.recogChar = CNN_Model(trainable=False).model
self.recogChar.load_weights('./weights/weight.h5')
self.candidates = []
def extractLP(self):
coordinates = self.detectLP.detect(self.image)
if len(coordinates) == 0:
ValueError('No images detected')
for coordinate in coordinates:
yield coordinate
def predict(self, image):
# Input image or frame
self.image = image
for coordinate in self.extractLP(): # detect license plate by yolov3
self.candidates = []
# convert (x_min, y_min, width, height) to coordinate(top left, top right, bottom left, bottom right)
pts = order_points(coordinate)
# crop number plate used by bird's eyes view transformation
LpRegion = perspective.four_point_transform(self.image, pts)
# cv2.imwrite('step1.png', LpRegion)
# segmentation
self.segmentation(LpRegion)
# recognize characters
self.recognizeChar()
# format and display license plate
license_plate = self.format()
# draw labels
self.image = draw_labels_and_boxes(self.image, license_plate, coordinate)
# cv2.imwrite('example.png', self.image)
# THIS RETURN AN IMAGE
# return self.image
# THIS RETURN A STRING
return license_plate
def segmentation(self, LpRegion):
# apply thresh to extracted licences plate
V = cv2.split(cv2.cvtColor(LpRegion, cv2.COLOR_BGR2HSV))[2]
# adaptive threshold
T = threshold_local(V, 15, offset=10, method="gaussian")
thresh = (V > T).astype("uint8") * 255
cv2.imwrite("step2_1.png", thresh)
# convert black pixel of digits to white pixel
thresh = cv2.bitwise_not(thresh)
cv2.imwrite("step2_2.png", thresh)
thresh = imutils.resize(thresh, width=400)
thresh = cv2.medianBlur(thresh, 5)
# connected components analysis
labels = measure.label(thresh, connectivity=2, background=0)
# loop over the unique components
for label in np.unique(labels):
# if this is background label, ignore it
if label == 0:
continue
# init mask to store the location of the character candidates
mask = np.zeros(thresh.shape, dtype="uint8")
mask[labels == label] = 255
# find contours from mask
contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if len(contours) > 0:
contour = max(contours, key=cv2.contourArea)
(x, y, w, h) = cv2.boundingRect(contour)
# rule to determine characters
aspectRatio = w / float(h)
solidity = cv2.contourArea(contour) / float(w * h)
heightRatio = h / float(LpRegion.shape[0])
if 0.1 < aspectRatio < 1.0 and solidity > 0.1 and 0.35 < heightRatio < 2.0:
# extract characters
candidate = np.array(mask[y:y + h, x:x + w])
square_candidate = convert2Square(candidate)
square_candidate = cv2.resize(square_candidate, (28, 28), cv2.INTER_AREA)
# cv2.imwrite('./characters/' + str(y) + "_" + str(x) + ".png", cv2.resize(square_candidate, (56, 56), cv2.INTER_AREA))
square_candidate = square_candidate.reshape((28, 28, 1))
self.candidates.append((square_candidate, (y, x)))
def recognizeChar(self):
characters = []
coordinates = []
for char, coordinate in self.candidates:
characters.append(char)
coordinates.append(coordinate)
characters = np.array(characters)
result = self.recogChar.predict_on_batch(characters)
result_idx = np.argmax(result, axis=1)
self.candidates = []
for i in range(len(result_idx)):
if result_idx[i] == 31: # if is background or noise, ignore it
continue
self.candidates.append((ALPHA_DICT[result_idx[i]], coordinates[i]))
def format(self):
first_line = []
second_line = []
for candidate, coordinate in self.candidates:
if self.candidates[0][1][0] + 40 > coordinate[0]:
first_line.append((candidate, coordinate[1]))
else:
second_line.append((candidate, coordinate[1]))
def take_second(s):
return s[1]
first_line = sorted(first_line, key=take_second)
second_line = sorted(second_line, key=take_second)
if len(second_line) == 0: # if license plate has 1 line
license_plate = "".join([str(ele[0]) for ele in first_line])
else: # if license plate has 2 lines
license_plate = "".join([str(ele[0]) for ele in first_line]) + "-" + "".join([str(ele[0]) for ele in second_line])
return license_plate