Path: blob/master/Deep-Learning-with-OpenCV-DNN-Module/python/detection/detect_vid.py
3150 views
import cv21import time2import numpy as np34# load the COCO class names5with open('../../input/object_detection_classes_coco.txt', 'r') as f:6class_names = f.read().split('\n')78# get a different color array for each of the classes9COLORS = np.random.uniform(0, 255, size=(len(class_names), 3))1011# load the DNN model12model = cv2.dnn.readNet(model='../../input/frozen_inference_graph.pb',13config='../../input/ssd_mobilenet_v2_coco_2018_03_29.pbtxt.txt',14framework='TensorFlow')1516# capture the video17cap = cv2.VideoCapture('../../input/video_1.mp4')18# get the video frames' width and height for proper saving of videos19frame_width = int(cap.get(3))20frame_height = int(cap.get(4))21# create the `VideoWriter()` object22out = cv2.VideoWriter('../../outputs/video_result.mp4', cv2.VideoWriter_fourcc(*'mp4v'), 30,23(frame_width, frame_height))2425# detect objects in each frame of the video26while cap.isOpened():27ret, frame = cap.read()28if ret:29image = frame30image_height, image_width, _ = image.shape31# create blob from image32blob = cv2.dnn.blobFromImage(image=image, size=(300, 300), mean=(104, 117, 123),33swapRB=True)34# start time to calculate FPS35start = time.time()36model.setInput(blob)37output = model.forward()38# end time after detection39end = time.time()40# calculate the FPS for current frame detection41fps = 1 / (end-start)42# loop over each of the detections43for detection in output[0, 0, :, :]:44# extract the confidence of the detection45confidence = detection[2]46# draw bounding boxes only if the detection confidence is above...47# ... a certain threshold, else skip48if confidence > .4:49# get the class id50class_id = detection[1]51# map the class id to the class52class_name = class_names[int(class_id)-1]53color = COLORS[int(class_id)]54# get the bounding box coordinates55box_x = detection[3] * image_width56box_y = detection[4] * image_height57# get the bounding box width and height58box_width = detection[5] * image_width59box_height = detection[6] * image_height60# draw a rectangle around each detected object61cv2.rectangle(image, (int(box_x), int(box_y)), (int(box_width), int(box_height)), color, thickness=2)62# put the class name text on the detected object63cv2.putText(image, class_name, (int(box_x), int(box_y - 5)), cv2.FONT_HERSHEY_SIMPLEX, 1, color, 2)64# put the FPS text on top of the frame65cv2.putText(image, f"{fps:.2f} FPS", (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)6667cv2.imshow('image', image)68out.write(image)69if cv2.waitKey(10) & 0xFF == ord('q'):70break71else:72break7374cap.release()75cv2.destroyAllWindows()767778