python - 使用 PyQt4 进行 OpenCV 视频捕获

标签 python opencv user-interface pyqt pyqt4

我一直在尝试使用 PyQt4 GUI 和 OpenCV 捕获视频。我创建了一些按钮,如“开始”、“结束”等来控制捕获。开始很好,但我无法停止捕获。要停止捕获,我需要打破下面 startCapture() 函数中的 while 循环,但我无法实现。

目前,endCapture() 会破坏窗口,但 startCapture while loop 只是创建它并继续捕获。唯一的选择是打破这个循环。

下面是我使用的代码:

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_())

谁能建议如何打破这个循环并退出捕获?

最佳答案

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()

在窗口中:

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)

关于python - 使用 PyQt4 进行 OpenCV 视频捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21169908/

相关文章:

python - Django REST Framework - 使用 reverse() 时的 NoReverseMatch

python - CV2 Python VideoCapture(0) 意外参数

opencv 示例不起作用(detectors_descriptors_evaluation.cpp)

java - StereoBM 无法提供正确的输出

python - PyQt:不均等地划分QHBoxLayout中小部件占用的区域

python - 已指定检测参数(argparse)

python - 尝试在定义循环中使用双重条件(?)

python - 使用 shell 脚本 Ubuntu 遍历目录中的文件

user-interface - 重新设计复杂 UI 的技术

android - 在 RelativeLayout 中对齐组件