Webcam works inconsistently [closed]

asked 2018-12-17 07:06:40 -0600

kgpinco gravatar image

Hello,

I have searched for answer to this error for a few days now as I have seen people getting the same error however none of the solutions I came across worked.
I created a simple program to do nothing other than load in the camera:

import cv2

cap = cv2.VideoCapture(0)

# Check if the webcam is opened correctly
if not cap.isOpened():
    raise IOError("Cannot open webcam")

while True:
    ret, frame = cap.read()
    cv2.imshow('Input', frame)

    c = cv2.waitKey(1)
    if c == 27:
        break

cap.release()
cv2.destroyAllWindows()

Though, sometimes my webcam loads in with no problems most of time doesn't work with the error messages:

[ WARN:0] videoio(MSMF): OnReadSample() is called with error status: -2147024894 [ WARN:0] videoio(MSMF): async ReadSample() call is failed with error status: -2147024894 [ WARN:1] videoio(MSMF): can't grab frame. Error: -2147024894 cv2.error: OpenCV(3.4.4) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:356: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow

I have seen many answers talk about the camera capture being empty and it not expecting an empty value: though they don't explain WHY it is empty and how to prevent it.

Any help is greatly appreciated.

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-11-09 10:19:56.995999

Comments

There is no onReadSample() in opencv

LBerger gravatar imageLBerger ( 2018-12-17 07:23:28 -0600 )edit

maybe you can try to use the dshow backend, instead of the MSMF one ?

cap = cv2.VideoCapture(0 + cv2.CAP_DSHOW)

error is from here, btw.

berak gravatar imageberak ( 2018-12-17 07:54:25 -0600 )edit

and really, why do python noobs NEVER check the ret value ?

berak gravatar imageberak ( 2018-12-17 07:55:11 -0600 )edit

@berak. The code above worked for me.

supra56 gravatar imagesupra56 ( 2018-12-17 09:44:45 -0600 )edit

@supra56, it also worked for me ;)

however, there may be combinations of hardware, drivers, etc, where it is not so...

berak gravatar imageberak ( 2018-12-17 09:47:25 -0600 )edit

The ret is boolean returned true.

supra56 gravatar imagesupra56 ( 2018-12-17 09:48:21 -0600 )edit

If you use windows 10 try to change privacy settings

LBerger gravatar imageLBerger ( 2018-12-17 11:34:52 -0600 )edit