2

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!

alkasm
  • 22,094
  • 5
  • 78
  • 94
  • 1
    @ Yashaswini Bhat. see my answer below. Please complete the SO tour to earn your first batch ;-) – ZF007 Nov 28 '17 at 18:52
  • did you try `VideoCapture(0)` or `VideoCapture(1)` – user1767754 Nov 28 '17 at 20:59
  • @Mike... you're half right on that. Yashaswini posted the Traceback error. which is not stated at the posted **permanent fix for ...* link. You provided the link :-) – ZF007 Nov 28 '17 at 21:07
  • Another thing that has not been mentioned yet: if you install opencv with `pip install opencv` or `opencv-contrib` on linux / osx, it is not compiled with video support, because of ffmpeg support. To fix this, you often have to build from source. (See also [the faq on PyPi](https://pypi.python.org/pypi/opencv-python)) I'm not 100% sure `VideoCapture` falls into this category though. – randyr Nov 28 '17 at 21:21
  • @user1767754 I have tried this and the same issue persists. – Yashaswini Bhat Nov 29 '17 at 10:11
  • @Yashaswini Bhat.. has the problem been solved? If so.. was it resolved with our help, the answer below or differently? Please let us know so we and others can learn from it for future purpose. And btw.. if below answer is the solution select it as such so we know. Cheers, – ZF007 Dec 06 '17 at 23:39

1 Answers1

3

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.

ZF007
  • 3,708
  • 8
  • 29
  • 48
  • 1
    I would also try `VideoCapture(0)` as this defaults to webcam if that is the only camera available ([docs](https://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-videocapture) "If there is a single camera connected, just pass 0.") – randyr Nov 28 '17 at 21:18