Look at any tutorial at reading video input with OpenCV using C++ and you’ll see code very much like this:
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.
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:
Sadly, this was not the end of the story as running a conan install resulted in this:
ERROR: Missing binary: opencv/4.5.2:6e43eadcfc39ce0e623333fbee7bd12289c046f1
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.