OpenCV VideoCapture device index / device number

折月煮酒 提交于 2020-03-23 08:24:12

问题


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.


回答1:


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




回答2:


Preface, I do not use windows, and this hasn't been tested, but is a combination of answers and source found from online, with some modifications.

Walk the USB registry keys and parse the sub_key strings:

import _winreg
usb_devices=[]
index = 0
with _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Enum\USB') as root_usb:
    while True:
        try:
            subkey = _winreg.EnumKey(root_usb, index)
            usb_devices.append(subkey)
            index += 1
        except WindowError as e:
            if e[0] == 259: # No more data is available
                break
            elif e[0] == 234: # more data is available
                index += 1
                continue
            raise e
print('parse these', usb_devices)

Or possibly Popen a wmic subprocess and parse the stdout:

from subprocess import Popen, PIPE
results1 = Popen(['wmic', 'path', 'win32_pnpentity', 'get', 'caption' '/format:list'], stdout=PIPE)
results2 = Popen(['wmic','path','Win32_SerialPort','get','DeviceID^,Caption^,Description^,Name^,ProviderType','/format:list'], stdout=PIPE)
print('parse these', results1.stdout.read())
print('parse these', results2.stdout.read())

Related, linux, mac, and windows c++:

  • https://superuser.com/questions/902012/how-to-identify-usb-webcam-by-serial-number-from-the-linux-command-line
  • https://superuser.com/questions/883053/mac-os-x-equivalent-of-udevadm-info-a-n-dev-ttyacm0
  • get serial com port description in windows batch
  • Detecting USB Insertion / Removal Events in Windows using C++
  • windows - How to enumerate all connected USB devices' device path?
  • https://msdn.microsoft.com/en-us/library/aa394413(v=vs.85).aspx
  • http://www.velleman.eu/images/tmp/usbfind.c
  • http://www.bitpim.org/pyxr/c/projects/bitpim/src/comscan.py.html



回答3:


If you can differentiate the cameras by their serial number or device and vendor id, you can loop through all video devices before opening with opencv and search for the camera device you want to open.



来源:https://stackoverflow.com/questions/41298588/opencv-videocapture-device-index-device-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!