-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathyolov3-camera-realtime-detection.py
306 lines (241 loc) · 9.87 KB
/
yolov3-camera-realtime-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
# -*- coding: utf-8 -*-
"""
Objects Detection in Real Time with YOLO v3 and OpenCV
File: yolov3-camera-realtime-detection.py
"""
# Detecting Objects in Real Time with OpenCV deep learning library
#
# How does YOLO-v3 Algorithm works for this example case:
# STEP1: Reading stream video from camera
# STEP2: Loading YOLO v3 Network
# STEP3: Implementing Forward Pass
# STEP4: Getting blob from the frame
# STEP5: Getting Bounding Boxes
# STEP6: Non-maximum Suppression
# STEP7: Drawing Bounding Boxes with Labels
# STEP8: Showing processed frames in OpenCV Window
# Result:
# Window with Detected Objects, Bounding Boxes and Labels in Real Time
# Importing needed libraries
import numpy as np
import cv2
import time
print (cv2.__version__)
"""
================== STEP1 ===================
Start of: Reading stream video from camera
"""
# Defining 'VideoCapture' object and reading stream video from camera
camera = cv2.VideoCapture(0)
# Preparing variables for spatial dimensions of the frames
h, w = None, None
"""
End of: Reading stream video from camera
"""
"""
================== STEP2 ===================
Start of: Loading YOLO v3 network
"""
# Loading COCO class labels from 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]
# 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()
# 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()]
# 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()
# print(type(colours)) # <class 'numpy.ndarray'>
# print(colours.shape)
# print(colours[0])
"""
End of:
Loading YOLO v3 network
"""
"""
================== STEP3 ===================
Start of: Reading frames in the loop
"""
# Defining loop for catching frames
while True:
# Capturing frame-by-frame from camera
_, frame = camera.read()
# Getting spatial dimensions of the frame 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]
"""
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
# eg.:
# 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()
# Showing spent time for single current frame
print('Current frame took {:.5f} seconds'.format(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
"""
"""
================== STEP7 ===================
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
"""
"""
================== STEP8 ===================
Start of: Showing processed frames in OpenCV Window
"""
# Showing results obtained from camera in Real Time
# Showing current frame with detected objects
# Giving name to the window with current frame
# And specifying that window is resizable
cv2.namedWindow('YOLO v3 Real Time Detections', cv2.WINDOW_NORMAL)
# Pay attention! 'cv2.imshow' takes images in BGR format
cv2.imshow('YOLO v3 Real Time Detections', frame)
# Breaking the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
"""
End of:
Showing processed frames in OpenCV Window
"""
"""
End of:
Reading frames in the loop
"""
# Releasing camera
camera.release()
# Destroying all opened OpenCV windows
cv2.destroyAllWindows()
"""
Some comments
cv2.VideoCapture(0)
To capture video, it is needed to create VideoCapture object.
Its argument can be camera's index or name of video file.
Camera index is usually 0 for built-in one.
Try to select other cameras by passing 1, 2, 3, etc.
"""