列出可用的相机 OpenCV/Python
List available cameras OpenCV/Python
我有多个网络摄像头连接到我的 PC,我想 select 一个基于其信息(名称、分辨率等)的摄像头。有没有办法列出 PC 上所有可用的相机,而不是尝试 cv2.VideoCapture()?
中的所有索引
答案是否定的。 OpenCV 没有列出系统上可用视频捕获设备的方法。如果您查看代码,您会发现 OpenCV 当前如何处理不存在的无效设备索引。例如,对于 MacOS,这里是 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
return 可用设备的数量,但 OpenCV 没有方法 return 向用户提供。
Windows的相关代码是here:
if ((unsigned)m_deviceID >= m_devices.Get()->Size)
{
OutputDebugStringA("Video::initGrabber - no video device found\n");
return false;
}
同样,没有 returning m_devices.Get()->Size
给用户的功能。 Linux 代码有点复杂。
如果您从代码构建 OpenCV,您可以添加一个 return 可用设备数量的函数。或者甚至更好地使用您的补丁向 OpenCV 提交拉取请求。
要回答您的问题标题,您可以使用 while 循环:
import cv2
def list_ports():
"""
Test the ports and returns a tuple with the available ports and the ones that are working.
"""
is_working = True
dev_port = 0
working_ports = []
available_ports = []
while is_working:
camera = cv2.VideoCapture(dev_port)
if not camera.isOpened():
is_working = False
print("Port %s is not working." %dev_port)
else:
is_reading, img = camera.read()
w = camera.get(3)
h = camera.get(4)
if is_reading:
print("Port %s is working and reads images (%s x %s)" %(dev_port,h,w))
working_ports.append(dev_port)
else:
print("Port %s for camera ( %s x %s) is present but does not reads." %(dev_port,h,w))
available_ports.append(dev_port)
dev_port +=1
return available_ports,working_ports
在您的代码上实现该解决方案非常简单。
版本 2
正如@ketza 所注意到的,可能存在工作端口不连续的情况,此版本将在退出 while 循环之前测试至少 5 个非工作端口:
import cv2
def list_ports():
"""
Test the ports and returns a tuple with the available ports and the ones that are working.
"""
non_working_ports = []
dev_port = 0
working_ports = []
available_ports = []
while len(non_working_ports) < 6: # if there are more than 5 non working ports stop the testing.
camera = cv2.VideoCapture(dev_port)
if not camera.isOpened():
non_working_ports.append(dev_port)
print("Port %s is not working." %dev_port)
else:
is_reading, img = camera.read()
w = camera.get(3)
h = camera.get(4)
if is_reading:
print("Port %s is working and reads images (%s x %s)" %(dev_port,h,w))
working_ports.append(dev_port)
else:
print("Port %s for camera ( %s x %s) is present but does not reads." %(dev_port,h,w))
available_ports.append(dev_port)
dev_port +=1
return available_ports,working_ports,non_working_ports
第一个安装包:
pip 安装 pygrabber==0.1
代码#
from pygrabber.dshow_graph import FilterGraph
graph = FilterGraph()
print(graph.get_input_devices())# list of camera device
try:
device =graph.get_input_devices().index("name camera that I want to use it ")
except ValueError as e:
device = graph.get_input_devices().index("Integrated Webcam")#use default camera if the name of the camera that I want to use is not in my list
vid=cv2.VideoCapture(device)
我有多个网络摄像头连接到我的 PC,我想 select 一个基于其信息(名称、分辨率等)的摄像头。有没有办法列出 PC 上所有可用的相机,而不是尝试 cv2.VideoCapture()?
中的所有索引答案是否定的。 OpenCV 没有列出系统上可用视频捕获设备的方法。如果您查看代码,您会发现 OpenCV 当前如何处理不存在的无效设备索引。例如,对于 MacOS,这里是 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
return 可用设备的数量,但 OpenCV 没有方法 return 向用户提供。
Windows的相关代码是here:
if ((unsigned)m_deviceID >= m_devices.Get()->Size)
{
OutputDebugStringA("Video::initGrabber - no video device found\n");
return false;
}
同样,没有 returning m_devices.Get()->Size
给用户的功能。 Linux 代码有点复杂。
如果您从代码构建 OpenCV,您可以添加一个 return 可用设备数量的函数。或者甚至更好地使用您的补丁向 OpenCV 提交拉取请求。
要回答您的问题标题,您可以使用 while 循环:
import cv2
def list_ports():
"""
Test the ports and returns a tuple with the available ports and the ones that are working.
"""
is_working = True
dev_port = 0
working_ports = []
available_ports = []
while is_working:
camera = cv2.VideoCapture(dev_port)
if not camera.isOpened():
is_working = False
print("Port %s is not working." %dev_port)
else:
is_reading, img = camera.read()
w = camera.get(3)
h = camera.get(4)
if is_reading:
print("Port %s is working and reads images (%s x %s)" %(dev_port,h,w))
working_ports.append(dev_port)
else:
print("Port %s for camera ( %s x %s) is present but does not reads." %(dev_port,h,w))
available_ports.append(dev_port)
dev_port +=1
return available_ports,working_ports
在您的代码上实现该解决方案非常简单。
版本 2
正如@ketza 所注意到的,可能存在工作端口不连续的情况,此版本将在退出 while 循环之前测试至少 5 个非工作端口:
import cv2
def list_ports():
"""
Test the ports and returns a tuple with the available ports and the ones that are working.
"""
non_working_ports = []
dev_port = 0
working_ports = []
available_ports = []
while len(non_working_ports) < 6: # if there are more than 5 non working ports stop the testing.
camera = cv2.VideoCapture(dev_port)
if not camera.isOpened():
non_working_ports.append(dev_port)
print("Port %s is not working." %dev_port)
else:
is_reading, img = camera.read()
w = camera.get(3)
h = camera.get(4)
if is_reading:
print("Port %s is working and reads images (%s x %s)" %(dev_port,h,w))
working_ports.append(dev_port)
else:
print("Port %s for camera ( %s x %s) is present but does not reads." %(dev_port,h,w))
available_ports.append(dev_port)
dev_port +=1
return available_ports,working_ports,non_working_ports
第一个安装包: pip 安装 pygrabber==0.1
代码#
from pygrabber.dshow_graph import FilterGraph
graph = FilterGraph()
print(graph.get_input_devices())# list of camera device
try:
device =graph.get_input_devices().index("name camera that I want to use it ")
except ValueError as e:
device = graph.get_input_devices().index("Integrated Webcam")#use default camera if the name of the camera that I want to use is not in my list
vid=cv2.VideoCapture(device)