Search code examples
pythonc++windowsopencvusb

OpenCV VideoCapture device index / device number


I have a python environment (on Windows 10) that uses OpenCV VideoCapture class to connect to multiple usb cameras.

As far as I know, there is no way to identify a specific camera in OpenCV other than the device parameter in the VideoCapture class constructor / open method.

The problem is that the device parameter changes depending on how many cameras are actually connected and to which usb ports.

I want to be able to identify a specific camera and find its "device index" or "camera index" no matter how many cameras are connected and to which usb ports.

Can somebody please suggest a way to achieve that functionality? python code is preferable but C++ will also do.


Solution

  • As far as I know, openCV enumerates devices and uses the index of it as a camera index. But the way it enumerates can differ among backends. Anyway, if you can enumerate devices as OpenCV do, you can match the index of the device and its information depend on your code.

    So, In Windows environment, you can use MSMF or DSHOW as a backend. If you are using MSMF as a backend, I made a simple function to list devices and match its name to its index. Here: https://github.com/pvys/CV-camera-finder.

    If you are using DSHOW as a background, here's a nice article: https://www.codeproject.com/Articles/1274094/Capturing-Images-from-Camera-using-Python-and-Dire