将 USB 视频捕获设备友好名称与 Python 中的 OpenCV 端口号相关联

Associate USB Video Capture Device Friendly Name with OpenCV Port Number in Python

我想在 Windows 平台上使用 Python 获取外部 USB 视频捕获设备的友好名称和 USB 端口号。我正在使用 OpenCV 从 USB 捕获设备捕获视频。 OpenCV 将 USB 端口称为“-1”(找到的第一个工作摄像头)、“0”(对我来说,这是网络摄像头),以及“1”、“2”等用于外部 USB 视频捕获设备( EasyCap、Hauppauge USB Live-2 等)。这是一个在 OpenCV 中连接到网络摄像头的单行示例:

cap = cv2.VideoCapture(0)

我有三个外部视频捕获设备和一个网络摄像头。这是我的代码,它成功地显示了这些设备的友好名称(我不确定这是否适用于每个 USB 视频捕获设备):

import wmi
c = wmi.WMI()
wql = "Select * From Win32_USBControllerDevice"
for item in c.query(wql):
    a = item.Dependent.PNPClass
    b = item.Dependent.Name.upper()
    if (a.upper() == 'MEDIA' or a.upper() == 'CAMERA') and 'AUDIO' not in b:
        print(item.Dependent.Name)

结果是:

Integrated Webcam
Hauppauge USB-Live2
USB TV Tuner
USB Video Device

如果将打印语句替换为

,我可以打印所有设备信息
print(item.Dependent)

然后我得到以下结果:

instance of Win32_PnPEntity
{
        Caption = "Integrated Webcam";
        ClassGuid = "{ca3e7ab9-b4c3-4ae6-8251-579ef933890f}";
        CompatibleID = {"USB\Class_0e&SubClass_03&Prot_00", "USB\Class_0e&SubClass_03", "USB\Class_0e"};
        ConfigManagerErrorCode = 0;
        ConfigManagerUserConfig = FALSE;
        CreationClassName = "Win32_PnPEntity";
        Description = "USB Video Device";
        DeviceID = "USB\VID_0BDA&PID_5686&MI_00\6&153A3DF0&0&0000";
        HardwareID = {"USB\VID_0BDA&PID_5686&REV_5729&MI_00", "USB\VID_0BDA&PID_5686&MI_00"};
        Manufacturer = "Microsoft";
        Name = "Integrated Webcam";
        PNPClass = "Camera";
        PNPDeviceID = "USB\VID_0BDA&PID_5686&MI_00\6&153A3DF0&0&0000";
        Present = TRUE;
        Service = "usbvideo";
        Status = "OK";
        SystemCreationClassName = "Win32_ComputerSystem";
        SystemName = "MY-LAPTOP";
};


instance of Win32_PnPEntity
{
        Caption = "Hauppauge USB-Live2";
        ClassGuid = "{4d36e96c-e325-11ce-bfc1-08002be10318}";
        CompatibleID = {"USB\Class_ff&SubClass_ff&Prot_ff", "USB\Class_ff&SubClass_ff", "USB\Class_ff"};
        ConfigManagerErrorCode = 0;
        ConfigManagerUserConfig = FALSE;
        CreationClassName = "Win32_PnPEntity";
        Description = "Hauppauge USB-Live2";
        DeviceID = "USB\VID_2040&PID_C200&MI_01\6&19EA708B&0&0001";
        HardwareID = {"USB\VID_2040&PID_C200&REV_4001&MI_01", "USB\VID_2040&PID_C200&MI_01"};
        Manufacturer = "Hauppauge";
        Name = "Hauppauge USB-Live2";
        PNPClass = "MEDIA";
        PNPDeviceID = "USB\VID_2040&PID_C200&MI_01\6&19EA708B&0&0001";
        Present = TRUE;
        Service = "hcw10bda";
        Status = "OK";
        SystemCreationClassName = "Win32_ComputerSystem";
        SystemName = "MY-LAPTOP";
};


instance of Win32_PnPEntity
{
        Caption = "USB TV Tuner";
        ClassGuid = "{4d36e96c-e325-11ce-bfc1-08002be10318}";
        CompatibleID = {"USB\Class_FF&SubClass_00&Prot_FF", "USB\Class_FF&SubClass_00", "USB\Class_FF"};
        ConfigManagerErrorCode = 0;
        ConfigManagerUserConfig = FALSE;
        CreationClassName = "Win32_PnPEntity";
        Description = "USB TV Tuner";
        DeviceID = "USB\VID_1B71&PID_3002\300000000002";
        HardwareID = {"USB\VID_1B71&PID_3002&REV_0100", "USB\VID_1B71&PID_3002"};
        Manufacturer = "Active Development Co., Ltd.";
        Name = "USB TV Tuner";
        PNPClass = "MEDIA";
        PNPDeviceID = "USB\VID_1B71&PID_3002\300000000002";
        Present = TRUE;
        Service = "X86BDA";
        Status = "OK";
        SystemCreationClassName = "Win32_ComputerSystem";
        SystemName = "MY-LAPTOP";
};


instance of Win32_PnPEntity
{
        Caption = "USB Video Device";
        ClassGuid = "{ca3e7ab9-b4c3-4ae6-8251-579ef933890f}";
        CompatibleID = {"USB\Class_0e&SubClass_03&Prot_00", "USB\Class_0e&SubClass_03", "USB\Class_0e"};
        ConfigManagerErrorCode = 0;
        ConfigManagerUserConfig = FALSE;
        CreationClassName = "Win32_PnPEntity";
        Description = "USB Video Device";
        DeviceID = "USB\VID_1E4E&PID_701F&MI_00\6&3543E1D5&0&0000";
        HardwareID = {"USB\VID_1E4E&PID_701F&REV_0100&MI_00", "USB\VID_1E4E&PID_701F&MI_00"};
        Manufacturer = "Microsoft";
        Name = "USB Video Device";
        PNPClass = "Camera";
        PNPDeviceID = "USB\VID_1E4E&PID_701F&MI_00\6&3543E1D5&0&0000";
        Present = TRUE;
        Service = "usbvideo";
        Status = "OK";
        SystemCreationClassName = "Win32_ComputerSystem";
        SystemName = "MY-LAPTOP";
};

这些结果中肯定有一些东西表明外部设备连接到的端口,但我没有看到。关于如何将友好名称与 OpenCV 识别的端口号相关联的建议?

我找到了一个非常有效的解决方案。 CV-camera-finder 可以在这里找到:https://github.com/pvys/CV-camera-finder.