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.
Currently, endCapture() destroys the window, but startCapture while loop simply creates it and continue the capturing. 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?
OpenCV provides a very simple interface to do this. Let's capture a video from the camera (I am using the built-in webcam on my laptop), convert it into grayscale video and display it. Just a simple task to get started. To capture a video, you need to create a VideoCapture object.
PyQt is a Python binding for Qt, which is a set of C++ libraries and development tools providing platform-independent abstractions for graphical user interfaces (GUIs). Qt also provides tools for networking, threads, regular expressions, SQL databases, SVG, OpenGL, XML, and many other powerful features.
The key difference in the two versions — in fact the entire reason PySide2 exists — is licensing. PyQt5 is available under a GPL or commercial license, and PySide2 under a LGPL license.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With