3
votes

I'm writing a Python program using OpenCV, but I cannot capture images from v4l2 cameras.

I tried with both a PS2 EyeToy and Droidcam (Android virtual webcam), which are both using v4l2, and none of them work: cv.CaptureFromCAM(0) only returns None.

I can use both webcams in other programs (tried VLC and Kamerka).

I guess that OpenCV supports v4l only, not v4l2.

How can I solve this problem? Does a v4l2->v4l converter exist?

EDIT: I read that cv.CaptureFromFile uses ffmpeg to decode video files. Is it possible to manually specify a format, so I can use ffmpeg's video4linux2 demuxer?

3
Please review the answers, up vote those who have been helpful to you, and if one of them lead you to solve the problem you should click on the checkbox near it to select it as the official answer. By doing these things you are helping future visitors.karlphillip

3 Answers

7
votes

For OpenCV 3.2.0, including the following in the cmake options worked for me

from https://github.com/opencv/opencv/issues/7974

-DWITH_V4L=ON 
-DWITH_LIBV4L=ON

I'm less sure if this one had an affect, but from VideoCapture Does Not Work in Anaconda

-DWITH_FFMPEG=1

Including all 3 in my cmake options seemed to be what finally made mine work

3
votes

OpenCV does support V4L2, but maybe not by default.

Download OpenCV's source code and make sure you have v4l2 headers and libraries installed on your system.

After configuring OpenCV with cmake, check it's output:

--   Video I/O:
--     DC1394 1.x:                  NO
--     DC1394 2.x:                  YES (ver 2.2.0)
--     FFMPEG:                      YES
--       codec:                     YES (ver Unknown)
--       format:                    YES (ver Unknown)
--       util:                      YES (ver Unknown)
--       swscale:                   YES (ver Unknown)
--       gentoo-style:              YES
--     OpenNI:                      NO
--     OpenNI PrimeSensor Modules:  NO
--     PvAPI:                       NO
--     GigEVisionSDK:               NO
--     QuickTime:                   NO
--     QTKit:                       YES
--     V4L/V4L2:                    NO/NO

If you notice the output above, OpenCV won't be build with support to V4L/V4L2 on my system because I don't have the necessary development files installed.

0
votes

I had to update the permissions of the camera, then OpenCV started working for me.

sudo chmod 0777 /dev/video0