在 Windows 10 上使用 open cv2 获取 USB 摄像头 ID

问题描述 投票:0回答:2

我有 4 个 USB 摄像头通过 USB 连接到我的电脑。目前我正在使用

opencv-python==4.5.5.64
连接到相机。问题是,我似乎无法读取每个摄像机的唯一 ID。目前我的代码片段如下所示,其中
cameraIndex
是一个整数。问题是,获得从我连接的相机读取唯一ID的上限后该怎么办?更好的是,是否可以仅使用相机的唯一 ID 连接到相机?我在 Windows 10 PC 上运行相机。

import cv2
cap = cv2.VideoCapture(cameraIndex, cv2.CAP_MSMF)
python windows opencv camera ms-media-foundation
2个回答
2
投票

我最近遇到了同样的问题,我能够在 here 找到使用 pywin32 的答案,以及另一个使用自定义 .pyd 库的 here。第一个答案最终更容易实现,这是一个片段:

import asyncio
import winrt.windows.devices.enumeration as windows_devices


CAMERA_NAME = "Dino-Lite Premier"

async def get_camera_info():
    return await windows_devices.DeviceInformation.find_all_async(4)

connected_cameras = asyncio.run(get_camera_info())
names = [camera.name for camera in connected_cameras]

if CAMERA_NAME not in names:
    print("Camera not found")
else:
    camera_index = names.index(CAMERA_NAME)
    print(camera_index)

0
投票

安装cv2_enumerate_cameras

pip install git+https://github.com/chinaheyu/cv2_enumerate_cameras.git

枚举相机信息。

import cv2
from cv2_enumerate_cameras import enumerate_cameras

for camera_info in enumerate_cameras(cv2.CAP_MSMF):
    print(camera_info)
    print(camera_info.path)
© www.soinside.com 2019 - 2024. All rights reserved.