使用PyQt4的OpenCV视频捕获 [英] OpenCV Video Capture with PyQt4
问题描述
我一直在尝试使用PyQt4 GUI和OpenCV捕获视频.我创建了一些按钮,例如开始",结束"等来控制捕获.开始很好,但是我无法停止捕获.要停止捕获,我需要在下面的 startCapture()函数中中断while循环,但无法实现.
I have been trying to capture video using PyQt4 GUI and 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.
当前, endCapture()破坏了窗口,但是 startCapture while循环只是创建它并继续捕获.唯一的选择是打破那个循环.
Currently, endCapture() destroys the window, but startCapture while loop simply creates it and continue the capturing. Only option is to break that 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_())
任何人都可以建议如何打破这种循环并退出捕获吗?
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()
在窗口中:
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)
这篇关于使用PyQt4的OpenCV视频捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!