studiosi / OpenCVDeviceEnumerator

This repository contains a class that allows the enumeration of video and audio devices in order to get the device IDs that are required to create a VideoCapture object inside OpenCV (in Windows).
MIT License
66 stars 30 forks source link

Differences with WMI and Camera order #4

Closed FrancescoBonizzi closed 4 years ago

FrancescoBonizzi commented 4 years ago

Hi and thanks for you project!

Since I'm not good with C++, neither with low level Windows libraries, I'd like to ask you two questions, please.

  1. What's the difference between your code and the builtin Windows WMI camera enumeration? (Here it is C# code)

    
    public static List<CameraDevice> GetAllConnectedCameras()
    {
    var cameras = new List<CameraDevice>();
    using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE (PNPClass = 'Camera')"))
    {
        foreach (var device in searcher.Get())
        {
            cameras.Add(new CameraDevice()
            {
                Name = device["Caption"].ToString(),
                Status = device["Status"].ToString(),
                DeviceId = device["DeviceID"].ToString()
            });
        }
    }
    
    cameras = cameras.OrderBy(c => c.DeviceId).ToList();
    for (int i = 0; i < cameras.Count; ++i)
        cameras[i].OpenCvId = i;
    
    return cameras;
    }

public class CameraDevice { public int OpenCvId { get; set; }

public string Name { get; set; }
public string DeviceId { get; set; }
public string Status { get; set; }

}


2. To map your enumeration result to the correct OpenCv indexes, how do you sort the enumeration? In other words, why these two cameras have that ids? Did you sort by `Path`?

== VIDEO DEVICE (id:0) == Name: HD Pro Webcam C920 Path: \?\usb#vid_046d&pid_082d&mi_00#7&3561b19d&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global

== VIDEO DEVICE (id:1) == Name: FLIR USB Video Path: \?\usb#vid_09cb&pid_1007&mi_00#6&1e15415f&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global



Thanks a lot!
studiosi commented 4 years ago

I am not sure about the WMI code, but regarding the order in which the devices are stored, this C++ library doesn't need to order anything, as it enumerates directly from the operating system, which provides said order.

FrancescoBonizzi commented 4 years ago

I understood, thanks: Windows just gives the list in that order.

I asked you that because with WMI Windows lists the devices with always the same order, maybe it is sorting by name or something else, while OpenCv sorts by the connection date of the device. I noticed that because if I have 2 webcams, the second plugged has always the id 1.

Thanks for your work!