The steps for this recipe are:
- Import all of the necessary modules:
import cv2import numpy as np
- 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()
- 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, ...