0

This is the code I am using for capturing video through my webcam:

import cv2
cap = cv2.VideoCapture(0)

while True:
    ret, frame= cap.read()
    # frame= cv2.resize(frame, None,fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)
    cv2.imshow('Input', frame)
    c = cv2.waitKey(1)
    if c == 27:
        break
cap.release()
cv2.destroyAllWindows()

And the error is:

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /io/opencv/modules/highgui/src/window. cpp, line 325 Traceback (most recent call last):   File "video_capturing2.py", line 10, in    cv2.imshow('Input', frame) cv2.error: /io/opencv/modules/highgui/src/window.cpp:325: error: (-215) size.width>0 && size.height>0 in function ims how

How do I solve it? whenever I am running the code above, the ret flag is never True. I have tried sudo pip3 install opencv-contrib-python but still it is not helping.

Omar Einea
  • 2,478
  • 7
  • 23
  • 35
Sohel Das
  • 9
  • 1
  • 2

1 Answers1

2

imshow fails when you pass an empty image, as pointed out by @101. In your case, this is most likely due to the fact that you ignore empty frames from cap.read() and call imshow even if cap.read returned an empty image.

To fix this, replace your while True loop with a while cap.isOpened() or check whether cap.isOpened() has been successful before reading frames.