import cv2
import numpy as np
OPENCV_OBJECT_TRACKERS = {
'boosting' : cv2.TrackerBoosting_create,
'csrt' : cv2.TrackerCSRT_create,
'kcf' : cv2.TrackerKCF_create,
'mil': cv2.TrackerMIL_create,
'tld': cv2.TrackerTLD_create,
'medianflow': cv2.TrackerMedianFlow_create,
'mosse' : cv2.TrackerMOSSE_create
}
trackers = cv2.legacy.MultiTracker.create()
cap = cv2. VideoCapture ('./videos/soccer_01.mp4')
while True:
flag,frame = cap.read()
if frame is None:
break
success,boxes = trackers. update(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)
cv2.imshow('frame', frame)
key = cv2.waitKey(100)
if key == ord('s'):
roi = cv2.selectROI('frame',frame,showCrosshair=True,fromCenter=False)
tracker = OPENCV_OBJECT_TRACKERS['kcf']()
trackers.add(tracker,frame,roi)
elif key == 27:
break
cap.release()
cv2.destroyAllWindows()