mounaim
mounaim

Reputation: 1180

Can't read frames from camera OpenCV

Im trying to get the frames from my camera with following basic code :

import cv2
import numpy as np

cap = cv2.VideoCapture(0)


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

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

But I'm getting this error :

None
OpenCV(3.4.1) Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /tmp/opencv-20180307-44253-2moj1c/opencv-3.4.1/modules/highgui/src/window.cpp, line 356
Traceback (most recent call last):
  File "video_cap_opencv.py", line 11, in <module>
    cv2.imshow('frame',frame)
cv2.error: OpenCV(3.4.1) /tmp/opencv-20180307-44253-2moj1c/opencv-3.4.1/modules/highgui/src/window.cpp:356: error: (-215) size.width>0 && size.height>0 in function imshow

Knowing the same code worked well for me before, What could be causing this ?

Upvotes: 0

Views: 2336

Answers (1)

Duloren
Duloren

Reputation: 2711

Well, the error

Assertion failed (size.width>0 && size.height>0)

says something like "Hey, the image you're trying to show is empty". So, the way to avoid the message is checking if the image was actually loaded:

ret, frame = cap.read(frame)
(....)
if frame is not None
    print(frame)
    cv2.imshow('frame',frame)

But the root problem is "why my image is empty?" For this question you should verify:

  • is the camera properly attached to the usb port?
  • is the camera index (0) right? Try 1 or 2 instead
  • is the camera device working?

Upvotes: 2

Related Questions