derjack
derjack

Reputation: 144

Python OpenCV: capture.read returns true, although camera unplugged

I have camera, i use cv2 module in python. Everything works fine, maybe except i would like to have error codes or success/failure returned instead of printed out to stderr.

I have loop which reads images and its return.

while True:
    ret, img = capture.read()
    print ret
    ........
    time.sleep(0.033)

According to OpenCV's docs it should return False on failure. While it works, it's fine. When i unplug webcam (to 'simulate' some failure) this results in many outputs of "VIDIOC_DQBUF: No such device", but the ret is still True. Is this a bug, a webcam or OS specific behaviour? Are there any workarounds on this? (instead of redirecting stderr within python and checking what's on there)

Upvotes: 1

Views: 1198

Answers (1)

Amin Suzani
Amin Suzani

Reputation: 587

Yes, it is supposed to return False on failure, but seems it doesn't work reliably in your case. I can suggest a workaround. I assume that when the webcam is unplugged the returned image is all zero. You can check that too in conjunction with the returned flag.

while True:
    ret, img = capture.read()
    print ret and (not img.any())
    ........
    time.sleep(0.033)

Upvotes: 1

Related Questions