-2

I am trying this code but its giving error: OpenCV(4.5.1) ..\modules\highgui\src\window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

cap = cv2.VideoCapture(0)
time.sleep(2)
while 1:
    ret, img = cap.read()
    image = cv2.imread('D:\dataset\signProject\amer_sign2.png')
    cv2.imshow("image", image)
    img = cv2.flip(img, 1)
    top, right, bottom, left = 75, 350, 300, 590
    roi = img[top:bottom, right:left]
    roi=cv2.flip(roi,1)
    gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (7, 7), 0)
    cv2.imshow('roi',gray)
    alpha=classify(gray)
    cv2.rectangle(img, (left, top), (right, bottom), (0,255,0), 2)
    font=cv2.FONT_HERSHEY_SIMPLEX
    cv2.putText(img,alpha,(0,130),font,5,(0,0,255),2)
    #cv2.resize(img,(1000,1000))
    cv2.imshow('img',img)
    key = cv2.waitKey(1) & 0xFF
    if key==ord('q'):
        break;
cap.release()
cv2.destroyAllWindows()

ERROR

error                                     Traceback (most recent call last)
Input In [45], in <cell line: 4>()
      4 ret, img = cap.read()
      5 image = cv2.imread('D:\dataset\signProject\amer_sign2.png')
----> 6 cv2.imshow("image", image)
      7 img = cv2.flip(img, 1)
      8 top, right, bottom, left = 75, 350, 300, 590

error: OpenCV(4.5.1) ..\modules\highgui\src\window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

ERROR

1 Answers1

0

Usually you should always use such if blocks checking if the image is read correctly.

if img and img.size != 0:
     // do the work

Your image is not read properly from the error message you showed. Check if the image exists and it is in correct format etc.

You can also use this function cv2.haveImageReader which checks whether the specified image can be decoded by OpenCV. It returns True or False.

val = cv2.haveImageReader(filename)
user2736738
  • 30,591
  • 5
  • 42
  • 56