A real-time web application that monitors user attention during video consumption using computer vision and eye-gaze tracking. Built with MediaPipe Face Landmark detection, this tool helps researchers and individuals understand distraction patterns while watching content.
The Distraction Detector uses advanced face landmark detection to track when users look away from the screen during video consumption. It provides real-time monitoring with a non-intrusive interface and comprehensive data logging for analysis.
- Real-time Face Tracking: Uses MediaPipe's Face Landmarker for accurate facial landmark detection
- Gaze Detection: Monitors eye movements to detect when users look away from the screen
- Non-intrusive Design: Webcam preview automatically hides after initial detection
- YouTube Integration: Embedded fullscreen YouTube video for testing scenarios
- Real-time Dashboard: Live monitoring interface for researchers/experimenters
- Data Export: CSV export functionality with customizable filenames
- WebSocket Communication: Real-time data streaming between components
βββββββββββββββββββ WebSocket βββββββββββββββββββ
β Main App βββββββββββββββββΊβ Dashboard β
β (index.html) β β (dashboard.html)β
β β β β
β β’ Face tracking β β β’ Event logging β
β β’ Gaze detectionβ β β’ Data export β
β β’ Timer logic β β β’ Live monitor β
βββββββββββββββββββ βββββββββββββββββββ
β
βΌ
βββββββββββββββββοΏ½οΏ½οΏ½β
β WebSocket β
β Server β
β (server.js) β
βββββββββββββββββββ
- Node.js (v16 or higher)
- Modern web browser with WebRTC support
- Webcam access
-
Clone the repository
git clone https://github.com/Samu01Tech/distraction-detector.git cd distraction-detector -
Install dependencies
npm install
-
Start the development server
npm run dev
-
Access the application
- Main application:
http://localhost:5173 - Dashboard:
http://localhost:5173/dashboard
- Main application:
npm run build
npm run start-
Initial Setup
- Open the main application URL
- Allow webcam access when prompted
- Wait for face detection to initialize
-
During the Session
- The webcam preview appears briefly for setup
- Preview automatically hides after 3 seconds of successful detection
- Watch the embedded YouTube video
- A small toggle button appears in the bottom-left corner to show/hide webcam preview if needed
-
Gaze Detection
- The system continuously monitors your face
- Looking away triggers are based on specific eye movement thresholds:
eyeLookDownRight> 0.3eyeLookInLeft> 0.4eyeLookOutRight> 0.4
-
Monitoring
- Open the dashboard in a separate browser window/tab
- View real-time events and connection status
- Monitor distraction events with duration and total time
-
Data Collection
- Events are logged automatically with timestamps
- View duration of each distraction event
- Track cumulative distraction time
-
Data Export
- Enter a custom filename in the dashboard
- Click "Download CSV" to export all collected data
- CSV includes: timestamp, event type, duration, total time, and details
| Event Type | Description | Data Fields |
|---|---|---|
gazeEnded |
User looked away from screen | duration, totalTime |
faceLandmarkerLoaded |
Face detection system ready | - |
webcamStarted |
Camera stream initiated | - |
webcamStopped |
Camera stream stopped | - |
timerReset |
Timer manually reset | - |
Timestamp,Type,Event,Duration_ms,Total_Time_ms,Details
"10:30:45 AM","Application","Looking Away Detected","1250.50","3847.25","Looking away detected"
"10:30:47 AM","Application","Webcam Started","","","Camera stream initiated"
Edit the threshold values in src/main.ts:
const targetCategories = [
{ name: "eyeLookDownRight", threshold: 0.3 },
{ name: "eyeLookInLeft", threshold: 0.4 },
{ name: "eyeLookOutRight", threshold: 0.4 },
];Change the embedded video by editing the iframe src in index.html:
<iframe
id="youtube-video"
src="https://www.youtube.com/embed/YOUR_VIDEO_ID?autoplay=1&mute=1&controls=1"
...>
</iframe>The WebSocket server runs on port 3000 by default. To change:
-
Update
server/server.js:const port = 3000; // Change this value
-
Update client connections in
src/main.tsandsrc/dashboard.ts:const ws = new WebSocket('ws://localhost:3000'); // Update port
distraction-detector/
βββ src/
β βββ main.ts # Main application logic
β βββ dashboard.ts # Dashboard functionality
β βββ vite-env.d.ts # TypeScript declarations
βββ server/
β βββ server.js # WebSocket server
β βββ package.json # Server dependencies
βββ public/ # Static assets
βββ index.html # Main application page
βββ dashboard.html # Dashboard interface
βββ package.json # Project configuration
βββ tsconfig.json # TypeScript configuration
βββ vite.config.ts # Vite configuration
npm run dev- Start development server with hot reloadnpm run build- Build for productionnpm run start- Start production servernpm test- Run tests (placeholder)
- Frontend: TypeScript, Vite, HTML5, CSS3
- Computer Vision: MediaPipe Tasks Vision
- Real-time Communication: WebSockets
- Backend: Node.js, Express.js
- Build Tool: Vite
- Package Manager: npm
This tool is designed for research in:
- Human-Computer Interaction: Understanding attention patterns during digital media consumption
- Cognitive Load Studies: Measuring distraction in different content types
- UX Research: Evaluating interface designs and content engagement
- Educational Research: Analyzing attention during e-learning sessions
- Accessibility Studies: Understanding attention patterns in different user groups
- Distraction Frequency: Number of times user looks away
- Distraction Duration: Individual and cumulative time spent looking away
- Attention Patterns: Temporal analysis of engagement
- Session Analytics: Complete session data with timestamps
- Local Processing: All face detection happens locally in the browser
- No Data Storage: Facial data is not stored or transmitted
- Consent Required: Always obtain proper consent before data collection
- Anonymization: Consider anonymizing exported data for research
- GDPR Compliance: Ensure compliance with relevant data protection regulations
-
Webcam Not Working
- Ensure browser has camera permissions
- Check if camera is being used by another application
- Try refreshing the page
-
Face Detection Not Loading
- Check internet connection (MediaPipe models are loaded from CDN)
- Verify browser compatibility
- Check browser console for errors
-
WebSocket Connection Failed
- Ensure server is running (
npm run dev) - Check if port 3000 is available
- Verify firewall settings
- Ensure server is running (
-
Dashboard Not Updating
- Refresh dashboard page
- Check WebSocket connection status
- Verify both main app and dashboard are connected
- Recommended: Chrome 90+, Firefox 88+, Safari 14+
- Required Features: WebRTC, WebSockets, ES6 modules
- Not Supported: Internet Explorer
This project is licensed under the MIT License. See the LICENSE file for details.
- Fork the repository
- Create a feature branch (
git checkout -b feature/new-feature) - Commit changes (
git commit -am 'Add new feature') - Push to branch (
git push origin feature/new-feature) - Create a Pull Request
Author: Samuele Mazzei
Email: samuele.mazzei@studenti.unitn.it
GitHub: @Samu01Tech
- MediaPipe for face landmark detection
- Vite for the build system
- WebSocket API for real-time communication
Note: This tool is intended for research and educational purposes. Always ensure proper consent and ethical guidelines are followed when collecting user data.