This is a simple web-based face recognition attendance system implemented using Flask and OpenCV.
The system captures real-time video feed from the webcam, detects faces using Haar cascade classifier, and recognizes them based on a pre-trained KNN classifier. Users can add new faces to the system, and the attendance of recognized individuals is logged in a SQL Database.
Clone the repository:
git clone https://github.com/scorpionTaj/face-recognition.git
Install dependencies:
pip install -r requirements.txt
Run the Flask application:
python app.py
Open a web browser and go to http://localhost:5000/
.
Click on the "Prendre la Présence" button to begin capturing attendance.
To add a new user, click on the "Ajouter un Nouvel Utilisateur" button and follow the instructions.
app.py
: Main Flask application file.templates/
: HTML templates for the web interface.static/
: Static files including user images and Trained Model.haarcascade_frontalface_default.xml
: Pre-trained Haar cascade classifier for face detection.haarcascade_frontalface_default.xml
) to detect faces in real time.Attendance is stored in a SQLite database with the following schema:
Field | Description |
---|---|
id | Unique record ID |
prenom | Name of the person |
emp_id | Employee ID |
arrivee | Check-in time |
depart | Check-out time |
date | Date of attendance |
@app.route("/add", methods=["GET", "POST"]) def add(): newusername = request.form["newusername"] newuserid = request.form["newuserid"] userimagefolder = f"static/faces/{newusername}_{newuserid}" os.makedirs(userimagefolder, exist_ok=True) # Capture images and train the model ... train_model()
def identify_face(facearray): model = joblib.load("static/face_recognition_model.pkl") return model.predict(facearray)
The system uses a KNN model trained on user-provided face images. The training steps include:
The model evaluation includes:
These metrics are visualized as a bar chart for better understanding.
@app.route("/metrics") def metrics(): # Load metrics from a pickle file with open("static/metrics.pkl", "rb") as f: metrics = joblib.load(f) # Generate a bar chart for visualization ...
To export attendance as a CSV file:
def export_to_csv(): names, rolls, arrivees, departs, _ = extract_attendance() csv_output = StringIO() csv_writer = csv.writer(csv_output) csv_writer.writerow(["Prénom", "N° Emp", "Temps d'arrivée", "Temps de Départ"]) for name, roll, arrive, depart in zip(names, rolls, arrivees, departs): csv_writer.writerow([name, roll, arrive, depart]) return Response( csv_output.getvalue(), mimetype="text/csv", headers={ "Content-Disposition": f"attachment;filename=attendance_{datetoday}.csv" }, )
scorpionTaj
GitHub Repository: face-recognition
There are no datasets linked
There are no datasets linked