Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make python record 30 or 60 frames per second using openCV and a Logitech C920

I am trying to record a video in 720p at 60 FPS or 1080p at 30 FPS, However when using the C920 webcam and OpenCV on python I can only get about 10 fps on 720p and 5fps on 1080p.

I have tried a lot different settings for openCV, none change the FPS however of the output.

import cv2
import time
FPS = 0

cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'MJPG')

if(not cap.isOpened()):
    exit()

cap.set(cv2.CAP_PROP_FOURCC, fourcc);
cap.open(cv2.CAP_ANY);
cap.set(cv2.CAP_PROP_CONVERT_RGB, 0);
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
cap.set(cv2.CAP_PROP_FPS, 60)

last = time.time()

for i in range(0,100):
    before = time.time()
    rval, frame = cap.read()
    now = time.time()
    print("cap.read() took: " + str(now - before))
    if(now - last >= 1):
        print(FPS)
        last = now
        FPS = 0
    else:
        FPS += 1
cap.release()

I expect it to output 60fps but it only gives 9 or 10 fps

like image 433
Joris Janssen Avatar asked Jun 07 '26 06:06

Joris Janssen


1 Answers

OpenCV automatically selects the first available capture backend (see here). It can be that it is not using V4L2 automatically.

Also set both -D WITH_V4L=ON and -D WITH_LIBV4L=ON if building from source.

Possibly, the pixel format that is selected by OpenCV does not support the frame rate that you want, in the resolution that you want. On Linux you can use v4l2-ctl --list-formats-ext and v4l2-ctl --all to see the settings.

In order to set the pixel format to be used set the CAP_PROP_FOURCC property of the capture:

capture = cv2.VideoCapture(cam_id, cv2.CAP_V4L2)
capture.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))
like image 157
zardosht Avatar answered Jun 10 '26 09:06

zardosht



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!