Now that we have a Pedestrian class to maintain data about the tracking of each pedestrian, let's implement our program's main function. We will look at the parts of the implementation sequentially:
- We begin by loading a video file, initializing a background subtractor, and setting the background subtractor's history length (that is, the number of frames affecting the background model):
def main(): cap = cv2.VideoCapture('pedestrians.avi') # Create the KNN background subtractor. bg_subtractor = cv2.createBackgroundSubtractorKNN() history_length = 20 bg_subtractor.setHistory(history_length)
- Then, we define morphology kernels:
erode_kernel = cv2.getStructuringElement( cv2.MORPH_ELLIPSE, (3, 3))dilate_kernel ...