pythonopencvusbportfriendly-id

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


I want to get the Friendly Names and USB port numbers for external USB video capture devices using Python on a Windows platform. I’m using OpenCV to capture video from a USB capture device. OpenCV refers to the USB ports as “-1” (the first working camera found), “0” (for me, this is the webcam), and “1”, “2”, etc. for external USB video capture devices (EasyCap, Hauppauge USB Live-2, etc.). Here is a one-line example of connecting to the webcam in OpenCV:

cap = cv2.VideoCapture(0)

I have three external video capture devices and a webcam. Here is my code that successfully shows the friendly name of just these devices (I’m not sure this will work for every USB video capture device):

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)

Results are:

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

I can print all the device information if I replace the print statement with

print(item.Dependent)

and then I get the following results:

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";
};

There must be something in these results that indicate the port that the external devices are connected to, but I don’t see it. Suggestions on how I can associate the friendly name with a port number that OpenCV recognizes?


Solution

  • I found a better solution than my first one. My first solution was this:

    I found a solution that works great. CV-camera-finder can be found here: cv-camera-finder.

    However, this solution requires Python 3.7 and the developer archived the project and isn't responding to requests to make it compatible with later versions of Python.

    NEW SOLUTION

    I just now found this solution that works with Python >= 3.6. It works great! cv2-enumerate-cameras