3

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

Joris Janssen
  • 64
  • 1
  • 10
  • What are the specifications of the hardware you are running this code on? – GPPK Apr 12 '19 at 10:27
  • If your fps drops at a higher resolution, it's pretty clear the script is limited by the CPU or time it takes for the camera to send its input. If the bottleneck is the camera and your CPU use remains low, perhaps you can get around it by using multiple threads. – Peter Apr 12 '19 at 10:46
  • I am running on windows10, python 3.6.5, and the result is the same accros my laptop(6700HQ) and my pc(8700K) – Joris Janssen Apr 12 '19 at 10:47
  • If I record in windows itself it does reach 1080p30fps – Joris Janssen Apr 12 '19 at 10:48

1 Answers1

1

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'))
zardosht
  • 3,014
  • 2
  • 24
  • 32