Video input using OpenCV on Linux with conan

Look at any tutorial at reading video input with OpenCV using C++ and you’ll see code very much like this:

1
2
3
4
5
cv::VideoCapture camera(0);
if (!camera.isOpened()) {
std::cerr << "Failed open camera" << std::endl;
return;
}

Except, that it wasn’t working for me. I tried various other values for the index parameter in the VideoCapture constructor but nothing worked.

For this project I’ve been experimenting with using conan to fetch C++ packages rather than building or installing them manually. My conanfile.txt looked like this.

1
2
3
4
5
[requires]
opencv/4.5.2

[generators]
cmake

After much head scratching and searching on stackoverflow I decided that the mostly likely cause was that OpenCV had not been built with Video4Linux support. I wanted to resist manually building OpenCV to get around this and thought there must be a way to pass package options to conan and it turns out there is. This page on the OpenCV recipe also explained conan options https://blog.conan.io/2019/02/11/OpenCV-conan-packages.html

So now my conanfile.txt looks like this:

1
2
3
4
5
6
7
8
[requires]
opencv/4.5.2

[options]
opencv:with_v4l=True

[generators]
cmake

Sadly, this was not the end of the story as running a conan install resulted in this:

1
2
3
4
5
6
7
8
9
10
11
12
ERROR: Missing binary: opencv/4.5.2:6e43eadcfc39ce0e623333fbee7bd12289c046f1
opencv/4.5.2: WARN: Can't find a 'opencv/4.5.2' package for the specified settings, options and dependencies:
- Settings: arch=x86_64, build_type=Release, compiler=gcc, compiler.libcxx=libstdc++11, compiler.version=7, os=Linux
- Options: contrib=False, dnn=True, fPIC=True, parallel=False, shared=False, with_cuda=False, with_eigen=True, with_gtk=True, with_jpeg=libjpeg, with_jpeg2000=jasper, with_openexr=True, with_png=True, with_quirc=True, with_tiff=True, with_v4l=True, with_webp=True, eigen:MPL2_only=False, gtk:version=2, jasper:fPIC=True, jasper:shared=False, jasper:with_libjpeg=libjpeg, jbig:build_executables=True, jbig:fPIC=True, jbig:shared=False, libdeflate:fPIC=True, libdeflate:shared=False, libjpeg:fPIC=True, libjpeg:shared=False, libpng:api_prefix=None, libpng:fPIC=True, libpng:shared=False, libtiff:cxx=True, libtiff:fPIC=True, libtiff:jbig=True, libtiff:jpeg=libjpeg, libtiff:libdeflate=True, libtiff:lzma=True, libtiff:shared=False, libtiff:webp=True, libtiff:zlib=True, libtiff:zstd=True, libwebp:fPIC=True, libwebp:near_lossless=True, libwebp:shared=False, libwebp:swap_16bit_csp=False, libwebp:with_simd=True, openexr:fPIC=True, openexr:shared=False, protobuf:fPIC=True, protobuf:lite=False, protobuf:shared=False, protobuf:with_rtti=True, protobuf:with_zlib=True, quirc:fPIC=True, quirc:max_regions=254, quirc:shared=False, xz_utils:fPIC=True, xz_utils:shared=False, zlib:fPIC=True, zlib:minizip=deprecated, zlib:shared=False, zstd:fPIC=True, zstd:shared=False
- Dependencies: zlib/1.2.11, libjpeg/9d, jasper/2.0.32, libpng/1.6.37, openexr/2.5.5, libtiff/4.2.0, eigen/3.3.9, libwebp/1.2.0, quirc/1.1, gtk/system, protobuf/3.17.1
- Requirements: eigen/3.Y.Z, gtk/system, jasper/2.Y.Z, libjpeg/9d, libpng/1.Y.Z, libtiff/4.Y.Z, libwebp/1.Y.Z, openexr/2.Y.Z, protobuf/3.Y.Z, quirc/1.Y.Z, zlib/1.Y.Z
- Package ID: 6e43eadcfc39ce0e623333fbee7bd12289c046f1

ERROR: Missing prebuilt package for 'opencv/4.5.2'
Try to build from sources with '--build=opencv'
Use 'conan search <reference> --table table.html'
Or read 'http://docs.conan.io/en/latest/faq/troubleshooting.html#error-missing-prebuilt-package'

It’s a good error message and helpfully points at https://docs.conan.io/en/latest/faq/troubleshooting.html#error-missing-prebuilt-package which explains how to enable local builds of missing pre-built packages.

The answer is to add --build missing to your conan command line.

I use CLion so no my conan options look like this:



Now, when I did a conan install (via CLion) conan automatically built OpenCV for me with the right options. Finally, I could open the webcam on my computer.