baoshiwei
2025-04-09 d3aacbbba47c393ef3645afdb8b3d940663328da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import cv2
import win32com.client
 
class CAM_UTIL:
    def __init__(self, cam1_name='USB Camera', cam2_name="PC Camera",):
        self.webcam_list = None
        self.cam1 = cam1_name
        self.cam2 = cam2_name
        self.list_webcams()
 
 
 
 
    # 获取摄像头列表
    def list_webcams(self):
        # 创建一个WMI客户端实例
        wmi = win32com.client.GetObject("winmgmts:")
        webcams = wmi.InstancesOf("Win32_PnPEntity")
        # 创建摄像头和索引字典
        webcam_dict = {}
        index = 0
        for device in webcams:
            name = getattr(device, 'Name', None)
            pnp_class = getattr(device, 'PNPClass', None)
            if name is not None and self.cam1 in name:
                # 将设备名字和索引添加到字典中
                webcam_dict[self.cam1] = index
                index += 1
            elif name is not None and self.cam2 in name:
                # 将设备名字和索引添加到字典中
                webcam_dict[self.cam2] = index
                index += 1
            self.webcam_list = webcam_dict
 
        return webcam_dict