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