Building a QR Code Attendance System
In this comprehensive guide, I'll show you how to build a QR Code Attendance System using Python, OpenCV, and MySQL. This project combines computer vision, database management, and GUI development.
Project Overview
The QR Code Attendance System automates the process of tracking student or employee attendance by scanning QR codes. Here's what we'll build:
QR Code Scanner
Real-time QR code detection using OpenCV and webcam integration.
Database Management
Store attendance records in MySQL with student information and timestamps.
GUI Interface
User-friendly interface built with Tkinter for easy operation.
Required Libraries
pip install opencv-python
pip install mysql-connector-python
pip install qrcode
pip install pillow
pip install tkinter
Key Features
- Real-time QR Detection: Instant scanning and recognition of QR codes
- Database Integration: Automatic storage of attendance records
- User Management: Add, edit, and manage student/employee profiles
- Attendance Reports: Generate detailed attendance reports and analytics
- Export Functionality: Export attendance data to CSV/Excel formats
Implementation Steps
1. Database Setup
First, create the MySQL database and tables for storing attendance data.
CREATE DATABASE attendance_system;
USE attendance_system;
CREATE TABLE students (
id INT PRIMARY KEY AUTO_INCREMENT,
student_id VARCHAR(20) UNIQUE,
name VARCHAR(100),
email VARCHAR(100),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE attendance (
id INT PRIMARY KEY AUTO_INCREMENT,
student_id VARCHAR(20),
check_in TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
check_out TIMESTAMP NULL,
FOREIGN KEY (student_id) REFERENCES students(student_id)
);
2. QR Code Generation
Generate unique QR codes for each student containing their ID.
import qrcode
import os
def generate_qr_code(student_id, name):
qr = qrcode.QRCode(version=1, box_size=10, border=5)
qr.add_data(student_id)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
filename = f"qr_codes/{student_id}_{name}.png"
img.save(filename)
return filename
3. QR Code Scanner
Implement real-time QR code scanning using OpenCV.
import cv2
from pyzbar import pyzbar
def scan_qr_code():
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
barcodes = pyzbar.decode(frame)
for barcode in barcodes:
student_id = barcode.data.decode('utf-8')
# Process attendance
mark_attendance(student_id)
cv2.imshow('QR Scanner', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Pro Tips
- • Use threading to prevent GUI freezing during scanning
- • Implement error handling for database connections
- • Add logging for debugging and monitoring
- • Consider adding face recognition for additional security