The integrated webcam in OpenCV on Ubuntu 16.04 is throwing back the below error. I checked cheese with a different program and it shows still images and video, therefore it seems not the camera itself to be the problem here.
The code I used to test this out :
import cv2
import numpy as np
import time
cam = cv2.VideoCapture(2)
if not cam.isOpened():
print('Cannot open camera')
while True:
ret,frame = cam.read()
cv2.imshow('webcam', frame)
if cv2.waitKey(1)&0xFF == ord('q'):
break
cam.release()
cv2.destroyAllWindows()
The error :
Cannot open camera (feedback from script at
if not cam.isOpened():
).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_test.py", line 13, in cv2.imshow('webcam', frame) cv2.error: /io/opencv/modules/highgui/src/window.cpp:325: error: (-215) size.width>0 && size.height>0 in function imshow
Any help would be appreciated. Thanks!
Try the following using cam.open()
:
import cv2
import numpy as np
import time
cam = cv2.VideoCapture(2) # camera index (default = 0) (added based on Randyr's comment).
print 'cam has image : %s' % cam.read()[0] # True = got image captured.
# False = no pics for you to shoot at.
# Lets check start/open your cam!
if cam.read() == False:
cam.open()
if not cam.isOpened():
print('Cannot open camera')
while True:
ret,frame = cam.read()
cv2.imshow('webcam', frame)
if cv2.waitKey(1)&0xFF == ord('q'):
break
cam.release()
cv2.destroyAllWindows()
You can also play with the cam = cv2.VideoCapture(value)
value.. which is now set to two. Try a range..e.g. 1-10.