-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.py
169 lines (134 loc) · 5.64 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
import cv2
import numpy as np
from art import tprint
def apply_yolo_object_detection(image_to_process):
"""
Recognition and determination of the coordinates of objects on the image
:param image_to_process: original image
:return: image with marked objects and captions to them
"""
height, width, _ = image_to_process.shape
blob = cv2.dnn.blobFromImage(image_to_process, 1 / 255, (608, 608),
(0, 0, 0), swapRB=True, crop=False)
net.setInput(blob)
outs = net.forward(out_layers)
class_indexes, class_scores, boxes = ([] for i in range(3))
objects_count = 0
# Starting a search for objects in an image
for out in outs:
for obj in out:
scores = obj[5:]
class_index = np.argmax(scores)
class_score = scores[class_index]
if class_score > 0:
center_x = int(obj[0] * width)
center_y = int(obj[1] * height)
obj_width = int(obj[2] * width)
obj_height = int(obj[3] * height)
box = [center_x - obj_width // 2, center_y - obj_height // 2,
obj_width, obj_height]
boxes.append(box)
class_indexes.append(class_index)
class_scores.append(float(class_score))
# Selection
chosen_boxes = cv2.dnn.NMSBoxes(boxes, class_scores, 0.0, 0.4)
for box_index in chosen_boxes:
box_index = box_index
box = boxes[box_index]
class_index = class_indexes[box_index]
# For debugging, we draw objects included in the desired classes
if classes[class_index] in classes_to_look_for:
objects_count += 1
image_to_process = draw_object_bounding_box(image_to_process,
class_index, box)
final_image = draw_object_count(image_to_process, objects_count)
return final_image
def draw_object_bounding_box(image_to_process, index, box):
"""
Drawing object borders with captions
:param image_to_process: original image
:param index: index of object class defined with YOLO
:param box: coordinates of the area around the object
:return: image with marked objects
"""
x, y, w, h = box
start = (x, y)
end = (x + w, y + h)
color = (0, 255, 0)
width = 2
final_image = cv2.rectangle(image_to_process, start, end, color, width)
start = (x, y - 10)
font_size = 1
font = cv2.FONT_HERSHEY_SIMPLEX
width = 2
text = classes[index]
final_image = cv2.putText(final_image, text, start, font,
font_size, color, width, cv2.LINE_AA)
return final_image
def draw_object_count(image_to_process, objects_count):
"""
Signature of the number of found objects in the image
:param image_to_process: original image
:param objects_count: the number of objects of the desired class
:return: image with labeled number of found objects
"""
start = (10, 120)
font_size = 1.5
font = cv2.FONT_HERSHEY_SIMPLEX
width = 3
text = "Objects found: " + str(objects_count)
# Text output with a stroke
# (so that it can be seen in different lighting conditions of the picture)
white_color = (255, 255, 255)
black_outline_color = (0, 0, 0)
final_image = cv2.putText(image_to_process, text, start, font, font_size,
black_outline_color, width * 3, cv2.LINE_AA)
final_image = cv2.putText(final_image, text, start, font, font_size,
white_color, width, cv2.LINE_AA)
return final_image
def start_video_object_detection(video: str):
"""
Захват и анализ видео в режиме реального времени
"""
while True:
try:
# Capturing a picture from a video
video_camera_capture = cv2.VideoCapture(video)
while video_camera_capture.isOpened():
ret, frame = video_camera_capture.read()
if not ret:
break
# Application of object recognition methods on a video frame from YOLO
frame = apply_yolo_object_detection(frame)
# Displaying the processed image on the screen with a reduced window size
frame = cv2.resize(frame, (1920 // 2, 1080 // 2))
cv2.imshow("Video Capture", frame)
cv2.waitKey(1)
video_camera_capture.release()
cv2.destroyAllWindows()
except KeyboardInterrupt:
pass
if __name__ == '__main__':
# Logo
tprint("Object detection")
tprint("by")
tprint("paveldat")
# Loading YOLO scales from files and setting up the network
net = cv2.dnn.readNetFromDarknet("Resources/yolov4-tiny.cfg",
"Resources/yolov4-tiny.weights")
layer_names = net.getLayerNames()
out_layers_indexes = net.getUnconnectedOutLayers()
out_layers = [layer_names[index - 1] for index in out_layers_indexes]
# Loading from a file of object classes that YOLO can detect
with open("Resources/coco.names.txt") as file:
classes = file.read().split("\n")
# Determining classes that will be prioritized for search in an image
# The names are in the file coco.names.txt
video = input("Path to video (or URL): ")
look_for = input("What we are looking for: ").split(',')
# Delete spaces
list_look_for = []
for look in look_for:
list_look_for.append(look.strip())
classes_to_look_for = list_look_for
start_video_object_detection(video)