forked from GachiSlave/sofg_eng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
176 lines (143 loc) · 7.72 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
import cv2
import numpy as np
import yaml
from ultralytics import YOLO
from detection import compute_overlaps, draw_bbox
from telegram_utils import send_photo, send_message
# Загрузка конфигурации телеграм-бота и видео
with open('config.yaml', 'r') as file:
config = yaml.safe_load(file)
TOKEN = config['telegram']['TOKEN']
chat_id = config['telegram']['chat_id']
video_path = config['video']['path']
# Загрузка предварительно обученной YOLOv8 модели
model = YOLO('yolov8n.pt')
video_capture = cv2.VideoCapture(video_path)
check_det_frame = None
final_image = 0
first_frame_parking_spaces = None
free_parking_timer = 0
free_parking_timer_bag1 = 0
first_frame_parking_score = 0
free_parking_space = False
free_parking_space_box = None
telegram_message = False
x_free, y_free, w_free, h_free = 0, 0, 0, 0
# Работа модели с отображением результатов на видео, пока не нажата клавиша q
while video_capture.isOpened():
ret, image_to_process = video_capture.read()
if not ret:
break
# Определение класса машин ("2") в COCO
results = model.predict(image_to_process)
detections = results[0].boxes
class_indexes, class_scores, boxes = ([] for i in range(3))
for class_index in detections:
if class_index.cls.numpy() == 2:
box = class_index.xywh.numpy().astype(int)[0].tolist()
box = [box[0] - box[2] // 2, box[1] - box[3] // 2,
box[2], box[3]]
boxes.append(box)
class_scores.append(float(class_index.conf))
# Предполагаем, что под каждой стоячей машиной будет парковочное место
if not first_frame_parking_spaces:
first_frame_parking_spaces = boxes
first_frame_parking_score = class_scores
else:
chosen_cars_boxes = cv2.dnn.NMSBoxes(boxes, class_scores, 0.0, 0.4)
cars_area = []
# Распознавание машин
for box_index in chosen_cars_boxes:
car_box = boxes[box_index]
cars_area.append(car_box)
x, y, w, h = car_box
parking_text = 'Car'
final_image = draw_bbox(image_to_process, x, y, w, h, parking_text, (255, 255, 0))
cars_boxes = cars_area
# IoU
overlaps = compute_overlaps(np.array(first_frame_parking_spaces), np.array(cars_boxes))
for parking_space_one, area_overlap in zip(first_frame_parking_spaces, overlaps):
max_IoU = max(area_overlap)
sort_IoU = np.sort(area_overlap[area_overlap > 0])[::-1]
if not free_parking_space:
if 0.0 < max_IoU < 0.4:
# Количество парковочных мест по первому условию: 0.0 < IoU < 0.4
len_sort = len(sort_IoU)
# Количество парковочных мест по второму условию: IoU > 0.15
sort_IoU_2 = sort_IoU[sort_IoU > 0.15]
len_sort_2 = len(sort_IoU_2)
# Проверка двух условий для подсчета кадров с освободившимся местом
if (check_det_frame == parking_space_one) & (len_sort != len_sort_2):
free_parking_timer += 1
elif check_det_frame is None:
check_det_frame = parking_space_one
else:
# Фильтр от багов со "скачком" свободного парковочного места
free_parking_timer_bag1 += 1
if free_parking_timer_bag1 == 2:
# Обнуление счётчика
check_det_frame = parking_space_one
free_parking_timer = 0
# Условие освободившегося места (10 кадров подряд)
if free_parking_timer == 10:
# Пометка свободного места
free_parking_space = True
free_parking_space_box = parking_space_one
# Отрисовка рамки освободившегося парковочного места
x_free, y_free, w_free, h_free = parking_space_one
else:
# Освободившееся место заняли
overlaps = compute_overlaps(np.array([free_parking_space_box]), np.array(cars_boxes))
for area in overlaps:
max_IoU = max(area)
if max_IoU > 0.6:
free_parking_space = False
telegram_message = False
# Отправка сообщения телеграмм-ботом о занятом месте
screenshot_parking_space = final_image
message = 'Где ты ездишь??? Место уже занято :('
path = './image_test_not_free.png'
send_message(TOKEN, chat_id, message)
cv2.imwrite(path, screenshot_parking_space)
send_photo(TOKEN, chat_id, path)
# Распознавание парковочных мест
chosen_boxes = cv2.dnn.NMSBoxes(first_frame_parking_spaces,
first_frame_parking_score, 0.0, 0.4)
parking_spaces = []
for box_index in chosen_boxes:
box = first_frame_parking_spaces[box_index]
# Отрисовка освободившегося места
if free_parking_space:
if box == [x_free, y_free, w_free, h_free]:
parking_text = 'FREE SPACE!!!'
final_image = draw_bbox(image_to_process, x_free, y_free, w_free, h_free, parking_text, (0, 255, 0))
# Отправка сообщения телеграмм-ботом о свободном месте
if not telegram_message:
# Скриншот свободного места
screenshot_parking_space = final_image
message = 'Свободное место! Давай, жми скорее!!!'
path = './image_test_free.png'
send_message(TOKEN, chat_id, message)
cv2.imwrite(path, screenshot_parking_space)
send_photo(TOKEN, chat_id, path)
telegram_message = True
else:
# Координаты и размеры парковочного места
x, y, w, h = box
parking_text = 'No parking'
final_image = draw_bbox(image_to_process, x, y, w, h, parking_text)
else:
# Координаты и размеры парковочного места
x, y, w, h = box
parking_text = 'No parking'
final_image = draw_bbox(image_to_process, x, y, w, h, parking_text)
# Координаты парковочных мест, обнаруженных на первом кадре
parking_spaces.append(box)
# Отображение результатов работы модели
cv2.imshow("Parking Space", final_image)
# Прерывание отображения клавишей q
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Закрытие всех окон с кадрами после завершения
video_capture.release()
cv2.destroyAllWindows()