vlachoudis / bCNC

GRBL CNC command sender, autoleveler and g-code editor
GNU General Public License v2.0
1.58k stars 537 forks source link

Camera improvements #1882

Open jasonharper opened 7 months ago

jasonharper commented 7 months ago
Harvie commented 7 months ago

Hello, thanks for improvements. I think it mostly makes sense, but i am not sure about the threading thing... doesn't OpenCV have some API to drop all the buffered frames and only get the latest one?

Maybe we can just read all the frames in buffer each time, that seems as a most straightforward solution to me:

ret = True
while ret:
    ret, frame = cam.read()

What about disabling the buffer completely? eg.:

cap.set(cv2.CAP_PROP_BUFFERSIZE, 0)

does that help in your case?

Mabye there is some further reading: https://stackoverflow.com/questions/43665208/how-to-get-the-latest-frame-from-capture-device-camera-in-opencv

This page https://stackoverflow.com/questions/59924971/is-there-a-way-to-show-last-frame-in-opencv-python

also proposes way to retrieve index of latest frame in buffer and retreive directly frame at that index...

jasonharper commented 7 months ago

CAP_PROP_BUFFERSIZE reportedly doesn't work on all cameras - and I'm pretty sure that cameras like mine are in the non-working category: the problematic buffering is happening in the camera, where OpenCV has no control over it.

Your second link is talking about getting the last frame of a video file, it is not applicable to cameras.

If your concern is that the background thread takes too much CPU time, please note that the laptop I'm testing this on shows around 5% CPU utilization with the camera active in bCNC.

Harvie commented 7 months ago

and I'm pretty sure that cameras like mine are in the non-working category

Can you try please?

jasonharper commented 7 months ago

I finally got a chance to try CAP_PROP_BUFFERSIZE as you suggested. It did actually help - but it only got the lag down to 4 seconds, which is still unusable.