How to do it...

You need to complete the following steps:

  1. Import the necessary modules:
import cv2import numpy as np
  1. Define a function which handles a video file. This function takes each frame, match the keypoints for this frame and the one 40 frames before:
def video_keypoints(matcher, cap=cv2.VideoCapture("../data/traffic.mp4"),                     detector=cv2.ORB_create(40)):    cap.set(cv2.CAP_PROP_POS_FRAMES, 0)    while True:        status_cap, frame = cap.read()        frame = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5)        if not status_cap:            break        if (cap.get(cv2.CAP_PROP_POS_FRAMES) - 1) % 40 == 0:            key_frame = np.copy(frame)            key_points_1, descriptors_1 = detector.detectAndCompute(frame, None)        else:            key_points_2, descriptors_2 = detector.detectAndCompute(frame, None)

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.