简体   繁体   English

使用PyQt4进行OpenCV视频捕获

[英]OpenCV Video Capture with PyQt4

I have been trying to capture video using PyQt4 GUI and OpenCV. 我一直在尝试使用PyQt4 GUI和OpenCV捕获视频。 I created some buttons like "Start", "End" etc to control the capturing. 我创建了一些按钮,如“开始”,“结束”等控制捕获。 Starting is fine, but I can't stop the capture. 开始很好,但我无法阻止捕获。 To stop the capture, I need to break the while loop in startCapture() function below, I couldn't achieve it. 要停止捕获,我需要在下面的startCapture()函数中打破while循环,我无法实现它。

Currently, endCapture() destroys the window, but startCapture while loop simply creates it and continue the capturing. 目前, endCapture()会破坏窗口,但startCapture while循环只是创建它并继续捕获。 Only option is to break that loop. 唯一的选择是打破这个循环。

Below is the code I used: 以下是我使用的代码:

import cv2
import numpy as np
from PyQt4 import QtGui, QtCore

def startCapture(cap):
    print "pressed start"
    while(True):
        ret, frame = cap.read()
        cv2.imshow("Capture", frame)
        cv2.waitKey(5)
    cv2.destroyAllWindows() 

def endCapture(cap):
    print "pressed End"
    cv2.destroyAllWindows()

def quitCapture(cap):
    print "pressed Quit"
    cv2.destroyAllWindows()
    cap.release()
    QtCore.QCoreApplication.quit()

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

        c = cv2.VideoCapture(0)

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

        self.start_button = QtGui.QPushButton('Start',self)
        self.start_button.clicked.connect(lambda : startCapture(c, True))

        self.end_button = QtGui.QPushButton('End',self)
        self.end_button.clicked.connect(lambda : endCapture(c))

        self.quit_button = QtGui.QPushButton('Quit',self)
        self.quit_button.clicked.connect(lambda : quit(c))

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

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

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    sys.exit(app.exec_())

Can any one suggest how to break that loop and exit the capturing? 任何人都可以建议如何打破这个循环并退出捕获?

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

    def startCapture(self):
        print "pressed start"
        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):
        print "pressed End"
        self.capturing = False
        # cv2.destroyAllWindows()

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

in Window: 在窗口中:

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

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

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

The answer above is great, but for for a beginner like me it's hard to see where the part 'in Window' should go and how much of the original code should remain. 上面的答案很棒,但对于像我这样的初学者来说,很难看出“窗口”中的部分应该去哪里以及应该保留多少原始代码。 Here is the full working code based on the information above: 以下是基于以上信息的完整工作代码:

import cv2
from PyQt4 import QtGui, QtCore


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

    def startCapture(self):
        print "pressed start"
        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):
        print "pressed End"
        self.capturing = False

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


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

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

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

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

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

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

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


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM