How to do it...

The steps for this recipe are:

  1. Import all of the necessary modules:
import cv2import numpy as np
  1. Open a video file, read its frame, and select an object to track:
cap = cv2.VideoCapture("../data/traffic.mp4")_, frame = cap.read()bbox = cv2.selectROI(frame, False, True)cv2.destroyAllWindows()
  1. Create the Median Flow tracker and initialize it with the first frame from the video and the bounding box we've selected. Then, read the remaining frames one-by-one, feed them into the tracker, and get a new bounding box for each frame. Display the bounding box, as well as the number of frames that the Median Flow algorithm is able to process each second:
tracker = cv2.TrackerMedianFlow_create()status_tracker = tracker.init(frame, ...

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.