Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
860 views
in Technique[技术] by (71.8m points)

python - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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, 'SYSTEMCurrentControlSetEnumUSB') 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++:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...