Open In App

Creating a Camera Application using Pyqt5

Last Updated : 23 Dec, 2021
Like Article

Prerequisite: Introduction to pyqt-5

PyQt5 is a cross-platform GUI toolkit, a set of python bindings for Qt v5. One can develop an interactive desktop application with so much ease because of the tools and simplicity provided by this library.

In this article, we will see how we can create a simple camera application using PyQt5, which will capture the images and save them in the desired location which can be changed any time.

GUI Implementation Steps : 

  1. Create the main window.
  2. Add QCameraViewfinder object as the central widget to the main window.
  3. Add a status bar to the window for status tips. 
  4. Add the toolbar to the window. 
  5. Add Click and change location actions to the toolbar, and a combo box for selecting the camera, below is how toolbar will look like.

front end of pyqt5 cam

Back-End Implementation Steps : 

  1. Create a path variable and set it currently to blank. 
  2. Add available cameras to the combo box and set the first camera as default. 
  3. Add action to the Take Photo button. 
  4. Inside the click action, capture the photo at the given path with name as a timestamp and increment the count.
  5. Add action to the change location button.
  6. Inside the change location action, open the dialog box to get the path and update the path and the counter.
  7. Add action to the combo box.
  8. Inside the combo, box action get the selected camera, set viewfinder to it, and the mode of capture. 
  9. Create an alert method that shows an error message if any error is shown while selecting the path and changing the camera.

Below is the implementation :


# importing required libraries
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from PyQt5.QtMultimediaWidgets import *
import os
import sys
import time
# Main window class
class MainWindow(QMainWindow):
    # constructor
    def __init__(self):
        # setting geometry
        self.setGeometry(100, 100,
                         800, 600)
        # setting style sheet
        self.setStyleSheet("background : lightgrey;")
        # getting available cameras
        self.available_cameras = QCameraInfo.availableCameras()
        # if no camera found
        if not self.available_cameras:
            # exit the code
        # creating a status bar
        self.status = QStatusBar()
        # setting style sheet to the status bar
        self.status.setStyleSheet("background : white;")
        # adding status bar to the main window
        # path to save
        self.save_path = ""
        # creating a QCameraViewfinder object
        self.viewfinder = QCameraViewfinder()
        # showing this viewfinder
        # making it central widget of main window
        # Set the default camera.
        # creating a tool bar
        toolbar = QToolBar("Camera Tool Bar")
        # adding tool bar to main window
        # creating a photo action to take photo
        click_action = QAction("Click photo", self)
        # adding status tip to the photo action
        click_action.setStatusTip("This will capture picture")
        # adding tool tip
        click_action.setToolTip("Capture picture")
        # adding action to it
        # calling take_photo method
        # adding this to the tool bar
        # similarly creating action for changing save folder
        change_folder_action = QAction("Change save location",
        # adding status tip
        change_folder_action.setStatusTip("Change folder where picture will be saved saved.")
        # adding tool tip to it
        change_folder_action.setToolTip("Change save location")
        # setting calling method to the change folder action
        # when triggered signal is emitted
        # adding this to the tool bar
        # creating a combo box for selecting camera
        camera_selector = QComboBox()
        # adding status tip to it
        camera_selector.setStatusTip("Choose camera to take pictures")
        # adding tool tip to it
        camera_selector.setToolTip("Select Camera")
        # adding items to the combo box
                                  for camera in self.available_cameras])
        # adding action to the combo box
        # calling the select camera method
        # adding this to tool bar
        # setting tool bar stylesheet
        toolbar.setStyleSheet("background : white;")
        # setting window title
        self.setWindowTitle("PyQt5 Cam")
        # showing the main window
    # method to select camera
    def select_camera(self, i):
        # getting the selected camera = QCamera(self.available_cameras[i])
        # setting view finder to the camera
        # setting capture mode to the camera
        # if any error occur show the alert self.alert(
        # start the camera
        # creating a QCameraImageCapture object
        self.capture = QCameraImageCapture(
        # showing alert if error occur
        self.capture.error.connect(lambda error_msg, error,
                                   msg: self.alert(msg))
        # when image captured showing message
        self.capture.imageCaptured.connect(lambda d,
                                           i: self.status.showMessage("Image captured : " 
                                                                      + str(self.save_seq)))
        # getting current camera name
        self.current_camera_name = self.available_cameras[i].description()
        # initial save sequence
        self.save_seq = 0
    # method to take photo
    def click_photo(self):
        # time stamp
        timestamp = time.strftime("%d-%b-%Y-%H_%M_%S")
        # capture the image and save it on the save path
                                          "%s-%04d-%s.jpg" % (
        # increment the sequence
        self.save_seq += 1
    # change folder method
    def change_folder(self):
        # open the dialog to select path
        path = QFileDialog.getExistingDirectory(self
                                                "Picture Location", "")
        # if path is selected
        if path:
            # update the path
            self.save_path = path
            # update the sequence
            self.save_seq = 0
    # method for alerts
    def alert(self, msg):
        # error message
        error = QErrorMessage(self)
        # setting text to the error message
# Driver code
if __name__ == "__main__" :
  # create pyqt5 app
  App = QApplication(sys.argv)
  # create the instance of our Window
  window = MainWindow()
  # start the app

Output : 
When we click on the change location, this dialog will appear 

camera application output

Let’s go to this folder and see if the images are captured or not 

camera application click images


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads