|
| 1 | +import cv2 as cv |
| 2 | +from motrackers.detectors import Caffe_SSDMobileNet |
| 3 | +from motrackers import CentroidTracker, CentroidKF_Tracker, SORT, IOUTracker |
| 4 | +from motrackers.utils import draw_tracks |
| 5 | + |
| 6 | + |
| 7 | +def main(video_path, weights_path, config_path, use_gpu, tracker): |
| 8 | + |
| 9 | + model = Caffe_SSDMobileNet( |
| 10 | + weights_path=weights_path, |
| 11 | + configfile_path=config_path, |
| 12 | + confidence_threshold=0.5, |
| 13 | + nms_threshold=0.2, |
| 14 | + draw_bboxes=True, |
| 15 | + use_gpu=use_gpu) |
| 16 | + |
| 17 | + cap = cv.VideoCapture(video_path) |
| 18 | + while True: |
| 19 | + ok, image = cap.read() |
| 20 | + |
| 21 | + if not ok: |
| 22 | + print("Cannot read the video feed.") |
| 23 | + break |
| 24 | + |
| 25 | + image = cv.resize(image, (700, 500)) |
| 26 | + |
| 27 | + bboxes, confidences, class_ids = model.detect(image) |
| 28 | + tracks = tracker.update(bboxes, confidences, class_ids) |
| 29 | + updated_image = model.draw_bboxes(image.copy(), bboxes, confidences, class_ids) |
| 30 | + |
| 31 | + updated_image = draw_tracks(updated_image, tracks) |
| 32 | + |
| 33 | + cv.imshow("image", updated_image) |
| 34 | + if cv.waitKey(1) & 0xFF == ord('q'): |
| 35 | + break |
| 36 | + |
| 37 | + cap.release() |
| 38 | + cv.destroyAllWindows() |
| 39 | + |
| 40 | + |
| 41 | +if __name__ == '__main__': |
| 42 | + import argparse |
| 43 | + |
| 44 | + parser = argparse.ArgumentParser( |
| 45 | + description='Object detections in input video using Caffemodel of MobileNetSSD.') |
| 46 | + |
| 47 | + parser.add_argument( |
| 48 | + '--video', '-v', type=str, default="./../video_data/cars.mp4", help='Input video path.') |
| 49 | + |
| 50 | + parser.add_argument( |
| 51 | + '--weights', '-w', type=str, default="./../pretrained_models/caffemodel_weights/MobileNetSSD_deploy.caffemodel", |
| 52 | + help='path to weights file of Caffe-MobileNetSSD, i.e., `.caffemodel` file.') |
| 53 | + |
| 54 | + parser.add_argument( |
| 55 | + '--config', '-c', type=str, default="./../pretrained_models/caffemodel_weights/MobileNetSSD_deploy.prototxt", |
| 56 | + help='path to config file of Caffe-MobileNetSSD, i.e., `.prototxt` file.') |
| 57 | + |
| 58 | + parser.add_argument( |
| 59 | + '--gpu', type=bool, default=False, help='Flag to use gpu to run the deep learning model. Default is `False`') |
| 60 | + |
| 61 | + parser.add_argument( |
| 62 | + '--tracker', type=str, default='IOUTracker', help="Tracker used to track objects." |
| 63 | + " Options include ['CentroidTracker', 'CentroidKF_Tracker', 'SORT', IOUTracker]") |
| 64 | + |
| 65 | + args = parser.parse_args() |
| 66 | + |
| 67 | + if args.tracker == 'CentroidTracker': |
| 68 | + tracker = CentroidTracker(max_lost=0, tracker_output_format='mot_challenge') |
| 69 | + elif args.tracker == 'CentroidKF_Tracker': |
| 70 | + tracker = CentroidKF_Tracker(max_lost=0, tracker_output_format='mot_challenge') |
| 71 | + elif args.tracker == 'SORT': |
| 72 | + tracker = SORT(max_lost=3, tracker_output_format='mot_challenge', iou_threshold=0.3) |
| 73 | + elif args.tracker == 'IOUTracker': |
| 74 | + tracker = IOUTracker(max_lost=2, iou_threshold=0.5, min_detection_confidence=0.4, max_detection_confidence=0.7, |
| 75 | + tracker_output_format='mot_challenge') |
| 76 | + else: |
| 77 | + raise NotImplementedError |
| 78 | + |
| 79 | + main(args.video, args.weights, args.config, args.gpu, tracker) |
0 commit comments