OpenCV Video Capture with PyQt4

后端 未结 2 746
陌清茗
陌清茗 2021-01-03 03:01

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 s

相关标签:
2条回答
  • 2021-01-03 03:25

    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_())
    
    0 讨论(0)
  • 2021-01-03 03:31
    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)
    
    0 讨论(0)
提交回复
热议问题