DEV Community

Debasis Panigrahi
Debasis Panigrahi

Posted on

Building a Real-Time Edge AI Driver Attention & Safety Monitoring System

OverviewDrowsy and distracted driving remains one of the primary contributors to vehicle accidents globally. Traditional edge systems often struggle with false positives—sounding alarms when a vehicle is safely parked, misreading passengers, or failing under harsh nighttime glare and low-light cabin environments. To solve this, I engineered an edge-first Driver Drowsiness & Attention Monitoring System using Python, MediaPipe, OpenCV, and PyQt6, built with a 3-thread decoupled architecture, low-light adaptive CLAHE, 3D head pose tracking, and vehicle motion gating. Key Architectural HighlightsAdaptive Low-Light Preprocessing: When ambient cabin illumination drops ($L < 65.0$), the pipeline applies CLAHE across the LAB color space with Gamma correction ($\gamma = 0.6$) and glare suppression to maintain eyelid landmark tracking at night without false closures. Multi-Signal Attention Classification: Combines Eye Aspect Ratio (EAR), Mouth Aspect Ratio (MAR), and solvePnP 3D Head Pose Estimation (Pitch, Yaw, Roll) to penalize micro-sleeps, yawning escalations ($\ge 3$ yawns in a 5-minute rolling window), and head drooping. Cabin Multi-Face ROI Isolation: Evaluates up to 4 concurrent faces in the camera frame and gates alerting solely to the primary driver's Region of Interest (ROI), preventing sleeping passengers from triggering alerts. Optical Flow Vehicle Motion Gating: Peripheral frame differencing evaluates vehicle movement so audible buzzer alarms sound only while in motion, automatically muting when parked or stationary. Dual-Tier Dismissal & Remote Web Access: Incorporates a local cognitive math verification puzzle alongside a remote Flask admin web panel accessible via local Wi-Fi or mobile hotspot QR codes. System Pipeline & Core Implementation[Camera Feed / USB]


[Thread 1: DirectShow Frame Ingestion]


[Thread 2: Computer Vision Pipeline]
├── Low-Light CLAHE & Glare Reduction
├── MediaPipe FaceMesh (Driver ROI Isolation)
├── EAR / MAR / solvePnP Euler Pose Derivation
├── Background Optical Flow Motion Detection
└── Multi-Signal State Machine Classifier


[Thread 3: Main PyQt6 UI + Background Flask Daemon]
├── Real-time HUD Visualizer & Cognitive Puzzle Modal
└── Remote LAN/Hotspot Admin Panel (PIN Override & Metrics)
Feature Extraction Code SnippetPythonimport numpy as np

def calculate_ear(eye_landmarks: np.ndarray) -> float:
"""Computes Eye Aspect Ratio (EAR) from 6 canonical 2D eye landmarks."""
# Vertical landmark Euclidean distances
a = np.linalg.norm(eye_landmarks[1] - eye_landmarks[5])
b = np.linalg.norm(eye_landmarks[2] - eye_landmarks[4])
# Horizontal landmark Euclidean distance
c = np.linalg.norm(eye_landmarks[0] - eye_landmarks[3])

return float((a + b) / (2.0 * c)) if c > 0 else 0.0
Enter fullscreen mode Exit fullscreen mode

Verification & PerformanceTest Suite: 54/54 passing unit and integration tests covering finite state transitions, motion gating, and remote buzzer endpoints. Processing Latency: Operates at $\ge 20\text{ FPS}$ on standard CPU hardware with $< 100\text{ms}$ cold start latency using DirectShow capture. Extensibility: Integrated SQLite persistence for custom user calibration baselines and audit logging.

Top comments (0)