python - 如何从图像采集卡中抓取帧?

标签 python opencv webcam video-capture

我正在尝试编写一个自定义软件(在 python 中)以从图像采集卡中抓取帧 ( Hauppauge WinTV-HVR-1900 ),但我无法让它工作。捆绑的 WinTV 软件运行完美,我能够从 USB 网络摄像头抓取帧,所以我知道硬件工作正常。不过,我还是设法捕捉到了一些东西,因为我的屏幕是全黑的,但会更改为正确的尺寸和分辨率,所以我的直觉是这是某种解码问题。

谁能帮我指出或提供从此类硬件中提取帧的代码示例?我应该写一个完整的自定义驱动程序吗?或者也许使用 VLC python bindings (因为 VLC 确实成功读取了视频流)?

编辑:

基本上,我的问题归结为:我的图像采集卡与网络摄像头有何不同,因为集成的硬件编码器产生 MPEG-2 流?不应该像我的网络摄像头一样工作?

我尝试的逻辑文件(在 Windows 7 上使用 pyQt4 和 python 2.7 的 QT 框架):

#-*- coding: utf-8 -*-
import sys
from VideoCapture import Device
#import Gui files and classes 
import FloGui
import deviceDialog
# PyQT4 imports
from PyQt4 import QtGui, QtCore

class devicedialog(QtGui.QDialog, deviceDialog.Ui_streamDialog):
    #the logic of the streaming device choice dialog
    def __init__(self,parent=None):
        super(devicedialog,self).__init__(parent)
        self.setupUi(self)
        self.retranslateUi(self)
        self.index = None
        i=0
        self.camlist=list()
        while True:
            try:
                cam = Device(i,0)
                name = cam.getDisplayName()
                dev = [name,i]
                self.camlist.append(dev)
                i+=1
                del cam
            except:
                break
        for j in xrange(len(self.camlist)):
            item = QtGui.QListWidgetItem(self.camlist[j][0])
            self.deviceListBox.addItem(item)
        self.exec_()

    def accept(self):
        selected = self.deviceListBox.currentItem().text()
        for k in xrange(len(self.camlist)):
            if self.camlist[k][0]==selected:
                self.index = k
        QtGui.QDialog.accept(self)

class Flolog(QtGui.QMainWindow,FloGui.Ui_MainWindow):
    """
    Flolog is inherited from both QtGui.QDialog and FloGui.Ui_MainWindow
    """
    def __init__(self, parent=None):
        """
            Initialization of the class. Call the __init__ for the super classes
        """
        super(Flolog,self).__init__(parent)
        self.setupUi(self)
        self.retranslateUi(self)
        self.connectActions()

    def streamchoice(self):
        streamdialog = devicedialog()
        self.VideoWidget.stop()
        self.VideoWidget.path = streamdialog.index
        self.VideoWidget.load()
        self.playButton.setEnabled(True)

    def menuloadfile(self):
        filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', '.')
        self.VideoWidget.stop()
        self.VideoWidget.path = str(filename)
        self.VideoWidget.load()
        self.playButton.setEnabled(True)


    def playbutton(self):
        self.VideoWidget.play()
        self.playButton.setEnabled(False)

    def main(self):
        self.show()

    def quit_(self):
        sys.exit(0)

    def connectActions(self):
        """
        Connect the user interface controls to the logic
        """
        self.actionFile.triggered.connect(self.menuloadfile)
        self.actionStream.triggered.connect(self.streamchoice)
        self.actionQuit.triggered.connect(self.quit_)
        self.playButton.clicked.connect(self.playbutton)
        self.stopButton.clicked.connect(self.VideoWidget.stop)




if __name__=='__main__':
    app = QtGui.QApplication(sys.argv)
    Flologob = Flolog()
    Flologob.main()
    sys.exit(app.exec_())

最佳答案

我终于解决了这个问题。问题源于这样一个事实,即首先应该手动或在代码中指定图形驱动程序应使用哪个视频设备。我通过 v4l2 图形驱动程序切换设备在 Linux Ubuntu 上解决了这个问题。我知道在 Windows 上可以执行等效操作。

关于python - 如何从图像采集卡中抓取帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20463824/

相关文章:

javascript - 如何查看用户的网络摄像头?

android - 网络摄像头直播android

python - python 对给定键集的 csv 进行排序

python - 如何在子进程 Popen 中引用工作区中的文件?

opencv - 尺度不变性和方向不变性是什么意思?

python - 可以使用Python 3.5.2下载OpenCV吗?

python - 网络摄像头无法在 Ubuntu 12.04 上使用 OpenCV 2.4 的 python 接口(interface)

python - Python 中 Matlab sprand() 的等价性?

python - 为什么 time.strptime ("Sept. 30, 2014", "%b. %d, %Y") 不工作

android - 如何从图像数据集创建模型并在 Chainer 中使用它?