Stay Focus is a computer vision application that monitors user attention in real-time. It utilizes MediaPipe for facial landmark detection to calculate head pose and eye aspect ratio (EAR). If sustained distraction or drowsiness is detected, the system triggers a concurrent background process to launch a specified URL via Selenium as a corrective mechanism.
Figure 1: Real-time tracking and visual distraction alert execution.
- Real-Time Facial Landmarking: Maps 478 3D facial landmarks using MediaPipe's
FaceLandmarker. - Head Pose Estimation: Calculates X and Y head orientation offsets relative to the shoulders to monitor gaze direction.
- Drowsiness Detection: Computes the Eye Aspect Ratio (EAR) to detect prolonged eye closure.
- Asynchronous Event Triggering: Implements Python
threadingto execute Selenium browser instances in the background, ensuring zero latency in the OpenCV video feed. - Automated Initialization: Automatically retrieves the required MediaPipe model weights (
face_landmarker_v2_with_blendshapes.task) upon first execution.
The graphical user interface overlays telemetry data directly onto the video feed, providing immediate feedback on the user's attention state.
Figure 2: System interface displaying normal operational status ("Attention OK") and active telemetry.
stay_focus/
├── .venv/ # Python virtual environment
├── image/ # Documentation assets
│ ├── attenion_ok.png
│ └── demo.gif
├── .gitignore # Git ignore rules
├── face_landmarker_v2... # MediaPipe model weights
├── LICENSE # License documentation
├── main.py # Core application logic
├── README.md # System documentation
└── requirements.txt # Dependency list
- Python 3.8 or higher
- Active webcam
- Google Chrome (required for the Selenium WebDriver)
-
Clone the repository:
git clone https://github.com/yourusername/stay-focus.git cd stay-focus -
Initialize a virtual environment:
# Windows python -m venv .venv .venv\Scripts\activate # macOS/Linux python3 -m venv .venv source .venv/bin/activate
-
Install dependencies:
pip install -r requirements.txt
Execute the main script to initialize the tracking system:
python main.py- The system will display the video feed with the overlaid telemetry UI.
- To safely terminate the application and close all associated browser drivers, press 'q' while the video window is in focus.
To modify the URL triggered upon distraction detection, update the launch_browser function in main.py:
def launch_browser():
# ...
driver.get("https://careers.mcdonalds.com.sg/") # Modify this target
# ...- Capture:
cv2.VideoCaptureretrieves frames. - Analysis: MediaPipe extracts facial landmarks. The system calculates distances between specific nodal points (nose to eye center for pose; upper to lower eyelids for EAR).
- Evaluation: Measurements are smoothed using a rolling 5-frame history (
collections.deque). If thresholds are exceeded for a sustained period, a distraction event is flagged. - Execution: The main thread updates the OpenCV UI with a visual warning, while a daemon thread initializes the Selenium WebDriver to prevent process blocking.
Refer to the LICENSE file for distribution and usage rights.