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
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'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With