0

This is my code

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

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

if cv2.waitKey(1) & 0xFF == ord('q'):
  break

cap.release()
cv2.destroyAllWindows()

I've got this error

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 "2.py", line 9, in cv2.imshow('frame',frame) cv2.error: /io/opencv/modules/highgui/src/window.cpp:325: error: (-215) size.width>0 && size.height>0 in function imshow

I've already try changing the '0' on cv2.VideoCapture(0) to '1' and still doesn't work.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Luis Veliz
  • 247
  • 4
  • 8

1 Answers1

1

This often happens when cap.read() returns empty frame for some reason. Solve this with:

while(True):
    result, frame = cap.read()
    if result:
        cv2.imshow('frame', frame)
ingvar
  • 4,169
  • 4
  • 16
  • 29