-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.py
265 lines (201 loc) · 6.48 KB
/
main.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
import time
import cv2
import numpy as np
import torch
from colour_detection.detect_color import detect_color
from lpr_net.model.lpr_net import build_lprnet
from lpr_net.rec_plate import rec_plate, CHARS
from object_detection.detect_car_YOLO import ObjectDetection
from track_logic import *
import settings
def get_frames(video_src: str) -> np.ndarray:
"""
Генератор, котрый читает видео и отдает фреймы
"""
cap = cv2.VideoCapture(video_src)
while cap.isOpened():
ret, frame = cap.read()
if ret:
yield frame
else:
print("End video")
break
return None
def preprocess(image: np.ndarray, size: tuple) -> np.ndarray:
"""
Препроцесс перед отправкой на YOLO
Ресайз, нормализация и т.д.
"""
image = cv2.resize(
image, size, fx=0, fy=0, interpolation=cv2.INTER_CUBIC # resolution
)
return image
def get_boxes(results, frame):
"""
return dict with labels and cords
:param results: inferences made by model
:param frame: frame on which cords calculated
:return: dict with labels and cords
"""
labels, cord = results
n = len(labels)
x_shape, y_shape = frame.shape[1], frame.shape[0]
labls_cords = {}
numbers = []
cars = []
trucks = []
buses = []
for i in range(n):
row = cord[i]
x1, y1, x2, y2 = (
int(row[0] * x_shape),
int(row[1] * y_shape),
int(row[2] * x_shape),
int(row[3] * y_shape),
)
if labels[i] == 0:
numbers.append((x1, y1, x2, y2))
elif labels[i] == 1:
cars.append((x1, y1, x2, y2))
elif labels[i] == 2:
trucks.append((x1, y1, x2, y2))
elif labels[i] == 3:
buses.append((x1, y1, x2, y2))
labls_cords["numbers"] = numbers
labls_cords["cars"] = cars
labls_cords["trucks"] = trucks
labls_cords["busses"] = buses
return labls_cords
def plot_boxes(cars_list: list, frame: np.ndarray) -> np.ndarray:
n = len(cars_list)
for car in cars_list:
car_type = car[2]
x1_number, y1_number, x2_number, y2_number = car[0][0]
number = car[0][1]
x1_car, y1_car, x2_car, y2_car = car[1][0]
colour = car[1][1]
if car_type == "car":
car_bgr = (0, 0, 255)
elif car_type == "truck":
car_bgr = (0, 255, 0)
elif car_type == "bus":
car_bgr = (255, 0, 0)
number_bgr = (255, 255, 255)
cv2.rectangle(frame, (x1_car, y1_car), (x2_car, y2_car), car_bgr, 2)
cv2.putText(
frame,
car_type + " " + colour,
(x1_car, y2_car + 15),
0,
1,
car_bgr,
thickness=2,
lineType=cv2.LINE_AA,
)
cv2.rectangle(
frame, (x1_number, y1_number), (x2_number, y2_number), number_bgr, 2
)
cv2.putText(
frame,
number,
(x1_number - 20, y2_number + 30),
0,
1,
number_bgr,
thickness=2,
lineType=cv2.LINE_AA,
)
detection_area = settings.DETECTION_AREA
cv2.rectangle(frame, detection_area[0], detection_area[1], (0, 0, 0), 2)
return frame
def check_roi(coords):
detection_area = settings.DETECTION_AREA
xc = int((coords[0] + coords[2]) / 2)
yc = int((coords[1] + coords[3]) / 2)
if (
(detection_area[0][0] < xc < detection_area[1][0])
and
(detection_area[0][1] < yc < detection_area[1][1])
):
return True
else:
return False
def main(
video_file_path,
yolo_model_path,
yolo_conf,
yolo_iou,
lpr_model_path,
lpr_max_len,
lpr_dropout_rate,
device
):
cv2.startWindowThread()
detector = ObjectDetection(
yolo_model_path,
conf=yolo_conf,
iou=yolo_iou,
device = device
)
LPRnet = build_lprnet(
lpr_max_len=lpr_max_len,
phase=False,
class_num=len(CHARS),
dropout_rate=lpr_dropout_rate
)
LPRnet.to(torch.device(device))
LPRnet.load_state_dict(
torch.load(lpr_model_path)
)
for raw_frame in get_frames(video_file_path):
time_start = time.time()
proc_frame = preprocess(raw_frame, (640, 480))
results = detector.score_frame(proc_frame)
labls_cords = get_boxes(results, raw_frame)
new_cars = check_numbers_overlaps(labls_cords)
# list to write cars that've been defined
cars = []
for car in new_cars:
plate_coords = car[0]
car_coords = car[1]
if check_roi(plate_coords):
x1_car, y1_car = car_coords[0], car_coords[1]
x2_car, y2_car = car_coords[2], car_coords[3]
# define car's colour
car_box_image = raw_frame[y1_car:y2_car, x1_car:x2_car]
colour = detect_color(car_box_image)
car[1] = [car_coords, colour]
x1_plate, y1_plate = plate_coords[0], plate_coords[1]
x2_plate, y2_plate = plate_coords[2], plate_coords[3]
# define number on the plate
plate_box_image = raw_frame[y1_plate:y2_plate, x1_plate:x2_plate]
plate_text = rec_plate(LPRnet, plate_box_image)
# check if number mutchs russian number type
if (
not re.match("[A-Z]{1}[0-9]{3}[A-Z]{2}[0-9]{2,3}", plate_text)
is None
):
car[0] = [plate_coords, plate_text + "_OK"]
else:
car[0] = [plate_coords, plate_text + "_NOK"]
cars.append(car)
drawn_frame = plot_boxes(cars, raw_frame)
proc_frame = preprocess(drawn_frame, settings.FINAL_FRAME_RES)
time_end = time.time()
cv2.imshow("video", proc_frame)
# wait 5 sec if push 's'
if cv2.waitKey(30) & 0xFF == ord("s"):
time.sleep(5)
if cv2.waitKey(30) & 0xFF == ord("q"):
break
if __name__ == "__main__":
main(
settings.FILE_PATH,
settings.YOLO_MODEL_PATH,
settings.YOLO_CONF,
settings.YOLO_IOU,
settings.LPR_MODEL_PATH,
settings.LPR_MAX_LEN,
settings.LPR_DROPOUT,
settings.DEVICE
)