An interactive web-based focus training game powered by real-time webcam analysis.
The system uses MediaPipe face landmarks to estimate focus level (0-3) and dynamically adjusts game difficulty. It also displays an explainable AI strategy panel and generates an end-of-session summary.
- Real-time webcam stream with face landmark overlay (MJPEG in browser)
- Focus level estimation on a unified
0-3scale - Adaptive game difficulty (speed + obstacle height) based on focus trends
- Explainable AI strategy report (short/mid/long window statistics)
- Live focus curve chart (last 2 minutes)
- Session duration control and restart support
- Mercy mechanism for long unfocused periods with end-of-game summary
- Python + Flask
- OpenCV
- MediaPipe
- NumPy
- Vanilla JavaScript + HTML Canvas
web_app.py: Flask server, camera loop, focus state APIai_strategy.py: adaptive strategy logic and game summary generationtemplates/index.html: main web UIstatic/js/app.js: game logic, chart drawing, API pollingstatic/css/style.css: page stylingrule_model/models/face_landmarker.task: required MediaPipe model file
- Python 3.9+ (recommended)
- A working webcam
- Model file at:
rule_model/models/face_landmarker.task
Install dependencies:
pip install -r requirements.txtFrom project root:
python web_app.pyThen open:
You can tune performance and quality:
python web_app.py --host 127.0.0.1 --port 5000 --cam-id 0 --capture-width 960 --stream-width 720 --jpeg-quality 75Common options:
--model: model path (.task)--cam-id: camera index--capture-width: input width before inference--detect-width: width for detection workload--stream-width: MJPEG output width--jpeg-quality: stream quality (40-95)--infer-interval: minimum interval between inferences (seconds)
GET /: main pageGET /video_feed: webcam MJPEG streamGET /api/state: current focus state + AI strategy/reportPOST /api/game_summary: generate session summary
Space: micro-adjust the eagle (manual upward impulse)R: restart the game- Set target session minutes and click Apply & Restart
- If the model fails to load, the page will show an error preview.
- If no face is detected, focus is treated as
0. - For production/public repos, do not hardcode API keys in source code.
No license file is currently provided. Add one (for example, MIT) if you plan to distribute this project publicly.