When cap.read() is not as fast as the FPS of a camera

Hello everyone. I have a question regarding the OpenCV VideoCapture class. Any answer will be helpful.

Suppose that we have the following code

import cv2
import time

cap = cv2.VideoCapture('/dev/video0', cv2.V4L2)
cap.set(cv2.CAP_PROP_FPS, 30.0)

while True:
      ret, frame = cap.read()
      time.sleep(0.1)

My question is that, what is actually happening inside the camera when the cap.read() loop is way slower than the actual FPS? Does the camera still keep capturing and storing images to its buffer?

Thank you in advance!

this already will sleep for 100 ms, so you cant get more than 10 fps.
(while 30fps would be more a 30ms interval)
(and none of this actually accounts for decoding / processing time)

please check return value (it’s not alwas supported)

Hi, thank you for your answer.

Does it mean that a camera captures an image internally only WHEN we call cap.read()?

well, sort of !

internally, cap_v4l has some callback, from which a FIFO queue is filled.
cap.read() will flush the 1st (oldest) image from this queue

so you have a “production” fps (what you tried to ‘set()’)
and a “consumer” fps (how fast you can grab() / process frames in your app).

if you fall behind the “production rate”, you’ll get stale frames, so you should try to read() as fast/often as you can

and, btw, time.sleep(0.1) will put your whole process thread to sleep, inluding the v4l parts (decoding video)

Great, thank you! Anyway, I’d like to make sure my understanding regarding this issue.

Let’s say that i want to call the cap.read() for every 10 seconds (0.1 FPS). If I set the cap.set(cv2.CAP_PROP_FPS, 10.0), this will be inefficient, right? Because the camera will keep capturing images with a time interval of 100 ms, even though we don’t retrieve the image at such speed. Is this correct?

this.

cameras keep producing frames. they are not just captured when you request it.

correct.

100 exposures will happen, and one will be taken from the buffer.

eventually the buffer is going to be full. then, nobody knows what’ll happen. the driver might drop frames. or it might throw an error.

1 Like

Got it. Thank you for the answer