Displaying Webcam Feed using QGraphicsView in Python with OpenCV and PyQT

Posted by


In this tutorial, we will create a webcam display using the Python programming language with the help of OpenCV and PyQt. We will be using the QGraphicsView widget from the PyQt library to display the webcam feed in a graphical interface.

Step 1: Install necessary libraries
First, you will need to install the necessary libraries. You can install them using pip by running the following commands in your terminal:

pip install opencv-python
pip install pyqt5

Step 2: Setting up the PyQt application
Now, let’s start by creating a PyQt application with a QGraphicsView widget to display the webcam feed. Create a new Python file and import the necessary modules:

import sys
from PyQt5.QtWidgets import QApplication, QGraphicsView, QGraphicsScene, QGraphicsPixmapItem
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtCore import Qt
import cv2

Next, create a class for our main application window:

class WebcamDisplay(QGraphicsView):
    def __init__(self):
        super().__init__()

        self.scene = QGraphicsScene()
        self.setScene(self.scene)

        self.camera = cv2.VideoCapture(0)

        self.timer = QTimer()
        self.timer.timeout.connect(self.update_frame)
        self.timer.start(1000/30)  # 30 frames per second

    def update_frame(self):
        ret, frame = self.camera.read()

        if ret:
            frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            h, w, ch = frame.shape
            image = QImage(frame_rgb, w, h, ch*w, QImage.Format_RGB888)
            pixmap = QPixmap.fromImage(image)

            self.scene.clear()
            self.scene.addPixmap(pixmap)
            self.setScene(self.scene)

In the __init__ method, we set up the QGraphicsView widget, create a QGraphicsScene to hold the webcam feed, initialize the camera to capture frames from the webcam, and set up a QTimer to update the frame every 1/30th of a second.

The update_frame method reads a frame from the camera, converts it to RGB format, creates a QImage from the frame, converts it to a QPixmap, and adds it to the QGraphicsScene.

Step 3: Running the application
To run the application, create an instance of the WebcamDisplay class within a QApplication:

if __name__ == "__main__":
    app = QApplication(sys.argv)

    window = WebcamDisplay()
    window.show()

    sys.exit(app.exec_())

Now, when you run the script, a window will appear displaying the webcam feed captured by your webcam. The feed will update in real-time at 30 frames per second.

In this tutorial, we have created a webcam display using the Python programming language with the help of OpenCV and PyQt. We used the QGraphicsView widget to display the webcam feed in a graphical interface. You can further customize the application by adding features such as image processing, face detection, or gesture recognition to enhance the functionality of the webcam display.

0 0 votes
Article Rating
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@Illyrium_sacrum
3 months ago

i bay your fuck code hahahh, you are crazy

@MotoGReviews
3 months ago

lol. so, you used uploaded this to show off?

@javedgulzar6046
3 months ago

where is source code