How to do it...

To complete this recipe, perform the following steps:

  1. Import the module:
import cv2
  1. 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
  1. Open the test video file and select an object:
video = cv2.VideoCapture('../data/traffic.mp4')bbox = (878, 266, 1153-878, 475-266)
  1. 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 ...

Get OpenCV 3 Computer Vision with Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.