Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中通过摄影机枚举_Python_Opencv_Gstreamer_Python Gstreamer - Fatal编程技术网

在Python中通过摄影机枚举

在Python中通过摄影机枚举,python,opencv,gstreamer,python-gstreamer,Python,Opencv,Gstreamer,Python Gstreamer,我在多个操作系统上用Python枚举摄像头时遇到一些问题 以下是我尝试过的一些方法: import cv2 as cv num = 0 while 1: cap = cv.VideoCapture(num) if cap.isOpened(): # working capture num += 1 else: break 使用Opencv的缺点是Opencv不提供任何友好的显示名称。此外,枚举摄影机的速度很慢,因为您需要实

我在多个操作系统上用Python枚举摄像头时遇到一些问题

以下是我尝试过的一些方法:

import cv2 as cv
num = 0
while 1:
    cap = cv.VideoCapture(num)
    if cap.isOpened():
        # working capture
        num += 1
    else:
        break
使用Opencv的缺点是Opencv不提供任何友好的显示名称。此外,枚举摄影机的速度很慢,因为您需要实际打开和关闭摄影机以检查它是否是有效的摄影机

我也尝试过使用PyPylon和pyuvc之类的库。它们有效,但只适用于特定品牌

我对堆栈溢出做了一些研究,一些人建议python的gstreamer绑定作为一种可能的独立于操作系统的解决方案。这就是我目前所拥有的

import pgi
pgi.require_version("Gtk", "3.0")
pgi.require_version("Gst", "1.0")
pgi.require_version("GstVideo", "1.0")
from pgi.repository import Gtk, GObject, Gst, GstVideo

Gst.init("")
dm = Gst.DeviceMonitor()
dm.set_show_all_devices(True)

dm.start()

print("Displaying devices.")
for device in dm.get_devices():
    print(device.get_display_name())
print("Displaying providers.")
for provider in dm.get_providers():
    print(provider)

dm.stop()
这是我得到的输出:

Displaying devices.
papalook Microphone
DisplayPort
HDMI
Built-in Output
Built-in Microph
Displaying providers.
osxaudiodeviceprovider
出于某种原因,我没有网络摄像头,只有音频设备。 你知道我做错了什么吗? 我应该采取什么不同的方法?
谢谢。

对于Windows,您可以使用pygrabber库,这是一个纯python工具,用于从相机捕获照片,并使用DirectShow和OpenCV进行简单的图像处理。它还列举了连接的网络摄像头,如下所示:

from __future__ import print_function
from pygrabber.dshow_graph import FilterGraph

graph = FilterGraph()
print(graph.get_input_devices())
我已经修改了这个库,使其可以在Python 2和3下工作。下载地址:

仅适用于Python 3的原始源代码: