Creating a Camera Application using Pyqt5

Creating a Camera Application using Pyqt5

Are you looking to create a camera application using Pyqt5? You have come to the right place. In this article, we will discuss how to create a camera application that captures video from a camera and displays it in a Pyqt5 GUI.

Prerequisites

Before we begin, make sure you have installed the following:

  • Python 3.x
  • Pyqt5
  • OpenCV

You can install Pyqt5 and OpenCV using pip. Run the following command:

pip install pyqt5 opencv-python-headless

Importing necessary libraries

Firstly, we need to import the necessary libraries in our Python script. Open your Python editor and import the following libraries:

import sys
from PyQt5.QtWidgets import QApplication, QDialog, QHBoxLayout, QVBoxLayout, QLabel
from PyQt5.QtCore import Qt, QTimer
import cv2
import numpy as np

Defining the GUI

Next, we will define the GUI. We will create a simple GUI that consists of a label and a button. The label will display the camera video feed, and the button will allow us to start and stop the camera feed.

class CameraGUI(QDialog):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        # Create labels for the camera feed
        self.displayLabel = QLabel()
        self.displayLabel.setAlignment(Qt.AlignCenter)
        self.displayLabel.setMinimumSize(320, 240)

        # Create a button to start and stop the feed
        self.button = QPushButton('Start Camera')
        self.button.clicked.connect(self.startCamera)

        # Create a horizontal layout for the button
        buttonLayout = QHBoxLayout()
        buttonLayout.addWidget(self.button)

        # Create a vertical layout for the GUI and add the button and labels
        mainLayout = QVBoxLayout()
        mainLayout.addWidget(self.displayLabel)
        mainLayout.addLayout(buttonLayout)

        # Set the layout for the GUI
        self.setLayout(mainLayout)
        self.setWindowTitle('CameraGUI')

Capturing Video from Camera

Now, let’s define the function that will capture the video from our camera.

class CameraGUI(QDialog):
    def __init__(self):
        ...

    def startCamera(self):
        # Create a video capture object
        self.cap = cv2.VideoCapture(0)

        # Set the video capture frame width and height
        self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
        self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)

        # Create a timer to read the camera feed and update the label
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.updateFrame)
        self.timer.start(5)

    def updateFrame(self):
        # Read the camera feed
        ret, frame = self.cap.read()

        # Convert the video frame to RGB
        rgbImage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

        # Convert the RGB image to QImage
        h, w, ch = rgbImage.shape
        qImg = QImage(rgbImage.data, w, h, ch * w, QImage.Format_RGB888)

        # Set the QImage to the display label
        self.displayLabel.setPixmap(QPixmap.fromImage(qImg))

The startCamera() function creates a video capture object using OpenCV’s cv2.VideoCapture() function. We then set the width and height of the video capture frame. Finally, we create a timer that reads the camera feed and calls the updateFrame() function to update the GUI.

The updateFrame() function takes the captured frame and converts it to RGB format using OpenCV’s cv2.cvtColor() function. We then convert the RGB image to a QImage that can be set to the label in our GUI using QPixmap.fromImage().

Stopping the Camera

Finally, we need to create the function that will stop the camera feed when the button is pressed.

class CameraGUI(QDialog):
    def __init__(self):
        ...

    def stopCamera(self):
        self.timer.stop()
        self.cap.release()
        self.displayLabel.clear()

    def startCamera(self):
        ...

    def updateFrame(self):
        ...

The stopCamera() function stops the timer, releases the video capture object, and clears the display label.

Running the Application

That’s it! We have defined the necessary functions to create a camera application using Pyqt5. Now, let’s run the application.

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = CameraGUI()
    window.show()
    app.exec_()

Conclusion

In this article, we discussed how to create a camera application using Pyqt5 that captures video from a camera and displays it in a GUI. We defined the necessary functions to start and stop the camera feed, and update the GUI with the captured frames. With these functions, you now have the foundation to create your own camera application. Happy coding!

Like(0)

Related

  • No articles