列出可用的相机 OpenCV/Python [英] List available cameras OpenCV/Python

查看:139
本文介绍了列出可用的相机 OpenCV/Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 PC 上连接了多个网络摄像头,我想根据其信息(名称、分辨率等)选择一台摄像头.有没有办法列出 PC 上所有可用的摄像机,而不是尝试 cv2.VideoCapture() 中的所有索引?

I have multiple webcams connected to my PC and I would like to select one camera based on its info (name, resolution etc.). Is there a way to list all the cameras available on a PC, instead of trying all the indices in cv2.VideoCapture()?

推荐答案

答案是否定的.OpenCV 没有列出系统上可用视频捕获设备的方法.如果您查看代码,您会看到 OpenCV 当前如何处理不存在的无效设备索引.例如,对于 MacOS,这里是 代码:

The answer is negative. OpenCV doesn't have a method for listing the available video capture devices on your system. If you look at the code you see how currently OpenCV handles invalid device indices that don't exist. For instance for MacOS here is the code:

if ( cameraNum < 0 || devices.count <= NSUInteger(cameraNum) ) {
    fprintf(stderr, "OpenCV: out device of bound (0-%ld): %d\n", devices.count-1, cameraNum);
    [localpool drain];
    return 0;
}

您看到 devices.count 返回可用设备的数量,但 OpenCV 没有将其返回给用户的方法.

You see devices.count returns the number of available devices but OpenCV doesn't have a method to return that to the user.

Windows 的相关代码是 :

The relevant code for Windows is here:

if ((unsigned)m_deviceID >= m_devices.Get()->Size)
{
    OutputDebugStringA("Video::initGrabber - no video device found\n");
    return false;
}

同样没有将 m_devices.Get()->Size 返回给用户的函数.Linux 代码稍微复杂一些.

Again there is no function for returning m_devices.Get()->Size to the user. The Linux code is a bit more complex.

如果您从代码构建 OpenCV,您可以添加一个返回可用设备数量的函数.或者甚至更好地向 OpenCV 提交拉取请求以及您的补丁.

If you're building OpenCV from code you could add a function that returns the number of available devices. Or even better submit a pull request to OpenCV with your patch.

这篇关于列出可用的相机 OpenCV/Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆