Why must I not delay VideoCapture read() calls?

Hi

I am using a usb camera.

The code below behaves as I expected.
Because this is common usage.

while True:
    ret, img = cap.read()
    cv2.imshow('Video', img)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

but,
There is something like inferring the image acquired after cap.read() by deep learning.
I reproduced this reasoning time using sleep().

while True:
    ret, img = cap.read()
    time.sleep(3)
    cv2.imshow('Video', img)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

I thought that cap.read() is executed every 3 seconds, so I can take a picture of that moment every 3 seconds.

However! !
The photo will appear when waiting for 3 seconds. In cap.read (), it will be like a 3-second picture of sleep is held in FIFO.

Why is this?

I raised my right hand in front of the camera.
The next cap.read is 3 seconds later.
I lowered my right hand during these three seconds.
cap.read is an image of still raising the right hand.

Why is this?

Hi,
Wht’s your camera?
How do you open it (souce code)?

1 Like

assuming, this is linux, and using the default v4l backend,
there is indeed a fifo queue

you could try to restrict it to a single image:

vcap.set(CAP_PROP_BUFFERSIZE, 1)
1 Like

@berak

Thank you for your advice.
There were many interesting articles when I checked with CAP_PROP_BUFFERSIZE.
I think I can solve this problem.

“common” doesn’t mean good. that code lacks all error checking.

  • assert cap.isOpened() before the loop
  • if not ret: break in the loop
1 Like

A more generic solution would be to read all frames as they come, keep track of elapsed time between interesting frames and discard the rest.

1 Like