import cv2
import numpy as np
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
}
trackers = cv2.MultiTracker_create()
vs = cv2.VideoCapture("1.mp4")
while True:
frame = vs.read()
frame = frame[1]
if frame is None:
break
(h,w) = frame.shape[:2]
width = 600
r = width/float(w)
dim = (width,int(h*r))
frame = cv2.resize(frame,dim)
#追踪结果
(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) & 0xFF
if key == ord("s"):
box = cv2.selectROI("frame",frame)
tracker = OPENCV_OBJECT_TRACKERS["csrt"]()
trackers.add(tracker,frame,box)
if key == 27:
break
vs.release()
cv2.destroyAllWindows()