Real-time exercise form analysis using Intel RealSense D435 + MediaPipe + Next.js + FastAPI.
Browser (Next.js)
↕ REST + WebSocket
FastAPI Backend (Python)
↕ PostgreSQL
↑
Local Camera Bridge (Python, runs on user's machine)
← Intel D435 USB → MediaPipe → angles → WS → Backend → Browser
mirage/
├── frontend/ Next.js 14 app (TypeScript + Tailwind)
│ └── src/
│ ├── app/ Pages: /, /auth, /app, /app/session/[key], /app/progress, /app/learn
│ ├── components/ Sidebar, TopBar, BodyAreaCard, FormScoreRing, FeedbackPanel, …
│ ├── hooks/ useSessionWS — live WebSocket data
│ └── lib/ store.ts (Zustand), api.ts (axios), exercises.ts (catalogue)
│
├── backend/ FastAPI (Python 3.11)
│ ├── main.py
│ ├── core/ database.py, auth.py (JWT)
│ ├── models/ user.py, session.py (Rep + ExerciseSession)
│ └── routers/ auth, exercises, sessions (+ WebSocket), progress
│
└── bridge/ Runs locally on user's PC next to the D435
└── camera_bridge.py
# Option A — Docker (recommended)
docker-compose up -d postgres
cd backend
pip install -r requirements.txt
uvicorn main:app --reload --port 8000
# Option B — full Docker
docker-compose upcd frontend
npm install
npm run dev # → http://localhost:3000cd bridge
pip install -r requirements.txt
# 1. Get a JWT by logging in
TOKEN=$(curl -s -X POST http://localhost:8000/api/auth/login \
-H "Content-Type: application/json" \
-d '{"email":"you@example.com","password":"yourpass"}' | python3 -c "import sys,json; print(json.load(sys.stdin)['access_token'])")
# 2. Start a session via the UI — copy the session UUID from the URL, then:
python camera_bridge.py \
--session_id <UUID-from-UI> \
--exercise mini_squat \
--token $TOKEN \
--show_cv # optional: opens OpenCV preview window| Key | Name | Area | Category |
|---|---|---|---|
upper_trapezius_stretch |
Upper Trapezius Stretch | Neck | Cervical rehab |
shoulder_circle |
Shoulder Circle | Shoulder | Mobility |
pendulum_shoulder |
Pendulum Shoulder | Shoulder | Post-surgery |
mini_squat |
Mini Squat | Hip | OA / quad strength |
sit_to_stand |
Sit to Stand | Hip | Neurological rehab |
Bridge → Backend → Browser (frame_data):
{
"type": "frame_data",
"angles": { "left_knee": 142.3, "trunk_lean": 88.1 },
"form_score": 82.0,
"feedback": ["Shift weight slightly forward"],
"rep_count": 8,
"timestamp": 1718000000.0
}Bridge → Backend → Browser (rep_data) — fires on each completed rep:
{
"type": "rep_data",
"rep_number": 8,
"angles": { "left_knee": 142.3 },
"form_score": 86.0,
"feedback": ["Knee angle 142° — great depth ✓"],
"rep_count": 8
}- XP per rep: 10 base + 5 bonus (form ≥ 80%) + 10 bonus (form ≥ 95%)
- Level thresholds: Level n requires
n × (n-1) × 100total XP - Streak: tracked server-side via
last_activefield on User - Achievements (extend in
backend/services/achievements.py):- First rep, 7-day streak, 100 reps milestone, 90%+ form session
Backend .env:
DATABASE_URL=postgresql+asyncpg://postgres:password@localhost:5432/mirage
SECRET_KEY=your-secret-key-here
Frontend .env.local:
NEXT_PUBLIC_API_URL=http://localhost:8000
Your 50 DB3 files → training pipeline:
# 1. Extract frames + run MediaPipe on each recording
python bridge/extract_training_data.py \
--input_dir /path/to/db3_files \
--exercise mini_squat \
--output training_data/mini_squat.json
# 2. Label reps (run the labelling UI or use rule-based auto-labelling)
# 3. Train LSTM classifier per exercise
# 4. Export model → backend/models/ml/mini_squat.onnx
# 5. Load in camera_bridge.py replacing the rule-based scorer| Layer | Technology |
|---|---|
| Frontend | Next.js 14, React 18, Tailwind, Zustand, Framer Motion, Recharts |
| Backend | FastAPI, SQLAlchemy (async), PostgreSQL |
| Auth | JWT (python-jose), bcrypt |
| Real-time | WebSockets (FastAPI native) |
| CV | Intel RealSense SDK, MediaPipe Pose |
| Deployment | Docker, any VPS (backend) + Vercel (frontend) |