1-Hour Course: Face Recognition Attendance with PyQt GUI in Python using OpenCV and Computer Vision (2021)

Posted by


Introduction

Welcome to this in-depth tutorial on developing a Face Recognition Attendance GUI using PyQt, OpenCV, and Python. By the end of this 1-hour course, you will have a fully functional application that can recognize faces and mark attendance in real-time.

Prerequisites
Before you begin this tutorial, make sure you have the following prerequisites:

  • Basic knowledge of Python programming
  • Understanding of OpenCV library
  • Understanding of PyQt library
  • Installed Python 3.x
  • Installed OpenCV and PyQt libraries

If you are new to these technologies, I recommend going through some introductory tutorials to get familiar with them.

Setting up the Environment
First, let’s set up our environment by installing the required libraries. Open Terminal or Command Prompt and run the following commands:

pip install opencv-python
pip install PyQt5

Creating the GUI
Now, it’s time to create the Graphical User Interface (GUI) for our Face Recognition Attendance system using PyQt. Open your favorite code editor and start by importing the necessary libraries:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QLabel, QGridLayout, QListWidget
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtCore import QTimer
import cv2
import numpy as np

Next, create a class that will represent our Face Recognition Attendance GUI:

class FaceRecognitionAttendance(QWidget):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Face Recognition Attendance")
        self.setGeometry(100, 100, 800, 600)

        self.capture = cv2.VideoCapture(0)
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.update_frame)
        self.timer.start(10)  # Update the frame every 10 milliseconds

        layout = QVBoxLayout()
        self.setLayout(layout)

        self.video_label = QLabel(self)
        layout.addWidget(self.video_label)

        self.names_list = QListWidget(self)
        layout.addWidget(self.names_list)

        self.names = []

        self.show()

In the __init__ method, we set the window title, geometry, and create a VideoCapture object to capture video from the default webcam. We also create a QTimer to update the video frame, set to trigger every 10 milliseconds.

Next, we create a QVBoxLayout to arrange our widgets vertically within the window. We add a QLabel to display the video feed and a QListWidget to show the names of the recognized faces.

Implementing the Face Recognition Algorithm
Now, let’s implement the face recognition algorithm using the OpenCV library. Add the following method to the FaceRecognitionAttendance class:

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

        if ret:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            frame = cv2.flip(frame, 1)
            height, width, channel = frame.shape
            bytes_per_line = channel * width

            q_image = QImage(frame.data, width, height, bytes_per_line, QImage.Format_RGB888)
            pixmap = QPixmap.fromImage(q_image)
            self.video_label.setPixmap(pixmap)

In the update_frame method, we read a frame from the webcam, convert it to RGB format, and display it in the QLabel widget.

Adding Face Recognition Logic
Next, let’s add the face recognition logic to detect faces and mark attendance. Add the following method to the FaceRecognitionAttendance class:

def recognize_faces(self, frame):
        face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)

        for (x, y, w, h) in faces:
            cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
            face_roi = frame[y:y+h, x:x+w]

            name = "Unknown"
            # Perform face recognition

            self.names_list.addItem(name)

In the recognize_faces method, we first load the Haar cascade classifier for face detection. We then detect faces in the frame, draw a rectangle around each face, and extract the face ROI (Region of Interest).

For now, we are assigning the name "Unknown" to each detected face. You can replace this with a face recognition algorithm like LBPH or Eigenfaces to accurately recognize faces.

Running the Application
To run the Face Recognition Attendance GUI, create an instance of the FaceRecognitionAttendance class and start the Qt application event loop:

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = FaceRecognitionAttendance()
    sys.exit(app.exec_())

Save your code as face_recognition_attendance.py and run it using the following command in Terminal or Command Prompt:

python face_recognition_attendance.py

Conclusion
Congratulations! You have successfully developed a Face Recognition Attendance GUI using PyQt, OpenCV, and Python. In this tutorial, you learned how to create a GUI, capture video from the webcam, implement face recognition logic, and mark attendance in real-time.

Feel free to customize the application further by adding features like database integration, automatic email notifications, or integrating with an attendance system. Keep experimenting and learning to enhance your skills in computer vision and application development. Thank you for following along with this tutorial!

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

You can enroll in the full YOLOv4 PyQt Course here: ►YOLOv4 PyQt Course – https://augmentedstartups.info/yolov4release

@maslowmbouolangtiotsa8315
3 months ago

thank you for this video

I would like to know if it was possible to integrate your own yolo model

@user-wp5cg8nc5s
3 months ago

how I add other detail of person such as Occupatuion , Role to the image data

@user-cj8we5ir7n
3 months ago

What is the password to open the compressed file?

@loojiazheng8199
3 months ago

Is that can convert to apk?

@patr2002
3 months ago

20:58

@NiharGandhi
3 months ago

How can we add a Search Box ? So basically, if we want to search if the person is in the Attendance.csv or not using PyQt5 ?

@EvanArca
3 months ago

It is possible to recreate this on kivy? And make it a mobile app?

@santosanthosh8121
3 months ago

Hello bro The tutorial was superb!!! But I have an doubt can you tell me how to add a new photo details???

@sashanks2609
3 months ago

hi sir I am unable to download pyqt5-tools in python 3.10 the tutorial was very helpful as i couldn't install it i am unable to design my gui

@arn3081
3 months ago

Bro i m facing issue in downloading face recognition package n dlib
,same we r facing in pycharm .bro which terminal should i use ,we were working on windows 10 .
Pls bro help me

@funbee746
3 months ago

Lots of error..explain import files and code

@hieuvu6543
3 months ago

thank you so much

@tohidnaaz7216
3 months ago

From where u have taken that imgLabel am not able to find it in my pyQT??? Help me plz

@RickCodez
3 months ago

No sequence , just nothing, bullshit tutorial

@it33sevillajhaymarm.68
3 months ago

Sir can we connect this program to my sql database and how it is done?

@muhammadabdullahi3732
3 months ago

Hello. I’m having errors while trying to install PyQt5-tools

@ranarashid7483
3 months ago

Why green bounding box around the face is not working correctly?

@ranarashid7483
3 months ago

is there any way to convert this GUI into exe

@iAm_Rohit_Raj
3 months ago

57:15
How can I change the Augmented Startups logo and put my own?
Changing the logo.png in folder didn't worked.
I have to show this project as my assignment in college