|
| 1 | +# USAGE |
| 2 | +# python multi_object_tracking.py --video videos/soccer_01.mp4 --tracker csrt |
| 3 | + |
| 4 | +# import the necessary packages |
| 5 | +from imutils.video import VideoStream |
| 6 | +import argparse |
| 7 | +import imutils |
| 8 | +import time |
| 9 | +import cv2 |
| 10 | + |
| 11 | +# construct the argument parser and parse the arguments |
| 12 | +ap = argparse.ArgumentParser() |
| 13 | +ap.add_argument("-v", "--video", type=str, |
| 14 | + help="path to input video file") |
| 15 | +ap.add_argument("-t", "--tracker", type=str, default="kcf", |
| 16 | + help="OpenCV object tracker type") |
| 17 | +args = vars(ap.parse_args()) |
| 18 | + |
| 19 | +# initialize a dictionary that maps strings to their corresponding |
| 20 | +# OpenCV object tracker implementations |
| 21 | +OPENCV_OBJECT_TRACKERS = { |
| 22 | + "csrt": cv2.TrackerCSRT_create, |
| 23 | + "kcf": cv2.TrackerKCF_create, |
| 24 | + "boosting": cv2.TrackerBoosting_create, |
| 25 | + "mil": cv2.TrackerMIL_create, |
| 26 | + "tld": cv2.TrackerTLD_create, |
| 27 | + "medianflow": cv2.TrackerMedianFlow_create, |
| 28 | + "mosse": cv2.TrackerMOSSE_create |
| 29 | +} |
| 30 | + |
| 31 | +# initialize OpenCV's special multi-object tracker |
| 32 | +trackers = cv2.MultiTracker_create() |
| 33 | + |
| 34 | +# if a video path was not supplied, grab the reference to the web cam |
| 35 | +if not args.get("video", False): |
| 36 | + print("[INFO] starting video stream...") |
| 37 | + vs = VideoStream(src=0).start() |
| 38 | + time.sleep(1.0) |
| 39 | + |
| 40 | +# otherwise, grab a reference to the video file |
| 41 | +else: |
| 42 | + vs = cv2.VideoCapture(args["video"]) |
| 43 | + |
| 44 | +# loop over frames from the video stream |
| 45 | +while True: |
| 46 | + # grab the current frame, then handle if we are using a |
| 47 | + # VideoStream or VideoCapture object |
| 48 | + frame = vs.read() |
| 49 | + frame = frame[1] if args.get("video", False) else frame |
| 50 | + |
| 51 | + # check to see if we have reached the end of the stream |
| 52 | + if frame is None: |
| 53 | + break |
| 54 | + |
| 55 | + # resize the frame (so we can process it faster) |
| 56 | + frame = imutils.resize(frame, width=600) |
| 57 | + |
| 58 | + # grab the updated bounding box coordinates (if any) for each |
| 59 | + # object that is being tracked |
| 60 | + (success, boxes) = trackers.update(frame) |
| 61 | + |
| 62 | + # loop over the bounding boxes and draw then on the frame |
| 63 | + for box in boxes: |
| 64 | + (x, y, w, h) = [int(v) for v in box] |
| 65 | + cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) |
| 66 | + |
| 67 | + # show the output frame |
| 68 | + cv2.imshow("Frame", frame) |
| 69 | + key = cv2.waitKey(1) & 0xFF |
| 70 | + |
| 71 | + # if the 's' key is selected, we are going to "select" a bounding |
| 72 | + # box to track |
| 73 | + if key == ord("s"): |
| 74 | + # select the bounding box of the object we want to track (make |
| 75 | + # sure you press ENTER or SPACE after selecting the ROI) |
| 76 | + box = cv2.selectROI("Frame", frame, fromCenter=False, |
| 77 | + showCrosshair=True) |
| 78 | + |
| 79 | + # create a new object tracker for the bounding box and add it |
| 80 | + # to our multi-object tracker |
| 81 | + tracker = OPENCV_OBJECT_TRACKERS[args["tracker"]]() |
| 82 | + trackers.add(tracker, frame, box) |
| 83 | + |
| 84 | + # if the `q` key was pressed, break from the loop |
| 85 | + elif key == ord("q"): |
| 86 | + break |
| 87 | + |
| 88 | +# if we are using a webcam, release the pointer |
| 89 | +if not args.get("video", False): |
| 90 | + vs.stop() |
| 91 | + |
| 92 | +# otherwise, release the file pointer |
| 93 | +else: |
| 94 | + vs.release() |
| 95 | + |
| 96 | +# close all windows |
| 97 | +cv2.destroyAllWindows() |
0 commit comments