A real-time computer vision application that watches your study habits so you can build better ones. Detects drowsiness, phone usage, posture problems, gaze direction, and more β all through your webcam.
Study-Focus is a Flask-based web application that uses your webcam and a combination of YOLOv8, MediaPipe FaceMesh, and MediaPipe Pose to monitor you while you study. It logs distraction events, tracks your focus sessions, generates analytics, and exports reports β all from your browser.
This project is locally run β no cloud, no signup, no data leaves your machine.
| Detection Type | Technology | How It Works |
|---|---|---|
| Eye Closure / Drowsiness | MediaPipe FaceMesh + EAR | Calculates Eye Aspect Ratio (EAR); alerts if eyes closed > threshold (default 3s) |
| Phone Detection | YOLOv8 (COCO) | Detects cell phone class; requires β₯4 consecutive frames + min bbox area to reduce false positives |
| Multiple People | YOLOv8 (COCO) | Detects person class count > 1; useful for exam/study room monitoring |
| Gaze / Looking Away | MediaPipe FaceMesh Iris | Tracks iris position ratio within eye socket; warns immediately, logs distraction after 10s |
| Posture Detection | MediaPipe Pose | Classifies posture as straight, leaning, or slouching using shoulder tilt + nose-shoulder ratio |
- Real-time stats: Focus time, distraction time, and goal progress (refreshes every 5 seconds)
- Interactive Charts (Chart.js):
- Focus vs Distraction doughnut chart
- Event breakdown by type (eye_closed, phone_detected, multiple_people, looking_away, posture_bad)
- User comparison leaderboard (bar chart)
- Recent Events Table: Last 10 events with type, timestamp, and duration
- Daily Goal Progress: Customizable target (default: 120 mins) with visual progress bar
- Real-time annotated webcam stream at
/video_feed - Bounding boxes drawn on detected phones and people
- On-frame text labels for eye closure, gaze direction, and posture state
- Camera feed is mirrored (flipped horizontally) for natural display
- Username-based login β no password required
- Auto-creates new user profile if username doesn't exist
- Per-user settings: daily goal & eye closure threshold
- Data reset option to clear all sessions and events
| Parameter | Default | Description |
|---|---|---|
PHONE_CONF_THRESHOLD |
0.65 |
Minimum YOLO confidence to count as phone |
MIN_PHONE_FRAMES |
4 |
Consecutive frames needed to confirm phone |
MIN_PHONE_AREA |
1500 |
Minimum bounding box area (pixelsΒ²) |
EAR_DYNAMIC_MULTIPLIER |
0.7 |
Fraction of baseline EAR below which eyes = closed |
BASELINE_FRAMES |
50 |
Frames used to calibrate personal EAR baseline |
GAZE_DISTRACTION_THRESHOLD |
10.0s |
Seconds looking away before logging as distraction |
POSTURE_SLOUCH_PERSIST |
5.0s |
Seconds of slouching before logging as event |
- CSV β Session-level data (focus time, distraction time per session)
- PDF β Summary report with session details via ReportLab
| Layer | Technology |
|---|---|
| Backend | Python 3.10, Flask 2.3 |
| Database | SQLite (via Flask-SQLAlchemy) |
| Computer Vision | OpenCV, MediaPipe (FaceMesh + Pose), Ultralytics YOLOv8 |
| Frontend | HTML5, CSS3 (custom), Vanilla JavaScript |
| Charts | Chart.js |
| ML Models | yolov8n.pt (default), yolov8s.pt (optional, more accurate) |
| Export | Pandas (CSV), ReportLab (PDF) |
| Containerization | Docker + Docker Compose |
Study-Focus/
βββ app.py # Main Flask app β all routes, detection logic, DB models
βββ requirements.txt # Python dependencies
βββ Dockerfile # Docker image definition (python:3.10-slim)
βββ docker-compose.yml # Local Docker Compose setup
βββ yolov8n.pt # YOLOv8 nano model (default, faster)
βββ yolov8s.pt # YOLOv8 small model (optional, more accurate)
βββ test_yolo.py # Quick YOLO model test script
βββ future_plan.txt # Feature roadmap & ideas
βββ instance/
β βββ focus_monitor.db # SQLite database (auto-created on first run)
βββ templates/
β βββ index.html # Main dashboard (single-page app)
βββ static/
βββ css/
β βββ style.css # Custom styles & dark/light theme
βββ js/
βββ app.js # Frontend logic (API calls, charts, alerts)
| Column | Type | Description |
|---|---|---|
id |
Integer (PK) | Auto-increment |
username |
String(80) | Unique username |
created_at |
DateTime | Account creation time |
daily_goal_minutes |
Integer | Personal focus goal (default: 120) |
eye_closure_threshold |
Float | Seconds before eye-close alert (default: 3.0) |
| Column | Type | Description |
|---|---|---|
id |
Integer (PK) | Auto-increment |
user_id |
FK β user | Owner of session |
start_time / end_time |
DateTime | Session window |
total_duration |
Integer | Seconds |
focus_duration |
Integer | Seconds (total β distraction) |
distraction_duration |
Integer | Sum of all event durations in seconds |
is_active |
Boolean | True while monitoring is running |
| Column | Type | Description |
|---|---|---|
id |
Integer (PK) | Auto-increment |
user_id |
FK β user | Owner |
session_id |
FK β session | Parent session |
timestamp |
DateTime | When event occurred |
event_type |
String(50) | eye_closed / phone_detected / multiple_people / looking_away / posture_bad |
duration |
Float | Duration in seconds |
| Method | Endpoint | Description |
|---|---|---|
GET |
/ |
Render main dashboard |
GET |
/video_feed |
MJPEG live annotated camera stream |
GET |
/api/status |
Current alert state (JSON) |
GET |
/api/users |
List all users |
POST |
/api/login |
Login / auto-create user |
POST |
/api/logout |
Logout and stop monitoring |
POST |
/api/start_monitoring |
Start webcam + detection thread |
POST |
/api/stop_monitoring |
Stop monitoring and save session |
GET |
/api/dashboard_data |
Today's stats, events, goal progress |
GET/POST |
/api/settings |
Get or update user settings |
POST |
/api/reset_user_data |
Delete all user sessions + events |
GET |
/api/export_data/<format> |
Export as csv or pdf |
GET |
/api/comparison_data |
All-user leaderboard data |
- Python 3.10
- Webcam
- pip
git clone https://github.com/theansh99999/Study-Focus.git
cd Study-Focuspython -m venv venv
# Windows
venv\Scripts\activate
# Mac/Linux
source venv/bin/activatepip install -r requirements.txtNote for Windows: Use
mediapipeinstead ofmediapipe-siliconinrequirements.txtif you're not on Apple Silicon.
python app.pyOpen your browser β http://localhost:5000
- Login β Enter your username (auto-creates profile if new)
- Grant Camera Permission β Browser will ask on first start
- Configure Settings (optional):
- Set your daily focus goal (minutes)
- Adjust eye closure alert threshold (seconds)
- Click "Start Monitoring" β Camera feed activates and detection begins
- Watch the Dashboard β Stats update live every 5 seconds
- Stop Monitoring β Session is saved and durations calculated
- Export Data β Download CSV or PDF report from dashboard
The app calibrates a personal baseline EAR over the first 50 frames, then uses baseline Γ 0.7 as the dynamic threshold. This adapts to each user's natural eye openness.
Uses MediaPipe's iris refinement landmarks (468β477). Iris horizontal position within eye socket is compared against configurable L/R thresholds (0.38). If gaze is off-center, a visual warning appears immediately; if sustained for >10s, it's logged as a looking_away distraction event.
Uses shoulder landmarks (11, 12) and nose (0) from MediaPipe Pose:
- Slouching: Nose Y close to or below shoulder midpoint Y, or shoulders very low in frame
- Leaning: High shoulder tilt ratio (one shoulder significantly higher than other)
- Straight: Normal posture
A 7-frame majority vote smooths out transient misclassifications.
| Problem | Solution |
|---|---|
| Camera not found | Make sure no other app is using the webcam; try refreshing |
| YOLO model not loading | Ensure yolov8n.pt is in the project root directory |
mediapipe-silicon install fails |
Replace with mediapipe in requirements.txt (Windows/Linux) |
| High CPU usage | YOLO runs at 640px; reduce imgsz in detect_objects_with_yolo() |
| False eye-closure alerts | Increase BASELINE_FRAMES or raise EAR_DYNAMIC_MULTIPLIER |
| Too many gaze alerts | Increase GAZE_LR_THRESHOLD or GAZE_DISTRACTION_THRESHOLD |
- Yawning / Drowsiness Detection β Mouth open detection via FaceMesh
- Hand Activity Tracking β Hand near face = phone probability
- Sound-based Distraction β Mic input for talking/noise detection
- Focus Score System β Weighted composite score (eye, phone, gaze, posture)
- Smart Alerts β Tiered alert system (10s = ignore, 30s = warn, 60s = strong)
- Break Recommendation β Notify after 40+ min continuous focus (Pomodoro-style)
- Tab / App Switching Detection β
visibilitychangeAPI integration - Peak Focus Hours Analytics β Identify your best study time windows
- User Authentication β Password-protected accounts
| Browser | Support |
|---|---|
| Chrome / Chromium | β Full (recommended) |
| Firefox | β Full |
| Edge | β Full |
| Safari (macOS) | β Full |
β οΈ Camera access over HTTP works onlocalhost. For any networked deployment, HTTPS is required (browser security policy).
Youtube link https://youtu.be/WuDIWtNUqpI?si=XWx9Vu74j8ddw-n4
Ansh Kumar Rai Connect me on Linkedin https://www.linkedin.com/in/anshkumarrai/
This project is for educational and personal use. Feel free to fork and build on it.