Error : (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'

import cv2

cap = cv2.VideoCapture(0)

while True:

    _,frame = cap.read()
    cv2.imshow("frame",frame)
    if cv2.waitKey(1) & 0xff == ord("q"):
        break
cap.release()
cv2.destroyAllWindows()

Error: Traceback (most recent call last): File “/home/erdem/Desktop/deneme1.py”, line 8, in cv2.imshow(“frame”,frame) cv2.error: OpenCV(4.6.0) /io/opencv/modules/highgui/src/window.cpp:967: error: (-215:Assertion failed) size.width>0 && size.height>0 in function ‘imshow’

How can I solve this error?

I reset raspberry. I deleted and installed the packages. I can take images from the camera with the LXTerminal **libcamera-hello –camera 0 -t 0 ** command. There is no problem with the camera.

Clearly VideoCapture is not getting the frame. Hard to say why without more info, like what type of camera is it? Webcam? GigeVision? You appear to not be using the correct interface. That 0 is not a universal thing.

I am using raspberry pi camera module 2 on raspberry pi 5. I don’t know how to find the accuracy of the interface.

this should be common knowledge by now.

those “pi cams” don’t present as regular Video For Linux devices. they have their own weird interface.

OpenCV does not support that. go find on the internet how you can make that camera be a regular V4L cam.

https://forums.raspberrypi.com/viewtopic.php?t=331441

I did what you said but it still didn’t work. I then tried the same code with my laptop’s USB-connected webcam. This time the camera does not turn on. “cap = cv2.VideoCapture(0)” does not read below this line.Then I opened my phone’s camera on the computer. And the code worked on it. What’s wrong with the webcam? My webcam is Logitech c270. I connected the phone via the Asus GlideX application.