Skip to content
Nur Amin
← All projects
P-09Sistem Presensi Pengenalan Wajah2025

Face Recognition Attendance (KNN)

Attendance taken from a face, with blink and smile checks so a photograph cannot sign in on someone’s behalf.

My role
Recognition model, camera program, Flask admin and PHP API.
Field
Machine Learning & AI, IoT & Embedded, Web Development
Stack
  • Python
  • OpenCV
  • scikit-learn
  • dlib
  • Flask
  • PHP
  • MySQL

Overview

Every attendance system is really an anti-cheating system wearing a friendly interface. Recognition alone does not get you there — a printed photo held up to a webcam recognises perfectly well. So the system asks the face to do something a photograph cannot: blink, and smile. Only then does it accept the match.

The rest is bookkeeping done honestly. Check-in up to the start time counts as on time; after that it is recorded as late, with exactly how late; once closing time has passed, a check-in is refused. A check-out before closing is accepted but recorded as early, with the duration, rather than quietly passing as a normal day. The result is read aloud so the person knows what was stored.

A Flask web interface sits on top so an administrator can enrol people, capture or re-capture their face data, edit or remove them, and export a daily recap to Excel. Both the camera program and the admin reach MySQL through a small PHP API that requires an API key, and the face samples themselves never leave the machine.

In pictures

The “Absen Anti Nitip” camera window recognising a face with both smile and blink checks passed, next to the daily attendance recap in the Flask app.
FIG. 01The camera station and the admin recap. Redrawn with an illustrated face and sample data; the app UI is in Indonesian.
Four steps — detect, crop to 128 × 128, flatten to 49,152 values, classify with KNN — above the cosine-distance gate for unknown faces and the enrolment details.
FIG. 02KNN always picks someone, so a separate distance check decides whether the face is known at all.
Eye landmarks and the eye-aspect-ratio formula, a chart of the ratio dipping below 0.2 during a blink, and three cases: a printed photo, a person yet to blink, and a person who blinked and smiled.
FIG. 03A photo can’t blink: nothing is recorded until a blink and a smile are both seen.
Timelines for check-in and check-out around 08:00 and 16:00, example scans with their statuses, and the spoken messages.
FIG. 04One pure function turns the clock and today’s record into a status and a spoken message.
Diagram: the camera program and the Flask admin call a PHP API with an API key, which writes to MySQL; below, the user management page.
FIG. 05Two clients, one small PHP API, one MySQL database — and face data that stays on the machine.

Function

Model
KNN (k = 3) on 128 × 128 face pixels, with a cosine-distance gate for strangers
Liveness
Blink (eye aspect ratio from dlib landmarks) and smile, both required
Rules
On time or late with duration; early check-out recorded as such

What it does

  1. 01Face detection with a Haar cascade and recognition with KNN; unknown faces are turned away by a cosine-distance threshold.
  2. 02Blink and smile detection as a liveness check.
  3. 03Check-in and check-out decided by one pure, unit-tested rules function.
  4. 04Spoken feedback in Indonesian for every result.
  5. 05Enrolment capturing personal data and 100 face samples, with re-capture later.
  6. 06Editing and deletion of users, including their records and face data.
  7. 07Daily attendance recap by date, exported to Excel.

Technical notes

Times
JAM_MASUK and JAM_PULANG set in .env (default 08:00 and 16:00)
Refused
Check-in after closing time, and any scan once both are recorded
Privacy
Face samples kept locally as .npz; the repository holds no biometric data