LBPH-based Face Recognition Algorithm for Automated Attendance Management System using RDBMS

Palash Hawee
3 min readMar 25, 2023

--

Face recognition technology has revolutionized the way attendance is marked in educational institutions and workplaces. Automated attendance management systems using face recognition technology offer several advantages over traditional methods of attendance management, such as reducing human errors and ensuring accurate and timely attendance tracking. Python provides a powerful platform for developing face recognition systems due to its ease of use and availability of various libraries and modules. In this system, we use the Local Binary Patterns Histograms (LBPH) algorithm for face recognition and a Relational Database Management System (RDBMS) to store attendance data. The LBPH algorithm extracts facial features and maps them to a high-dimensional space, which can be used to recognize faces with high accuracy. The RDBMS enables the storage and retrieval of attendance data, making it easy to generate reports and analyze attendance patterns. In this project, we will develop a face recognition attendance system using Python, LBPH algorithm, and RDBMS, which can be used in educational institutions and workplaces for efficient and accurate attendance management.

Let’s build a Face Recognition System using the following algorithm:

Step 1: Import required libraries and modules

import cv2
import numpy as np
import os
import pymysql

Step 2: Connect to the database

db = pymysql.connect(host='localhost', user='root', password='', db='attendance')

Step 3: Initialize LBPH Face Recognizer

recognizer = cv2.face.LBPHFaceRecognizer_create()

Step 4: Load trained data for face recognition

recognizer.read('trained_data.yml')

Step 5: Initialize video capture object and face detector

while True:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)

Step 7: Predict the face using LBPH and mark attendance

    for (x, y, w, h) in faces:
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
id_, confidence = recognizer.predict(roi_gray)

if confidence >= 45: # confidence threshold
cursor = db.cursor()
cursor.execute(f"SELECT name FROM students WHERE id={id_}")
name = cursor.fetchone()[0]
cv2.putText(img, name, (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)

# insert attendance record in database
cursor.execute(f"INSERT INTO attendance (student_id, date) VALUES ({id_}, CURRENT_DATE())")
db.commit()

cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)

cv2.imshow('Attendance System', img)
if cv2.waitKey(1) == ord('q'): # press 'q' to quit
break

Step 8: Release the video capture object and close the database connection

cap.release()
cv2.destroyAllWindows()
db.close()

Note: This algorithm assumes that you have a relational database called attendance with two tables students and attendance. The students table has columns id and name, and the attendance table has columns student_id and date. You also need to train the LBPH face recognizer on a dataset of student's faces and save the trained data as trained_data.yml.

You can find the full source code for the project here. Github: https://github.com/PalashHawee/Face-Recognition-System

Watch the Project video below:

https://www.youtube.com/watch?v=yBqUcTosCCk&t=2s

The use of face recognition technology in attendance management systems has greatly simplified the process of marking attendance and has reduced the time and effort required to do so. In this project, I used Python and the LBPH algorithm to develop an automated attendance management system that is both efficient and accurate. The LBPH algorithm is known for its high accuracy in recognizing faces, which makes it an ideal choice for face recognition systems. The use of a Relational Database Management System (RDBMS) further enhances the system’s functionality by enabling the storage and retrieval of attendance data for generating reports and analysis. Overall, this project demonstrates the power of Python and its libraries and modules in developing sophisticated and reliable systems for attendance management. By implementing this system, educational institutions and workplaces can benefit from faster and more accurate attendance tracking, allowing them to focus on other important aspects of their operations.

--

--