Skip to content

About

Mirage — depth-sensing physiotherapy coach: Intel RealSense D435 + MediaPipe pose tracking streamed over WebSocket into a Next.js 14 app with a FastAPI + PostgreSQL backend. Live form scoring, rep counting and corrective feedback.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mirage — Depth-sensing Physiotherapy Coach

Real-time exercise form analysis using Intel RealSense D435 + MediaPipe + Next.js + FastAPI.


Architecture

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

Project structure

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

Quick start

1. Start the database + backend

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

2. Start the frontend

cd frontend
npm install
npm run dev           # → http://localhost:3000

3. Start the camera bridge (on machine with D435)

cd 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

Exercises

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

WebSocket message format

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
}

Gamification

  • XP per rep: 10 base + 5 bonus (form ≥ 80%) + 10 bonus (form ≥ 95%)
  • Level thresholds: Level n requires n × (n-1) × 100 total XP
  • Streak: tracked server-side via last_active field on User
  • Achievements (extend in backend/services/achievements.py):
    • First rep, 7-day streak, 100 reps milestone, 90%+ form session

Environment variables

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

Adding your DB3 recordings for model training

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

Tech stack

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)

About

Mirage — depth-sensing physiotherapy coach: Intel RealSense D435 + MediaPipe pose tracking streamed over WebSocket into a Next.js 14 app with a FastAPI + PostgreSQL backend. Live form scoring, rep counting and corrective feedback.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages