-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainPrediction.py
More file actions
246 lines (200 loc) · 8.15 KB
/
Copy pathMainPrediction.py
File metadata and controls
246 lines (200 loc) · 8.15 KB
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
from os.path import splitext
import cv2
import imutils
import numpy as np
from keras.models import model_from_json
from local_utils import detect_lp
class Model:
# Hàm key lúc sort
def takeFirst(self, elem):
return elem[0]
# Load WPOD để detect vùng biển
def load_model(self, 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("Loading model successfully...")
return model
except Exception as e:
print(e)
# tiền xử lý ảnh
def pre_process_img(self, img, resize=False):
# đưa ảnh về thang màu RGB
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# cv2.imshow('anh mau rgb', img)
# chuẩn hóa các pixel về dải 0-1
img = img / 255
# cv2.imshow('anh img', img)
return img
# lầy ra vùng ảnh chứa biển số
def get_plate(self, img, wpod_net):
Dmax = 608
Dmin = 256
try:
img_pre_process = self.pre_process_img(img)
# print(img_pre_process.shape[:2])
check = float(
max(img_pre_process.shape[:2])) / min(img_pre_process.shape[:2])
side = int(check * Dmin)
# print(side)
bound_dim = min(side, Dmax)
# tìm ra vùng ảnh chứ biển số
_, place_img, _, cor = detect_lp(
wpod_net, img_pre_process, bound_dim, lp_threshold=0.5)
except:
place_img = None
cor = None
return place_img, cor
# xử lý ảnh
def process_img(self, place_img):
# kiểm tra có ảnh vùng biển
if (len(place_img)):
# chuyển đổi đưa ảnh về hệ 8bit
plate_img = cv2.convertScaleAbs(place_img[0], alpha=(255.0))
# chuyển ảnh sang thang xám và làm mở
img_gray = cv2.cvtColor(plate_img, cv2.COLOR_BGR2GRAY)
img_blur = cv2.GaussianBlur(img_gray, (7, 7), 0)
# ================
# Phân ngưỡng ảnh
# binary = cv2.threshold(img_blur, 170, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# binary = cv2.adaptiveThreshold(img_blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)\
binary = cv2.adaptiveThreshold(
img_blur, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
# cv2.imshow("Anh bien so sau threshold", binary)
# cv2.imwrite("anhbienso2.jpg", binary)
# dãn nở nhằm khử nhiễu và đưa ra ảnh đã được xử lý
kernel3 = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
img_thre = cv2.morphologyEx(binary, cv2.MORPH_DILATE, kernel3)
return img_thre
# tìm ra đường viền bao
def find_contours(self, img_thre):
point = []
# cv2.imshow("anh trong find_contours", img_thre)
# tìm ra các đường viền trong ảnh
contours = cv2.findContours(
img_thre.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(contours)
# contours = sorted(contours, key = cv2.contourArea, reverse = True)[:7]
# duyệt qua các đường bao
for c in contours:
# tính chu vi, tìm ra hình vuông xấp xỉ bao quanh và đưa ra tọa độ, dài, rộng
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.018 * peri, True)
x, y, w, h = cv2.boundingRect(approx)
# kiểm ra các ô vuông, chỉ ra các ô chứa ký tự
if 17 <= w <= 55 and 63 <= h <= 86:
point.append((x, y, w, h))
return point
# sắp xếp lại các ký tự đúng theo thứ tự biển số
def sort_point(self, img_thre, point):
point1 = []
point2 = []
rate = img_thre.shape[1] / img_thre.shape[0]
# biển chữ nhật
if rate > 3:
point.sort(key=self.takeFirst)
# biển vuông
else:
mean = np.mean(np.array(point), axis=0)[1]
for x in point:
if x[1] < mean:
point1.append(x)
else:
point2.append(x)
point1.sort(key=self.takeFirst)
point2.sort(key=self.takeFirst)
point = point1 + point2
return point
# tách các ký tự ra làm từng ảnh con và đưa về dạng 30*60 rồi chuyển về dạng thích hơp làm input cho model
def find_char(self, point, img_thre, place_img):
character = []
point = self.sort_point(img_thre, point)
for i in point:
# vẽ ô vuông quanh các ký tự
x, y, w, h = i
cv2.rectangle(place_img[0], (x, y), (x + w, y + h), (0, 255, 0), 2)
# bóc các ký tự về ảnh con
pts1 = np.float32([[x, y], [x + w, y], [x, y + h], [x + w, y + h]])
pts2 = np.float32([[0, 0], [w, 0], [0, h], [w, h]])
matrix = cv2.getPerspectiveTransform(pts1, pts2)
char = cv2.warpPerspective(img_thre, matrix, (w, h))
# đưa về dạng thích hợp để làm input cho model
char = cv2.resize(char, (30, 60))
char = char.reshape(-1, 30 * 60)
character.append(char)
character = np.array(character, dtype=np.float32)
return character
def recognize(self, model_svm, character):
string = ''
# dự đoán các ký tự
for i in range(character.shape[0]):
result = model_svm.predict(character[i])
result = int(result[1][0][0])
# chuyển lại mã ascii về ký tự
result = chr(result)
string += result
return string
# format lại string vừa nhận diện
def format(self, string):
string_new = ''
if len(string) == 8:
for i in range(len(string)):
if i == 2:
string_new += '-'
elif i == 4:
string_new += ' '
string_new += string[i]
elif len(string) == 9:
for i in range(len(string)):
if i == 2:
string_new += '-'
elif i == 4:
string_new += ' '
elif i == 7:
string_new += '.'
string_new += string[i]
else:
string_new = string
return string_new
# viết biển số lên ảnh
def draw_box(self, img, cor, string):
pts = []
# tọa x của 4 góc biển số
x = cor[0][0]
# tọa y của 4 góc biển số
y = cor[0][1]
for i in range(4):
pts.append([int(x[i]), int(y[i])])
pts = np.array(pts)
peri = cv2.arcLength(pts, True)
approx = cv2.approxPolyDP(pts, 0.018 * peri, True)
x, y, w, h = cv2.boundingRect(approx)
# vẽ ô vuông và biển số lên ảnh
cv2.putText(img, string, (x - 10, y - 10),
cv2.FONT_HERSHEY_PLAIN, 1.5, 255, 2)
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
def predict(path):
# Load model
model_svm = cv2.ml.SVM_load('model_svmNew.xml')
model = Model()
wpod_net = model.load_model("wpod-net.json")
# print(type(wpod_net))
# img_path = path
# img = cv2.imread(img_path)
img = path
place_img, cor = model.get_plate(img, wpod_net)
assert place_img is not None
clone_img = place_img[0].copy()
if place_img is not None and cor is not None:
img_thre = model.process_img(place_img)
point = model.find_contours(img_thre)
character = model.find_char(point, img_thre, place_img)
string = model.recognize(model_svm, character)
string = model.format(string)
model.draw_box(img, cor, string)
return string, clone_img * 255
else:
return None, None