The reason behind your error is that the frame is None(Null)
. Sometimes, the first frame that is captured from the webcam is None mainly because
(1)
the webcam is not ready yet ( and it takes some extra second for it to get ready)
or (2)
the operating system does not allow your code to access the webcam.
In the first case, before you do anything on the frame you need to check whether the frame is valid or not :
while True:
frame = image.read()
if frame is not None: # add this line
(H, W) = frame.shape[:2]
In the other case, you need to check the camera setting in your Operating system.
Also, for capturing the webcam frames there is another method based on the VideoCapure class in Opencv that might be easier to debug.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…