Does OpenCV support v4l2? - opencv

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?

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

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.

I had to update the permissions of the camera, then OpenCV started working for me.
sudo chmod 0777 /dev/video0

Related

Why I cannot use FFMPEG as a backend of the OpenCV?

I would like to use FFMPEG as a backend for OpenCV, but I cannot get it to work.
The operating system used is CentOS8.
OpenCV and FFMPEG versions are 3.4.5 and 2.8.17, respectively.
The OpenCV build information shows that FFMPEG=ON, so OpenCV seems to recognize the FFMPEG library.
This is the video I/O part of the OpenCV build information.
Video I/O:
DC1394: NO
FFMPEG: YES
avcodec: YES (58.91.100)
avformat: YES (58.45.100)
avutil: YES (56.51.100)
swscale: YES (5.7.100)
avresample: NO
GStreamer: NO
v4l/v4l2: YES (linux/videodev2.h)
However, the Info message displayed at runtime says
[ INFO:0] VIDEOIO: Enabled backends(6, sorted by priority): GSTREAMER(1000); V4L2(990); CV_IMAGES(980); CV_MJPEG(970); FIREWIRE(960); UNICAP(950)
OpenCV | GStreamer warning: your gstreamer installation is missing a required plugin(/builddir/build/BUILD/opencv-3.4.6/modules/videoio/src/cap_gstreamer.cpp:1823)
OpenCV | GStreamer warning: GStreamer: unable to start pipeline(/builddir/build/BUILD/opencv-3.4.6/modules/videoio/src/cap_gstreamer.cpp:872)
I also don't understand why Gstreamer is being used when it is not enabled in the build information.
What could be the cause?
Please advise me if you know more.
Even you make the FFMPEG=ON as preference, there is no guarantee that FFMPEG will be on in the opencv build process. Because OpenCV will check for all the dependency. Please install all the dependency and try to install missing parts like GStreamer, avresample.

cv2.VideoCapture error when open Gstreamer pipeline

I'm trying to open gstreamer pipeline with opencv-python on window10 like this
cv2.VideoCapture('videotestsrc ! video/x-raw,framerate=20/1 ! videoscale ! videoconvert ! appsink', cv2.CAP_GSTREAMER)
I have installed gstreamer and test it, but VideoCapture is not open.
So I used print(cv2.getBuildInformation()) and checked gstreamer is not built like below
Video I/O:
DC1394: NO
FFMPEG: YES (prebuilt binaries)
avcodec: YES (58.91.100)
avformat: YES (58.45.100)
avutil: YES (56.51.100)
swscale: YES (5.7.100)
avresample: YES (4.0.0)
GStreamer: NO
DirectShow: YES
Media Foundation: YES
DXVA: NO
And now, I don't know how to build opencv with gstreamer.:(
I've run into this exact same frustrating problem. Fortunately, there's a handy guide to resolving it here: https://medium.com/#galaktyk01/how-to-build-opencv-with-gstreamer-b11668fa09c
In summary, you have to manually build gstreamer opencv with cmake in order to properly configure it. It takes about 1-2 hours in my experience, with 1hr compile time. Hope it works for you!

OpenCV VideoCapture error: VIDIOC_REQBUFS: Inappropriate ioctl for device

I have been trying to read videos from a file in Java using OpenCV. I am running OpenCV 4.0.0 on Ubuntu. Currently I have been trying to do so as follows:
VideoCapture videoCapture = new VideoCapture("/home/gkammer/Downloads/test.mp4");
However, this is giving me the error message VIDIOC_REQBUFS: Inappropriate ioctl for device.
I have already tried rebuilding OpenCV with FFMPEG like many similar questions recommend, but I still get the error. When I ran cmake this was the relevant output:
Video I/O:
DC1394: YES (ver 2.2.5)
FFMPEG: YES
avcodec: YES (ver 57.107.100)
avformat: YES (ver 57.83.100)
avutil: YES (ver 55.78.100)
swscale: YES (ver 4.8.100)
avresample: YES (ver 3.7.0)
GStreamer: NO
v4l/v4l2: linux/videodev2.h
Any suggestions for how to resolve this would be appreciated.
I was able to read a .mp4 video with OpenCV 4.0.1-34-g78da67947 and GStreamer 1.14.1 with Ubuntu 18.04 LTS.
To enable this video backend, install gstreamer required dependencies with:
sudo apt install libgstreamer1.0-dev gstreamer1.0-plugins-base gstreamer1.0-plugins-good
Then recompile OpenCV with WITH_GSTREAMER flag on.
I also have issues reading mp4 video or rtsp stream with V4L or DC1364 even if I have compiled OpenCV with the correct options :
" Video I/O:
"
" DC1394: YES (2.2.5)
"
" FFMPEG: NO
"
" avcodec: NO
"
" avformat: NO
"
" avutil: NO
"
" swscale: NO
"
" avresample: NO
"
" GStreamer: YES (1.14.1)
"
" v4l/v4l2: YES (linux/videodev2.h)
If someone has a solution to this problem (specifically the DC1394 backend).
If gstreamer is not installed or there are problems with installing with sudo apt-get gstreamer-xxx, try sudo apt install ubuntu-restricted-extras.
I also have faced with this problem, then I have paid attention to the OpenCV compilation headers, installed almost all packages listed in official docs (except libjasper), cmake made it without WITH_GSTREAMER but with WITH_FFMPEG=ON and got videos with different files extensions(avi, mkv) playing on C++ and Python in Ubuntu 18.04.
Besides, explicit WITH_GSTREAMER=ON does not set flag GSTREAMER to YES.

Unable to start UDP GStreamer Pipeline via OpenCV and Aruco

I'm trying to use opencv via aruco to read an UDP-Stream via Network on Ubuntu 14.04 LTS using OpenCV 3.1.0 and Gstreamer 1.2.4.
I changed the code of the "aruco_simple.cpp" Example file to accomplish that, by changing the parameter of the VideoCapturer constructor to the GStreamer Pipeline:
string PIPELINE_DEF = "udpsrc uri=udp://192.168.71.50:49152 do-timestamp=true name=src blocksize=1316 closefd=false buffer-size=100 !" \
"tsdemux !" \
"queue !" \
"avdec_h264 max-threads=0 !" \
"videoconvert !" \
"xvimagesink name=opencvsink"
//"appsink !"
;
aruco::CameraParameters CamParam;
// read the input image
cv::Mat InImage;
// Open input and read image
//VideoCapture vreader(argv[1]);
VideoCapture vreader(PIPELINE_DEF);
Executing this I always get the following Error:
OpenCV Error: Unspecified error (GStreamer: unable to start pipeline
) in cvCaptureFromCAM_GStreamer, file /home/osboxes/Aruco/opencv-3.1.0/modules/videoio/src/cap_gstreamer.cpp, line 834
Exception :/home/osboxes/Aruco/opencv-3.1.0/modules/videoio/src/cap_gstreamer.cpp:834: error: (-2) GStreamer: unable to start pipeline
in function cvCaptureFromCAM_GStreamer
I found this Bug here http://code.opencv.org/issues/3953
But the solution does not help me in my case.
If I start a GStreamer Pipeline directly (without aruco and opencv) in Python it works.
GStreamer was found by opencv according to the cmake output:
-- GStreamer:
-- base: YES (ver 1.2.4)
-- video: YES (ver 1.2.4)
-- app: YES (ver 1.2.4)
-- riff: YES (ver 1.2.4)
-- pbutils: YES (ver 1.2.4)

OpenCV configure with TBB for ARM (Ubuntu, 3.0.63)

I'm trying to compile OpenCV libs with TBB support for odroid U2 (with Quad core ARM Cortex-A9 MPCore). I have no problem with compile current OpenCV (from github) without TBB. And also I have no problem with compiling TBB libs fron sources - tbb41_20130116oss_src.tgz and I have successfully builded TBB libs:
root#odroid:~/src/tbb41_20130116oss/build/linux_armv7l_gcc_cc4.6_libc2.15_kernel3.0.63_release# ls
arena.d concurrent_queue.o frontend.d libtbbmalloc.so.2 proxy.d scheduler.o task_v2.d tbb_thread.d
arena.o concurrent_queue_v2.d frontend.o libtbbmalloc_proxy.so proxy.o semaphore.d task_v2.o tbb_thread.o
backend.d concurrent_queue_v2.o governor.d libtbbmalloc_proxy.so.2 queuing_mutex.d semaphore.o tbb.def tbbmalloc.d
backend.o concurrent_vector.d governor.o market.d queuing_mutex.o spin_mutex.d tbb_function_replacement.d tbbmalloc.def
backref.d concurrent_vector.o itt_notify.d market.o queuing_rw_mutex.d spin_mutex.o tbb_function_replacement.o tbbmalloc.o
backref.o concurrent_vector_v2.d itt_notify.o mutex.d queuing_rw_mutex.o spin_rw_mutex.d tbb_main.d tbbmallocproxy.def
cache_aligned_allocator.d concurrent_vector_v2.o itt_notify_malloc.d mutex.o reader_writer_lock.d spin_rw_mutex.o tbb_main.o tbbvars.csh
cache_aligned_allocator.o condition_variable.d itt_notify_malloc.o observer_proxy.d reader_writer_lock.o spin_rw_mutex_v2.d tbb_misc.d tbbvars.sh
concurrent_hash_map.d condition_variable.o large_objects.d observer_proxy.o recursive_mutex.d spin_rw_mutex_v2.o tbb_misc.o version_string.ver
concurrent_hash_map.o critical_section.d large_objects.o pipeline.d recursive_mutex.o task.d tbb_misc_ex.d
concurrent_monitor.d critical_section.o libtbb.so pipeline.o rml_tbb.d task.o tbb_misc_ex.o
concurrent_monitor.o dynamic_link.d libtbb.so.2 private_server.d rml_tbb.o task_group_context.d tbb_statistics.d
concurrent_queue.d dynamic_link.o libtbbmalloc.so private_server.o scheduler.d task_group_context.o tbb_statistics.o
root#odroid:~/src/tbb41_20130116oss/build/linux_armv7l_gcc_cc4.6_libc2.15_kernel3.0.63_release#
I can compile my own programm with g++ using this TBB libs, but I can't configure OpenCV to see this TBB libs:
root#odroid:~/src/work/opencv/release# cmake -D CMAKE_BUILD_TYPE=RELEASE -D WITH_TBB=ON -D CMAKE_INSTALL_PREFIX=/usr/local ..
-- Detected version of GNU GCC: 46 (406)
-- Found OpenEXR: /usr/lib/libIlmImf.so
-- checking for module 'gstreamer-app-0.10'
-- package 'gstreamer-app-0.10' not found
-- Looking for linux/videodev.h
-- Looking for linux/videodev.h - not found
-- Looking for linux/videodev2.h
-- Looking for linux/videodev2.h - found
-- Looking for sys/videoio.h
-- Looking for sys/videoio.h - not found
-- Looking for libavformat/avformat.h
-- Looking for libavformat/avformat.h - found
-- Looking for ffmpeg/avformat.h
-- Looking for ffmpeg/avformat.h - not found
-- checking for module 'tbb'
-- package 'tbb' not found
[...]
could you explain how I can do this? Thank you!
Try building opencv with the included TBB build (BUILD_TBB=true), works for me on odroid-x2:
cmake -D CMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local/opencv-2.4-ec15d6f383e0aae -DBUILD_TBB:BOOL="1" -DWITH_TBB:BOOL="1" ..
make -j 4
If you do a make install, be sure to copy ./lib/libtbb.so to somewhere where it can be found, otherwise opencv just leaves it there.

Resources