cap = cv2.VideoCapture(video_path) if (cap.isOpened() == False): print('Error while trying to read video. Please check path again') # Get the frame width and height. frame_width = int(cap.get(3)) frame_height = int(cap.get(4)) # Pass the first frame through `letterbox` function to get the resized image, # to be used for `VideoWriter` dimensions. Resize by larger side. vid_write_image = letterbox(cap.read()[1], (frame_width), stride=64, auto=True)[0] resize_height, resize_width = vid_write_image.shape[:2] save_name = f"{video_path.split('/')[-1].split('.')[0]}" # Define codec and create VideoWriter object . out = cv2.VideoWriter(f"{save_name}_keypoint.mp4", cv2.VideoWriter_fourcc(*'mp4v'), 30, (resize_width, resize_height)) frame_count = 0 # To count total frames. total_fps = 0 # To get the final frames per second.