To complete this recipe, perform the following steps:
- Import the module:
import cv2
- Create the main window and loop over the different trackers:
cv2.namedWindow('frame')for name, tracker in (('KCF', cv2.TrackerKCF_create), ('MIL', cv2.TrackerMIL_create), ('TLD', cv2.TrackerTLD_create)): tracker = tracker() initialized = False
- Open the test video file and select an object:
video = cv2.VideoCapture('../data/traffic.mp4')bbox = (878, 266, 1153-878, 475-266)
- Track until the video ends or ESC is pressed, and visualize the current tracked object:
while True: t0 = time.time() ok, frame = video.read() if not ok: break if initialized: tracked, bbox = tracker.update(frame) else: cv2.imwrite('/tmp/frame.png', frame) tracked ...