Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Adding timeout counter to a video stream



  • So i'm trying to create a simple photobooth application that uses the default camera (e.g. webcam) to stream live video in a QCameraViewFinder. I want to be able to show a countdown on the screen and then some camera flash animation after which the picture will be saved in host computer. I"m having difficulty adding the timeout on screen and don't know how to do animation for camera flash. Any help would be appreciated. Here's the code

    code_text
    from PyQt5.QtGui import *
    from PyQt5.QtWidgets import *
    from PyQt5.QtCore import *
    from PyQt5.QtPrintSupport import *
    from PyQt5.QtMultimedia import *
    from PyQt5.QtMultimediaWidgets import *
    
    import os
    import sys
    import time
    
    CAMERA_TIMEOUT = 3      # Timeout in seconds
    
    class MainWindow(QMainWindow):
    
        def __init__(self, *args, **kwargs):
            super(MainWindow, self).__init__(*args, **kwargs)
    
            self.available_cameras = QCameraInfo.availableCameras()
    
            self.timeout = CAMERA_TIMEOUT
            self.timeout_label = QLabel(str(self.timeout))
            self.timeout_label.setFont(QFont('Courier',26))
            self.timeout_label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
    
            # If no camera is found, show error and exit application
    
            if not self.available_cameras:
                error_msg = QErrorMessage(self)
                error_msg.showMessage("Camera not found! Please connect one and try again.\nExiting...")
                error_msg.exec_()
    
                print('No camera found! Exiting...', file=sys.stderr)
                sys.exit(0)
    
            self.status = QStatusBar()
            self.setStatusBar(self.status)
    
            self.save_path = ""
    
            self.viewfinder = QCameraViewfinder()
            self.viewfinder.show()
            self.setCentralWidget(self.viewfinder)
            
            # Set the default camera.
            self.select_camera(0)
    
            # Setup tools
            camera_toolbar = QToolBar("Camera")
            camera_toolbar.setIconSize(QSize(14, 14))
            self.addToolBar(camera_toolbar)
    
            photo_action = QAction(QIcon(os.path.join('images', 'camera-black.png')), "Take photo...", self)
            photo_action.setStatusTip("Take photo of current view")
            photo_action.triggered.connect(self.take_photo)
            camera_toolbar.addAction(photo_action)
    
            change_folder_action = QAction(QIcon(os.path.join('images', 'blue-folder-horizontal-open.png')), "Change save location...", self)
            change_folder_action.setStatusTip("Change folder where photos are saved.")
            change_folder_action.triggered.connect(self.change_folder)
            camera_toolbar.addAction(change_folder_action)
    
    
            camera_selector = QComboBox()
            camera_selector.addItems([c.description() for c in self.available_cameras])
            camera_selector.currentIndexChanged.connect( self.select_camera )
    
            camera_toolbar.addWidget(camera_selector)
    
    
            self.setWindowTitle("Python PhotoBooth")
            self.show()
    
        def select_camera(self, i):
            
            self.camera = QCamera(self.available_cameras[i])    
            
            self.camera.setViewfinder(self.viewfinder)
            self.camera.setCaptureMode(QCamera.CaptureStillImage)
            self.camera.error.connect(lambda: self.alert(self.camera.errorString()))
            self.camera.start()
    
            self.capture = QCameraImageCapture(self.camera)
            self.capture.error.connect(lambda i, e, s: self.alert(s))
            self.capture.imageCaptured.connect(lambda d, i: self.status.showMessage("Image %04d captured" % self.save_seq))
    
            self.current_camera_name = self.available_cameras[i].description()
            self.save_seq = 0
    
        def take_photo(self):
    
    #        self.addWidget(self.timeout_label)
    
            timestamp = time.strftime("%d-%b-%Y-%H_%M_%S")
            self.capture.capture(os.path.join(self.save_path, "%s-%04d-%s.jpg" % (
                self.current_camera_name,
                self.save_seq,
                timestamp
            )))
            self.save_seq += 1
    
        def change_folder(self):
            path = QFileDialog.getExistingDirectory(self, "Snapshot save location", "")
            if path:
                self.save_path = path
                self.save_seq = 0
    
        def alert(self, s):
            """ 
            Handle errors coming from QCamera dn QCameraImageCapture by displaying alerts.
            """
            err = QErrorMessage(self)
            err.showMessage(s)
    
    
    if __name__ == '__main__':
    
        app = QApplication([])
        app.setApplicationName("Python PhotoBooth")
    
        window = MainWindow()
        app.exec_()```

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Do you want the timer to be on top of the video or in a widget above it ?


Log in to reply