How 050903/aifacedetectionproject works
Project type and purpose
• This is a stand-alone Python command-line & GUI tool (an “open-source tool”) for real-time drowsiness and yawn detection using a webcam.
• Its goal is to capture video frames, detect faces and facial landmarks, compute eye/mouth aspect ratios, and trigger visual/sound alerts and logging when thresholds are crossed.
a. Input Layer
– Webcam (video stream)
– Command-line arguments (thresholds, flags, model path, log file path)
b. Initialization
– Argument parser (argparse)
– Load Dlib pre-trained model (shape_predictor_68_face_landmarks.dat)
– Initialize OpenCV VideoCapture
– Initialize logging (optional file drowsiness_log.txt)
c. Frame Processing Pipeline
1. & Preprocess
• Grab frame → resize → convert to grayscale
2. Face Detection & Landmark Prediction
3. Metric Calculation
• EAR module (Eye Aspect Ratio)
• MAR module (Mouth Aspect Ratio)
• PERCLOS module (sliding‐window percentage of eye closure)
4. Alert Decision Logic
• Compare EAR/MAR/PERCLOS against thresholds
• Maintain counters (consecutive frames, yawns, PERCLOS window)
d. Output & Side Effects
1. Visualization
• Screen flash overlay on alert
2. Audible Alerts
• winsound thread (Windows only), toggled ON/OFF at runtime
3. Event Logging
• Write timestamped entries to log file when events occur (session start, drowsiness, yawn, alarm toggles)
4. Runtime Controls
• Hotkey handlers for ESC, r, +/–, f, a
• Command-line arguments → Initialization modules (feeds thresholds & flags)
• Webcam → Frame Preprocessing → Face Detection → Landmark Prediction → Metric Calculators → Alert Logic
• Alert Logic → Visualization, Sound, Logging
• Visualization & Sound → User (feedback loop)
• Hotkeys → Runtime controls → Alert Logic / Display / Logging
Architectural patterns & principles
• Modular pipeline: each stage is a separate responsibility (separation of concerns)
• Data flow orchestration: linear, frame-by-frame processing
• Event logging as a cross-cutting concern
• Runtime configurability via CLI & hotkeys
Technologies & libraries (annotate each component)
• Python 3.x (core)
• OpenCV (frame capture, display, GUI)
• Dlib (face detection, landmarks)
• NumPy/SciPy (calculations)
• argparse (configuration)
• winsound & threading (audio alerts)
• collections.deque (PERCLOS history)