Skip to content
AstralSurgeLearnings XP 0 Streak 0
AstralSurgeLearnings

Drowsiness Detection Systems using ML

Driver drowsiness and microsleep episodes are among the leading causes of automotive accidents worldwide. Building a machine learning-based Driver Monitoring System (DMS) combines real-time computer vision, facial landmark tracking, and statistical classifiers to detect signs of fatigue before accidents occur.

1. Key Behavioral and Physiological Indicators

Non-invasive camera-based systems monitor several key visual features:

  • Eye Closure Duration (PERCLOS): The percentage of time over a 60-second window that the eyes remain closed at least 80%. It is the gold standard metric in physiological sleep research.
  • Eye Aspect Ratio (EAR): A geometric metric calculated from facial landmark coordinates surrounding the eyelids.
  • Yawn Frequency (Mouth Aspect Ratio - MAR): Measures vertical and horizontal lip displacement to track yawning frequency and duration.
  • Head Pose Estimation: Tracks pitch, roll, and yaw angles to detect head nodding or drooping associated with fatigue.

2. Mathematical Foundation: Eye Aspect Ratio (EAR)

Using facial landmark detectors (such as MediaPipe Face Mesh or Dlib's 68-point model), each eye is defined by 6 coordinates (p1 through p6). The EAR measures the ratio of vertical distances between eyelids relative to the horizontal eye width:

EAR = (||p2 - p6|| + ||p3 - p5||) / (2.0 * ||p1 - p4||)

When eyes are open, EAR remains relatively constant (typically between 0.25 and 0.35). During a blink or prolonged closure, EAR drops rapidly toward zero. If the EAR stays below a defined threshold for consecutive frames (e.g., 20–30 frames at 30 FPS, representing ~1 second), an alarm is triggered.

3. Python Implementation Example

Below is a practical implementation using Euclidean geometry to compute EAR and track frame-by-frame closure thresholds:

import numpy as np
from scipy.spatial import distance as dist

def calculate_ear(eye_landmarks):
    # Compute the euclidean distances between vertical eye landmarks
    a = dist.euclidean(eye_landmarks[1], eye_landmarks[5])
    b = dist.euclidean(eye_landmarks[2], eye_landmarks[4])

    # Compute the euclidean distance between horizontal eye landmarks
    c = dist.euclidean(eye_landmarks[0], eye_landmarks[3])

    # Calculate Eye Aspect Ratio
    ear = (a + b) / (2.0 * c)
    return ear

# System configuration constants
EAR_THRESHOLD = 0.25
CONSECUTIVE_FRAMES = 30  # ~1 second at 30 FPS

frame_counter = 0
alarm_triggered = False

def process_frame_landmarks(left_eye, right_eye):
    global frame_counter, alarm_triggered
    
    left_ear = calculate_ear(left_eye)
    right_ear = calculate_ear(right_eye)
    avg_ear = (left_ear + right_ear) / 2.0

    if avg_ear < EAR_THRESHOLD:
        frame_counter += 1
        if frame_counter >= CONSECUTIVE_FRAMES:
            alarm_triggered = True
            print("[ALERT] Drowsiness detected! Sounding audio warning.")
    else:
        frame_counter = 0
        alarm_triggered = False

4. Enhancing Detection with Machine Learning

  • Temporal Sequence Models (LSTM / GRU): Feed time-series sequences of EAR, MAR, and head-pitch values into a recurrent neural network to capture temporal patterns instead of relying on fixed frame thresholds.
  • Ensemble Classifiers (Random Forest, XGBoost): Combine blink duration, blink frequency, and steering wheel variability signals into tabular features for robust multimodal predictions.
  • Production Considerations: Deploy near-infrared (NIR) cameras to ensure reliable face detection in nighttime driving conditions, and optimize models using TensorRT or ONNX Runtime for edge deployment on low-power in-vehicle microprocessors.

0 Comments