Last active
March 29, 2022 07:50
-
-
Save adioshun/72106c82674fd6cd7b06fe9105c2ab86 to your computer and use it in GitHub Desktop.
Tracking multiple objects with OpenCV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://www.pyimagesearch.com/2018/08/06/tracking-multiple-objects-with-opencv/ | |
# USAGE | |
# python multi_object_tracking.py --video videos/soccer_01.mp4 --tracker csrt | |
# import the necessary packages | |
from imutils.video import VideoStream | |
import argparse | |
import imutils | |
import time | |
import cv2 | |
# construct the argument parser and parse the arguments | |
ap = argparse.ArgumentParser() | |
ap.add_argument("-v", "--video", type=str, | |
help="path to input video file") | |
ap.add_argument("-t", "--tracker", type=str, default="kcf", | |
help="OpenCV object tracker type") | |
args = vars(ap.parse_args()) | |
# initialize a dictionary that maps strings to their corresponding | |
# OpenCV object tracker implementations | |
OPENCV_OBJECT_TRACKERS = { | |
"csrt": cv2.TrackerCSRT_create, | |
"kcf": cv2.TrackerKCF_create, | |
"boosting": cv2.TrackerBoosting_create, | |
"mil": cv2.TrackerMIL_create, | |
"tld": cv2.TrackerTLD_create, | |
"medianflow": cv2.TrackerMedianFlow_create, | |
"mosse": cv2.TrackerMOSSE_create | |
} | |
# initialize OpenCV's special multi-object tracker | |
trackers = cv2.MultiTracker_create() | |
# if a video path was not supplied, grab the reference to the web cam | |
if not args.get("video", False): | |
print("[INFO] starting video stream...") | |
vs = VideoStream(src=0).start() | |
time.sleep(1.0) | |
# otherwise, grab a reference to the video file | |
else: | |
vs = cv2.VideoCapture(args["video"]) | |
# loop over frames from the video stream | |
while True: | |
# grab the current frame, then handle if we are using a | |
# VideoStream or VideoCapture object | |
frame = vs.read() | |
frame = frame[1] if args.get("video", False) else frame | |
# check to see if we have reached the end of the stream | |
if frame is None: | |
break | |
# resize the frame (so we can process it faster) | |
frame = imutils.resize(frame, width=600) | |
# grab the updated bounding box coordinates (if any) for each | |
# object that is being tracked | |
(success, boxes) = trackers.update(frame) | |
# loop over the bounding boxes and draw then on the frame | |
for box in boxes: | |
(x, y, w, h) = [int(v) for v in box] | |
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) | |
# show the output frame | |
cv2.imshow("Frame", frame) | |
key = cv2.waitKey(1) & 0xFF | |
# if the 's' key is selected, we are going to "select" a bounding | |
# box to track | |
if key == ord("s"): | |
# select the bounding box of the object we want to track (make | |
# sure you press ENTER or SPACE after selecting the ROI) | |
box = cv2.selectROI("Frame", frame, fromCenter=False, | |
showCrosshair=True) | |
# create a new object tracker for the bounding box and add it | |
# to our multi-object tracker | |
tracker = OPENCV_OBJECT_TRACKERS[args["tracker"]]() | |
trackers.add(tracker, frame, box) | |
# if the `q` key was pressed, break from the loop | |
elif key == ord("q"): | |
break | |
# if we are using a webcam, release the pointer | |
if not args.get("video", False): | |
vs.stop() | |
# otherwise, release the file pointer | |
else: | |
vs.release() | |
# close all windows | |
cv2.destroyAllWindows() |
update the tracker methods to cv2.TrackerCSRT_create() from cv2.TrackerCSRT_create if you face compilation issues
and use a fresh pip install of opencv by "pip install opencv-contrib-python==4.2.0.34"
That is because the tutorial has tabs button as space. If you make the tabs and spaces to single format, it will work
This code is only running the videos and not giving any option to create bounding boxes..
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am trying to save the output file (.avi) and the bounding box (initBB). I tried to use cv2.videowriter but it shows indent error.