Skip to content

JosselinPerret/StayFocus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stay Focus: Real-Time Attention Monitoring System

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.

System Demonstration Figure 1: Real-time tracking and visual distraction alert execution.


System Features

  • 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 threading to 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.

System Interface

The graphical user interface overlays telemetry data directly onto the video feed, providing immediate feedback on the user's attention state.

Focused Status Interface Figure 2: System interface displaying normal operational status ("Attention OK") and active telemetry.


Project Structure

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

Installation

Prerequisites

  • Python 3.8 or higher
  • Active webcam
  • Google Chrome (required for the Selenium WebDriver)

Setup Instructions

  1. Clone the repository:

    git clone https://github.com/yourusername/stay-focus.git
    cd stay-focus
  2. Initialize a virtual environment:

    # Windows
    python -m venv .venv
    .venv\Scripts\activate
    
    # macOS/Linux
    python3 -m venv .venv
    source .venv/bin/activate
  3. Install dependencies:

    pip install -r requirements.txt

Usage

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.

Configuration

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
    # ...

Technical Architecture

  1. Capture: cv2.VideoCapture retrieves frames.
  2. 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).
  3. 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.
  4. Execution: The main thread updates the OpenCV UI with a visual warning, while a daemon thread initializes the Selenium WebDriver to prevent process blocking.

License

Refer to the LICENSE file for distribution and usage rights.

About

Stay focus or it will remind you.

Resources

Stars

Watchers

Forks

Contributors

Languages