FACIAL ATTENDANCE SYSTEM - LOGIC OVERVIEW ======================================== 1. USER REGISTRATION & FACE EMBEDDING ------------------------------------- - Users register by providing their name, email, and employee code, and by capturing/uploading 3 face images. - Each image is processed using the insightface model (Python backend) to extract a face embedding (vector representation). - Embeddings are stored in the database (as JSON) along with the image path and user reference. - Only images with a valid detected face are saved; others are discarded. 2. REAL-TIME FACE DETECTION (FRONTEND) --------------------------------------- - The browser loads face-api.js models (TinyFaceDetector) for fast, lightweight face detection. - The user's webcam stream is analyzed in real time; when a face is detected, a frame is captured. - The captured frame is converted to a base64-encoded JPEG and sent to the backend via AJAX (fetch POST request). - No manual button press is needed; detection and sending are automatic. 3. ATTENDANCE/LOGOUT MARKING (BACKEND) --------------------------------------- - The backend receives the image data and decodes it to a NumPy array (OpenCV image). - insightface is used to extract the face embedding from the received image. - The new embedding is compared to all stored user embeddings using cosine similarity. - If a match (similarity > 0.5) is found, the corresponding user is identified. - For attendance: checks if the user has already marked 'login' today; if not, creates a new 'login' event. - For logout: checks if the user has already marked 'logout' today; if not, creates a new 'logout' event. - The result (success, already marked, or error) is sent back to the frontend for user feedback. 4. MATCHING LOGIC ----------------- - Cosine similarity is used to compare the new face embedding with each stored embedding. - A threshold (0.5) is used to determine a match (can be tuned for stricter/looser matching). - Only the first matching user is used (no multi-user detection per frame). 5. EVENT TYPE HANDLING ---------------------- - Each attendance record has an 'event_type' field: 'login' or 'logout'. - The system prevents duplicate login/logout events for the same user on the same day. - Logs show both event types for full traceability. 6. LOGS, ADMIN, AND EXPORT -------------------------- - Recent attendance/logout events are shown on the main pages. - Full logs can be filtered by name, employee code, or date. - Logs can be exported as CSV for reporting. - Django admin is enabled for user and attendance management. 7. ERROR HANDLING & OPTIMIZATIONS --------------------------------- - Handles cases where no face is detected (frontend and backend). - Handles cases where no matching user is found. - Only sends frames to the backend when a face is detected (reduces server load). - Uses async/await and event listeners for smooth, non-blocking UI. - Cleans up user images from disk when a user is deleted. This logic ensures a seamless, automatic, and robust facial attendance workflow for both users and administrators.