OpenCV VideoCapture 设备索引/设备号
OpenCV VideoCapture device index / device number
我有一个 python 环境(在 Windows 10 上)使用 OpenCV VideoCapture
class 连接到多个 USB 摄像头。
据我所知,除了 VideoCapture
class 构造函数 / open
中的 device
参数外,没有办法在 OpenCV 中识别特定相机方法。
问题是设备参数会根据实际连接的相机数量和连接到哪个 USB 端口而变化。
我希望能够识别特定的摄像头并找到它的 "device index" 或 "camera index",无论连接了多少摄像头以及连接到哪个 USB 端口。
有人可以建议一种实现该功能的方法吗? python 最好使用代码,但 C++ 也可以。
如果您可以通过序列号或设备和供应商 ID 区分摄像头,则可以在使用 opencv 打开之前循环遍历所有视频设备并搜索要打开的摄像头设备。
前言,我没有使用windows,这个也没有测试过,是结合网上的答案和源码,做了一些修改。
遍历 USB 注册表项并解析 sub_key 字符串:
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)
或者可能 Popen
一个 wmic
子进程并解析 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())
相关,linux、mac 和 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
- 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
据我所知,openCV 枚举设备并将其索引用作相机索引。
但它枚举的方式可能因后端而异。
无论如何,如果你能像OpenCV那样枚举设备,你可以根据你的代码匹配设备的索引和它的信息。
因此,在Windows 环境中,您可以使用MSMF 或DSHOW 作为后端。如果您使用 MSMF 作为后端,我做了一个简单的函数来列出设备并将其名称与其索引相匹配。
这里:https://github.com/pvys/CV-camera-finder.
如果您使用 DSHOW 作为背景,这里有一篇不错的文章:https://www.codeproject.com/Articles/1274094/Capturing-Images-from-Camera-using-Python-and-Dire
我有一个 python 环境(在 Windows 10 上)使用 OpenCV VideoCapture
class 连接到多个 USB 摄像头。
据我所知,除了 VideoCapture
class 构造函数 / open
中的 device
参数外,没有办法在 OpenCV 中识别特定相机方法。
问题是设备参数会根据实际连接的相机数量和连接到哪个 USB 端口而变化。
我希望能够识别特定的摄像头并找到它的 "device index" 或 "camera index",无论连接了多少摄像头以及连接到哪个 USB 端口。
有人可以建议一种实现该功能的方法吗? python 最好使用代码,但 C++ 也可以。
如果您可以通过序列号或设备和供应商 ID 区分摄像头,则可以在使用 opencv 打开之前循环遍历所有视频设备并搜索要打开的摄像头设备。
前言,我没有使用windows,这个也没有测试过,是结合网上的答案和源码,做了一些修改。
遍历 USB 注册表项并解析 sub_key 字符串:
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)
或者可能 Popen
一个 wmic
子进程并解析 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())
相关,linux、mac 和 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
- 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
据我所知,openCV 枚举设备并将其索引用作相机索引。 但它枚举的方式可能因后端而异。 无论如何,如果你能像OpenCV那样枚举设备,你可以根据你的代码匹配设备的索引和它的信息。
因此,在Windows 环境中,您可以使用MSMF 或DSHOW 作为后端。如果您使用 MSMF 作为后端,我做了一个简单的函数来列出设备并将其名称与其索引相匹配。 这里:https://github.com/pvys/CV-camera-finder.
如果您使用 DSHOW 作为背景,这里有一篇不错的文章:https://www.codeproject.com/Articles/1274094/Capturing-Images-from-Camera-using-Python-and-Dire