Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/Deep-Learning-with-OpenCV-DNN-Module/python/detection/detect_vid.py
3150 views
1
import cv2
2
import time
3
import numpy as np
4
5
# load the COCO class names
6
with open('../../input/object_detection_classes_coco.txt', 'r') as f:
7
class_names = f.read().split('\n')
8
9
# get a different color array for each of the classes
10
COLORS = np.random.uniform(0, 255, size=(len(class_names), 3))
11
12
# load the DNN model
13
model = cv2.dnn.readNet(model='../../input/frozen_inference_graph.pb',
14
config='../../input/ssd_mobilenet_v2_coco_2018_03_29.pbtxt.txt',
15
framework='TensorFlow')
16
17
# capture the video
18
cap = cv2.VideoCapture('../../input/video_1.mp4')
19
# get the video frames' width and height for proper saving of videos
20
frame_width = int(cap.get(3))
21
frame_height = int(cap.get(4))
22
# create the `VideoWriter()` object
23
out = cv2.VideoWriter('../../outputs/video_result.mp4', cv2.VideoWriter_fourcc(*'mp4v'), 30,
24
(frame_width, frame_height))
25
26
# detect objects in each frame of the video
27
while cap.isOpened():
28
ret, frame = cap.read()
29
if ret:
30
image = frame
31
image_height, image_width, _ = image.shape
32
# create blob from image
33
blob = cv2.dnn.blobFromImage(image=image, size=(300, 300), mean=(104, 117, 123),
34
swapRB=True)
35
# start time to calculate FPS
36
start = time.time()
37
model.setInput(blob)
38
output = model.forward()
39
# end time after detection
40
end = time.time()
41
# calculate the FPS for current frame detection
42
fps = 1 / (end-start)
43
# loop over each of the detections
44
for detection in output[0, 0, :, :]:
45
# extract the confidence of the detection
46
confidence = detection[2]
47
# draw bounding boxes only if the detection confidence is above...
48
# ... a certain threshold, else skip
49
if confidence > .4:
50
# get the class id
51
class_id = detection[1]
52
# map the class id to the class
53
class_name = class_names[int(class_id)-1]
54
color = COLORS[int(class_id)]
55
# get the bounding box coordinates
56
box_x = detection[3] * image_width
57
box_y = detection[4] * image_height
58
# get the bounding box width and height
59
box_width = detection[5] * image_width
60
box_height = detection[6] * image_height
61
# draw a rectangle around each detected object
62
cv2.rectangle(image, (int(box_x), int(box_y)), (int(box_width), int(box_height)), color, thickness=2)
63
# put the class name text on the detected object
64
cv2.putText(image, class_name, (int(box_x), int(box_y - 5)), cv2.FONT_HERSHEY_SIMPLEX, 1, color, 2)
65
# put the FPS text on top of the frame
66
cv2.putText(image, f"{fps:.2f} FPS", (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
67
68
cv2.imshow('image', image)
69
out.write(image)
70
if cv2.waitKey(10) & 0xFF == ord('q'):
71
break
72
else:
73
break
74
75
cap.release()
76
cv2.destroyAllWindows()
77
78