pythonopencvvideo-processingmjpeg

Changing FPS in OpenCV


I have an application that needs to capture only a few frames per second from a webcam. Setting videowriter in the below code to 3 frames per second results in the webcam's normal framerate of approximately 30 fps being saved. What are the options to save only the recorded 3 frames per second, and let the other 27 or so go? Thanks in advance.

import cv2
import numpy as np
import time
import datetime
import pathlib
import imutils

cap = cv2.VideoCapture(0)

if (cap.isOpened() == False): 
  print("Unable to read camera feed")

capture_duration = 15
frame_per_sec = 3
frame_width = 80
frame_height = 60

out = cv2.VideoWriter('C:\\Users\\student\\Desktop\\videoFile.avi',cv2.VideoWriter_fourcc('m','j','p','g'),frame_per_sec, (frame_width,frame_height))

start_time = time.time()
while( int(time.time() - start_time) < capture_duration ):
    ret, frame = cap.read()
    if ret==True:
        frame = imutils.resize(frame, width=frame_width)
        out.write(frame)
        cv2.imshow('frame',frame) 
        if cv2.waitKey(1) & 0xFF == ord('q'):
          break
    else:
        break

cap.release()
out.release()
 
cv2.destroyAllWindows()

Solution

  • This method programmatically sets frames per second. A 6.1mb file was created when frame rate was set for 30fps, and a 0.9mb file when set for 3fps.

    #!/usr/bin/env python3
    
    import cv2
    import numpy as np
    import time
    import datetime
    import pathlib
    import imutils
    
    cap = cv2.VideoCapture(0)
    
    if (cap.isOpened() == False): 
      print("Unable to read camera feed")
    
    capture_duration = 15
    frame_per_sec = 30
    prev = 0
    frame_width = int(cap.get(3))
    frame_height = int(cap.get(4))
    out = cv2.VideoWriter('C:\\videoPy\\LZ\\'outpout.avi',cv2.VideoWriter_fourcc('m','j','p','g'),frame_per_sec, (frame_width,frame_height))
    start_time = time.time()
    while( int(time.time() - start_time) < capture_duration ): 
    #start fps
      time_elapsed = time.time() - prev
      while(time_elapsed > 1./frame_per_sec):                   
            ret, frame = cap.read()
            if not ret:
                break
            if time_elapsed > 1./frame_per_sec:
              prev = time.time()
    #end fps
            if ret==True:
              frame = imutils.resize(frame, width=frame_width)
              out.write(frame)
              cv2.imshow('frame',frame) 
            if cv2.waitKey(1) & 0xFF == ord('q'):
              break
            else:
               break
    
    cap.release()
    out.release()
     
    cv2.destroyAllWindows()