8

I am using ubuntu16.04 and trying to run opencv script. when i use:

video_capture = cv2.VideoCapture(-1)

it gives me error VIDEOIO ERROR: V4L: can't find camera device No video window opens But when i run

video_capture = cv2.VideoCapture('test.jpg')

It opens window shows the picture and close the window. Please tell me why it is not streaming video directly from camera.

3
  • 2
    have you tried with cv2.VideoCapture(0)?
    – api55
    Commented Oct 26, 2018 at 11:50
  • @api55 0 - is for default PC webcam. it doesn't work.
    – t_sologub
    Commented Oct 26, 2018 at 18:06
  • @TARS Yes, I know what it is for, and that is the reason i asked If OP tried that, since it is not in the question. And my other suggestion is to try to use another backend...
    – api55
    Commented Oct 27, 2018 at 6:40

2 Answers 2

4

The suggestion api55 gave in his comment

video_capture = cv2.VideoCapture(0)

is what I would try first.

Generally, you can list the available cameras with ls /dev/video* or v4l2-ctl --list-devices. Here sample output:

NZXT-U:rt-trx> v4l2-ctl --list-devices
Microsoft® LifeCam Cinema(TM): (usb-0000:00:14.0-1):
    /dev/video1

Microsoft® LifeCam Cinema(TM): (usb-0000:00:1a.0-1.3):
    /dev/video0

/dev/video0 corresponds to device id 0, etc.

PS: v4l2-ctl is quite useful for solving camera issues and can do much more than --list-devices. I installed it via packagev4l-utils on a 16.04 machine.

3
  • Thank you very much Ulrich for your answer. I did sudo apt-get install cheese and it started working. Commented Oct 29, 2018 at 6:53
  • @NeerajSharma, I took a quick look at the OpenCV source code, and VideoCapture does support negative device numbers (at least for cap_v4l.cpp), even though this is not in the docs! You could answer your own question. If you are not interested, I will edit my answer. Commented Oct 29, 2018 at 21:59
  • Yes i tried with -1 and it worked. Thanks for your support Ulrich. Commented Oct 30, 2018 at 7:49
-1

Late, but to get mine working i put in terminal:

-ltrh /dev/video*

To get a list of the video devices that are plugged into my computer. Then for each one I did:

sudo chmod 777 /dev/videox

Where x was one of the video files that were listed, giving everything access to them. Probaly not the most secure solution, but it got my code working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.