pythonopencvvideo-capture

Decreasing Frames extracted per sec in openCV with Python


I want to decrease the frames extracted. Currently the below python script extracts around 30 frames per sec but I want 12-14 frames extracted per sec.

How can I change the code to decrease it?

import numpy as np
import os
from glob import glob
import cv2
def create_dir(path):
    try:
        if not os.path.exists(path):
            os.makedirs(path)
    except OSError:
        print(f"ERROR: creating directory with name {path}")
def save_frame(video_path, save_dir):
    name = video_path.split("/")[-1].split(".")[0]
    save_path = os.path.join(save_dir, name)
    create_dir(save_path)
    
    cap = cv2.VideoCapture(video_path)
    idx=0
    
    while True:
        ret, frame = cap.read()
        
        if ret==False:
            cap.release()
            break
        
        cv2.imwrite(f"{save_path}/{idx}.png", frame)
        idx += 1
video_paths = glob("video/*")
save_dir = "frames"
for path in video_paths:
    save_frame(path, save_dir)

Solution

  • Just set the frame rate of the camera. It might be able to capture at the requested frame rate.

    cap = ...
    cap.set(cv.CAP_PROP_FPS, 15)
    ...