-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathyolov3-video-detection.py
342 lines (267 loc) · 11.3 KB
/
yolov3-video-detection.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# -*- coding: utf-8 -*-
"""
Objects Detection on Image with YOLO v3 and OpenCV
File: yolov3-video-detection.py
"""
# Detecting Objects on Image with OpenCV deep learning library
#
# How does YOLO-v3 Algorithm works for this example case:
# STEP1: Reading input video
# STEP2: Loading YOLO v3 Network
# STEP3: Reading frames in the loop
# STEP4: Getting blob from the frame
# STEP4: Implementing Forward Pass
# STEP5: Getting Bounding Boxes
# STEP6: Non-maximum Suppression
# STEP7: Drawing Bounding Boxes with Labels
# STEP8: creating a new video by writing processed frames
#
# Result:
# New video file with Detected Objects, Bounding Boxes and Labels
# Importing needed libraries
import numpy as np
import cv2
import time
print (cv2.__version__)
"""
================== STEP1 ===================
Start of: Reading input video
"""
#NOTE:
# Defining 'VideoCapture' object and reading video from a file make sure that the path and file name is correct
video = cv2.VideoCapture('videos/demo-traffic-cars.mp4')
# Preparing variable for writer that we will use to write processed frames
writer = None
# Preparing variables for spatial dimensions of the frames
h, w = None, None
"""
End of:
Reading input video
"""
"""
================== STEP2 ===================
Start of: Loading YOLO v3 network
"""
# Loading COCO class labels from file and Opening file
with open('yolo-coco-data/coco.names') as f:
# Getting labels reading every line
# and putting them into the list
labels = [line.strip() for line in f]
# # Check point
# print('List with labels names:')
# print(labels)
# Loading trained YOLO v3 Objects Detector with the help of 'dnn' library from OpenCV
network = cv2.dnn.readNetFromDarknet('yolo-coco-data/yolov3.cfg',
'yolo-coco-data/yolov3.weights')
# Getting list with names of all layers from YOLO v3 network
layers_names_all = network.getLayerNames()
# # Check point
# print()
# print(layers_names_all)
# Getting only output layers' names that we need from YOLO v3 algorithm
# with function that returns indexes of layers with unconnected outputs
layers_names_output = \
[layers_names_all[i[0] - 1] for i in network.getUnconnectedOutLayers()]
# # Check point
# print()
# print(layers_names_output) # ['yolo_82', 'yolo_94', 'yolo_106']
# Setting minimum probability to eliminate weak predictions
probability_minimum = 0.5
# Setting threshold for filtering weak bounding boxes
# with non-maximum suppression
threshold = 0.3
# Generating colours for representing every detected object
# with function randint(low, high=None, size=None, dtype='l')
colours = np.random.randint(0, 255, size=(len(labels), 3), dtype='uint8')
# # Check point
# print(colours.shape)
# print(colours[0])
"""
End of:
Loading YOLO v3 network
"""
"""
================== STEP3 ===================
Start of: Reading frames in the loop
"""
# Defining variable for counting frames at the end we will show total amount of processed frames
f = 0
# Defining variable for counting total time At the end we will show time spent for processing all frames
t = 0
# Defining loop for catching frames
while True:
# Capturing frame-by-frame
ret, frame = video.read()
# If the frame was not retrieved e.g.: at the end of the video, then we break the loop
if not ret:
break
# Getting spatial dimensions of the frame as we do it only once from the very beginning
# all other frames have the same dimension
if w is None or h is None:
# Slicing from tuple only first two elements
h, w = frame.shape[:2]
"""
End of: Reading frame in loop
"""
"""
================== STEP4 ===================
Start of: Getting blob from current frame
"""
# Getting blob from current frame
# The 'cv2.dnn.blobFromImage' function returns 4-dimensional blob from current
# frame after mean subtraction, normalizing, and RB channels swapping
# Resulted shape has number of frames, number of channels, width and height
# e.g.:
# blob = cv2.dnn.blobFromImage(image, scalefactor=1.0, size, mean, swapRB=True)
blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (416, 416),
swapRB=True, crop=False)
"""
End of: Getting blob from current frame
"""
"""
================== STEP4 ===================
Start of: Implementing Forward pass
"""
# Implementing forward pass with our blob and only through output layers
# Calculating at the same time, needed time for forward pass
network.setInput(blob) # setting blob as input to the network
start = time.time()
output_from_network = network.forward(layers_names_output)
end = time.time()
# Increasing counters for frames and total time
f += 1
t += end - start
# Showing spent time for single current frame
print('Frame number {0} took {1:.5f} seconds'.format(f, end - start))
"""
End of:Implementing Forward pass
"""
"""
================== STEP5 ===================
Start of: Getting bounding boxes
"""
# Preparing lists for detected bounding boxes, obtained confidences and class's number
bounding_boxes = []
confidences = []
classIDs = []
# Going through all output layers after feed forward pass
for result in output_from_network:
# Going through all detections from current output layer
for detected_objects in result:
# Getting 80 classes' probabilities for current detected object
scores = detected_objects[5:]
# Getting index of the class with the maximum value of probability
class_current = np.argmax(scores)
# Getting value of probability for defined class
confidence_current = scores[class_current]
# # Every 'detected_objects' numpy array has first 4 numbers with
# # bounding box coordinates and rest 80 with probabilities
# # for every class
# print(detected_objects.shape) # (85,)
# Eliminating weak predictions with minimum probability
if confidence_current > probability_minimum:
# Scaling bounding box coordinates to the initial frame size
# YOLO data format keeps coordinates for center of bounding box
# and its current width and height
# That is why we can just multiply them elementwise
# to the width and height
# of the original frame and in this way get coordinates for center
# of bounding box, its width and height for original frame
box_current = detected_objects[0:4] * np.array([w, h, w, h])
# Now, from YOLO data format, we can get top left corner coordinates that are x_min and y_min
x_center, y_center, box_width, box_height = box_current
x_min = int(x_center - (box_width / 2))
y_min = int(y_center - (box_height / 2))
# Adding results into prepared lists
bounding_boxes.append([x_min, y_min, int(box_width), int(box_height)])
confidences.append(float(confidence_current))
classIDs.append(class_current)
"""
End of: Getting bounding boxes
"""
"""
================== STEP6 ===================
Start of: Non-maximum suppression
"""
# Implementing non-maximum suppression of given bounding boxes
# With this technique we exclude some of bounding boxes if their
# corresponding confidences are low or there is another
# bounding box for this region with higher confidence
# It is needed to make sure that data type of the boxes is 'int'
# and data type of the confidences is 'float'
# https://github.com/opencv/opencv/issues/12789
results = cv2.dnn.NMSBoxes(bounding_boxes, confidences,
probability_minimum, threshold)
"""
End of: Non-maximum suppression
"""
"""
================== STEP6 ===================
Start of: Drawing bounding boxes and labels
"""
# Checking if there is at least one detected object after non-maximum suppression
if len(results) > 0:
# Going through indexes of results
for i in results.flatten():
# Getting current bounding box coordinates, its width and height
x_min, y_min = bounding_boxes[i][0], bounding_boxes[i][1]
box_width, box_height = bounding_boxes[i][2], bounding_boxes[i][3]
# Preparing colour for current bounding box and converting from numpy array to list
colour_box_current = colours[classIDs[i]].tolist()
# print(type(colour_box_current)) # <class 'list'>
# print(colour_box_current) # [172 , 10, 127]
# Drawing bounding box on the original current frame
cv2.rectangle(frame, (x_min, y_min),
(x_min + box_width, y_min + box_height),
colour_box_current, 2)
# Preparing text with label and confidence for current bounding box
text_box_current = '{}: {:.4f}'.format(labels[int(classIDs[i])],
confidences[i])
# Putting text with label and confidence on the original image
cv2.putText(frame, text_box_current, (x_min, y_min - 5),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, colour_box_current, 2)
"""
End of:
Drawing bounding boxes and labels
"""
"""
================== STEP7 ===================
Start of: Writing processed frame into the file
"""
# Initializing writer
# we do it only once from the very beginning when we get spatial dimensions of the frames
if writer is None:
# Constructing code of the codec to be used in the function VideoWriter
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
# Writing current processed frame into the video file
writer = cv2.VideoWriter('videos/result-traffic-cars.mp4', fourcc, 30,
(frame.shape[1], frame.shape[0]), True)
# Write processed current frame to the file
writer.write(frame)
"""
End of: Writing processed frame into the file
"""
"""
End of: Writing processed frame into the file
"""
# Printing final results
print()
print('Total number of frames', f)
print('Total amount of time {:.5f} seconds'.format(t))
print('FPS:', round((f / t), 1))
# Releasing video reader and writer
video.release()
writer.release()
"""
Some comments
What is a FOURCC?
FOURCC is short for "four character code" - an identifier for a video codec,
compression format, colour or pixel format used in media files.
http://www.fourcc.org
Parameters for cv2.VideoWriter():
filename - Name of the output video file.
fourcc - 4-character code of codec used to compress the frames.
fps - Frame rate of the created video.
frameSize - Size of the video frames.
isColor - If it True, the encoder will expect and encode colour frames.
"""