Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Read latest live image from webcam, not from queue?

Hi all,

I've been running some speed tests of a webcam-based opencv program and I was surprised to find that a bottleneck seems to be due to cap.read() behaviour for live cams.

My cpp code runs every 0.5 seconds and takes a snapshot from the webcam, displays it on screen, then sleeps till the next time. When I display these images on the screen there is loads of latency, sometimes several seconds.

What I think is happening is that cap.read() isn't reading the latest image from the webcam. Rather, the webcam is running at a higher fps, eg 10fps, determined by its 4vl settings, and storing these images in a buffer. Then cap.read() is only getting the next image from the buffer, as if it was reading from a movie file. As my code only runs twice per second, I'm reading old image from the camera hardware rather than the latest ones. It looks like the buffer holds a few seconds of images, because my latency doesn't get worse than that once the buffer is full.

My webcam doesn't go down to 2fps, I think the lowest is about 10. But regardless of this, I wonder if cv has a function that is like cap.read() but which always returns the latest available live image rather than the next one off the buffer? Something like cap.readlatest() ? Or any other workarounds? At the moment I just call cap.read() five times in a row on each callback, which has just about got rid of the latency. But that's a hack for the particular rates here, I'd prefer a general solution. Maybe there is some way to purge the buffer of all but the latest image? I have googled but can't find the right words for such a thing if it exists.

(Strange I have never noticed this effect before -- all my other cv codes have been driven by the frame rate of the camera, blocking on cap.read, but this is the first time I've needed the timer to come from elsewhere.)