Face Recognition Based Smart Attendance System with Real-Time Database | OpenCV | Python Project
Table of Contents
Introduction
In this tutorial, we will create a Face Recognition Based Smart Attendance System using Python, OpenCV, and Google Firebase. This system will automatically recognize and record attendance for students, employees, or teachers in real-time. The project is ideal for final year computer science students looking to implement machine learning and computer vision techniques.
Step 1: Set Up Your Development Environment
- Install Python: Ensure you have Python installed. You can download it from python.org.
- Install IDE: Choose an Integrated Development Environment (IDE) such as PyCharm or Visual Studio. Download links:
- Install Required Libraries: Open your terminal and run the following command to install the necessary packages:
pip install opencv-python face_recognition firebase-admin
Step 2: Capture Video Input with OpenCV
- Initialize Video Capture:
- Use OpenCV to capture video from your webcam.
- Code snippet to get started:
import cv2 video_capture = cv2.VideoCapture(0) while True: ret, frame = video_capture.read() cv2.imshow('Video', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break video_capture.release() cv2.destroyAllWindows()
Step 3: Load and Encode Face Images
- Load Images: Use the
face_recognition
library to load and encode images of the individuals whose attendance you want to track. - Encoding Faces:
- Save the images in a specific directory and use the following code to load and encode:
import face_recognition import os known_faces = [] known_names = [] for filename in os.listdir('path_to_images'): image = face_recognition.load_image_file(f'path_to_images/{filename}') encoding = face_recognition.face_encodings(image)[0] known_faces.append(encoding) known_names.append(filename.split('.')[0]) # Assuming filename is <name>.jpg
- Save the images in a specific directory and use the following code to load and encode:
Step 4: Create a CSV File for Attendance
- Generate CSV: Create a CSV file to store attendance data.
- Code to Create CSV:
import pandas as pd from datetime import datetime def create_attendance_csv(): date = datetime.now().strftime('%Y-%m-%d') filename = f'attendance_{date}.csv' df = pd.DataFrame(columns=['Name', 'Status']) df.to_csv(filename, index=False)
Step 5: Process Video Input and Recognize Faces
- Face Recognition Logic:
- Continuously read frames from the video capture and recognize faces.
- Code example:
while True: ret, frame = video_capture.read() # Resize frame for faster processing small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) # Find all face locations and encodings face_locations = face_recognition.face_locations(small_frame) face_encodings = face_recognition.face_encodings(small_frame, face_locations) for face_encoding in face_encodings: matches = face_recognition.compare_faces(known_faces, face_encoding) name = "Unknown" # Check if a match is found if True in matches: first_match_index = matches.index(True) name = known_names[first_match_index] # Update attendance in CSV update_attendance(name)
Step 6: Update Attendance in Real-Time Database
- Connect to Firebase: Use Firebase to update attendance data in real-time.
- Firebase Setup:
- Create a Firebase project and set up the database.
- Install the Firebase Admin SDK and initialize it in your project.
- Code to Update Attendance:
import firebase_admin from firebase_admin import credentials, firestore # Initialize Firebase cred = credentials.Certificate('path_to_your_firebase_credentials.json') firebase_admin.initialize_app(cred) db = firestore.client() def update_attendance(name): db.collection('attendance').add({ 'name': name, 'timestamp': datetime.now() })
Conclusion
In this tutorial, we built a Face Recognition Based Smart Attendance System using Python, OpenCV, and Firebase. We set up the environment, processed video input, recognized faces, and updated attendance records in real time. This project integrates practical applications of computer vision and machine learning, making it an excellent choice for academic projects.
Next steps include enhancing the system with features like user management, reporting, or integrating with a web interface for better usability.