如何为视频播放器选择文件

时间:2019-02-08 15:48:35

标签: python opencv user-interface pyqt pyqt5

我是班级和PyQt5的新手,正在尝试使用PyQt5和Opencv构建视频播放器。使用OpenCV Video Capture with PyQt4中的代码,进行更改以将PyQt4中的命令转换为PyQt5并添加暂停按钮,我现在有了一个带有暂停,播放,结束和退出按钮的视频播放器。这是正常工作的代码:

import cv2
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QApplication, QPushButton, 
QVBoxLayout, QFileDialog

fileName = 'C:/Users/Art/Downloads/testVideo.mp4'


class Capture():
    def __init__(self):
        self.capturing = False
        self.c = cv2.VideoCapture(fileName)

    def startCapture(self):
        self.capturing = True
        cap = self.c
        while(self.capturing):
            ret, frame = cap.read()
            cv2.imshow("Capture", frame)
            cv2.waitKey(5)
        cv2.destroyAllWindows()

    def endCapture(self):
        self.capturing = False

    def pauseCapture(self): 
        if cv2.waitKey(0) & 0xFF == ord('p'):  # Pause
            self.capturing = False

    def quitCapture(self):
        cap = self.c
        cv2.destroyAllWindows()
        cap.release()
        QtCore.QCoreApplication.quit()

class Window(QtWidgets.QWidget):
    def __init__(self):

    QtWidgets.QWidget.__init__(self)
    self.setWindowTitle('Control Panel')

    self.capture = Capture()
    self.start_button = QPushButton('Start', self)
    self.start_button.clicked.connect(self.capture.startCapture)

    self.end_button = QPushButton('End', self)
    self.end_button.clicked.connect(self.capture.endCapture)

    self.pause_button = QPushButton('Pause', self)
    self.pause_button.clicked.connect(self.capture.pauseCapture)

    self.quit_button = QPushButton('Quit', self)
    self.quit_button.clicked.connect(self.capture.quitCapture)

    vbox = QVBoxLayout(self)
    vbox.addWidget(self.start_button)
    vbox.addWidget(self.end_button)
    vbox.addWidget(self.pause_button)
    vbox.addWidget(self.quit_button)

    self.setLayout(vbox)
    self.setGeometry(100, 100, 200, 200)
    self.show()

    if __name__== '__main__':
        import sys
        app = QApplication(sys.argv)
        window = Window()
        sys.exit(app.exec())

到目前为止,我已经将视频文件名及其路径硬编码为代码(fileName)。现在,我想添加一个加载按钮,让用户选择视频。像这样:

 self.load_button = QPushButton('Load', self)
 self.load_button.clicked.connect(self.pick_video)

 def pick_video():
     dialog = QtGui.QFileDialog()
     fileName = dialog.getExistingDirectory(None, 
      "Select Folder")
     return fileName

然后将加载按钮添加到现有按钮列表中,如下所示:

vbox.addWidget(self.load_button)

我的问题是我不知道如何将其合并到现有代码中。如果将其放在Window类中,则会引发错误。我的问题是如何在现有代码中添加类似内容,以便用户可以在按下加载按钮后选择视频文件。

编辑:基于@ekhumoro更改代码后,我得到了类似的内容:

import cv2
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QApplication, QPushButton, 
QVBoxLayout, QFileDialog


class Capture():
    def __init__(self):
        self.capturing = False

    def startCapture(self, path):
        self.capturing = True
        self.c = cv2.VideoCapture(path)
        while self.capturing:
            ret, frame = self.c.read()
            cv2.imshow("Capture", frame)
            cv2.waitKey(5)
        cv2.destroyAllWindows()

    def endCapture(self):
        self.capturing = False

    def pauseCapture(self): 
        if cv2.waitKey(0) & 0xFF == ord('p'):  # Pause
            self.capturing = False

    def quitCapture(self):
        cap = self.c
        cv2.destroyAllWindows()
        cap.release()
        QtCore.QCoreApplication.quit()

class Window(QtWidgets.QWidget):
    def __init__(self):

        QtWidgets.QWidget.__init__(self)
        self.setWindowTitle('Control Panel')

        self.capture = Capture()
        self.start_button = QPushButton('Start', self)
        self.start_button.clicked.connect(self.start)

        self.end_button = QPushButton('End', self)
        self.end_button.clicked.connect(self.capture.endCapture)

        self.pause_button = QPushButton('Pause', self)
        self.pause_button.clicked.connect(self.capture.pauseCapture)

        self.quit_button = QPushButton('Quit', self)
        self.quit_button.clicked.connect(self.capture.quitCapture)

        vbox = QVBoxLayout(self)
        vbox.addWidget(self.start_button)
        vbox.addWidget(self.end_button)
        vbox.addWidget(self.pause_button)
        vbox.addWidget(self.quit_button)

        self.setLayout(vbox)
        self.setGeometry(100, 100, 200, 200)
        self.show()

    def start(self):
        path = QtWidgets.QFileDialog.getOpenFileName(self)[0]
        if path:
            self.capture.startCapture(path)

if __name__== '__main__':
    import sys
    app = QApplication(sys.argv)
    window = Window()
    sys.exit(app.exec())

但是,当我运行此代码时,出现以下错误: AttributeError:“ Window”对象没有属性“ start” 。另一件事是,我想为此过程使用一个单独的按钮,这意味着一旦用户运行代码,在打开的窗口中,他/她可以单击该按钮(将其称为“加载”按钮),然后选择视频文件,我在这段代码中看不到。我在某处缺少什么吗?也许重新排列的代码不是@ekhumoro的意思。

1 个答案:

答案 0 :(得分:2)

重新排列代码,以使startCapture使用一个path参数。然后从开始按钮的插槽中的文件对话框中输入路径:

class Capture():
    def __init__(self):
        self.capturing = False

    def startCapture(self, path):
        self.capturing = True
        self.c = cv2.VideoCapture(path)
        while self.capturing:
            ret, frame = self.c.read()
            cv2.imshow("Capture", frame)
            cv2.waitKey(5)
        cv2.destroyAllWindows()

class Window(QtWidgets.QWidget):
    def __init__(self):
        ...
        self.start_button = QPushButton('Start', self)
        self.start_button.clicked.connect(self.start)
        ...

    def start(self):
        path = QtWidgets.QFileDialog.getOpenFileName(self)[0]
        if path:
            self.capture.startCapture(path)

这是一个完整的替代实现:

import cv2
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QApplication, QPushButton,QVBoxLayout, QFileDialog

class Capture():
    def __init__(self):
        self.capturing = False
        self.c = None

    def setVideoFile(self, path):
        if self.c is not None:
            cv2.destroyAllWindows()
            self.c.release()
        self.c = cv2.VideoCapture(path)
        self.startCapture()

    def startCapture(self):
        self.capturing = True
        cap = self.c
        while(self.capturing):
            ret, frame = cap.read()
            cv2.imshow("Capture", frame)
            cv2.waitKey(5)
        cv2.destroyAllWindows()

    def endCapture(self):
        self.capturing = False

    def pauseCapture(self):
        if cv2.waitKey(0) & 0xFF == ord('p'):  # Pause
            self.capturing = False

    def quitCapture(self):
        cap = self.c
        cv2.destroyAllWindows()
        cap.release()
        QtCore.QCoreApplication.quit()

class Window(QtWidgets.QWidget):
    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        self.setWindowTitle('Control Panel')

        self.capture = Capture()
        self.open_button = QPushButton('Open', self)
        self.open_button.clicked.connect(self.open)

        self.start_button = QPushButton('Start', self)
        self.start_button.clicked.connect(self.capture.startCapture)

        self.end_button = QPushButton('End', self)
        self.end_button.clicked.connect(self.capture.endCapture)

        self.pause_button = QPushButton('Pause', self)
        self.pause_button.clicked.connect(self.capture.pauseCapture)

        self.quit_button = QPushButton('Quit', self)
        self.quit_button.clicked.connect(self.capture.quitCapture)

        vbox = QVBoxLayout(self)
        vbox.addWidget(self.open_button)
        vbox.addWidget(self.start_button)
        vbox.addWidget(self.end_button)
        vbox.addWidget(self.pause_button)
        vbox.addWidget(self.quit_button)

        self.setLayout(vbox)
        self.setGeometry(100, 100, 200, 200)
        self.show()

    def open(self):
        path = QtWidgets.QFileDialog.getOpenFileName(self)[0]
        if path:
            self.capture.setVideoFile(path)

if __name__== '__main__':
    import sys
    app = QApplication(sys.argv)
    window = Window()
    sys.exit(app.exec())