A fullstack posture correction system that uses your webcam + machine learning to monitor your posture in real time, detect misalignment, and guide you through recovery exercises โ all running locally on your machine.
- Real-time posture detection โ classifies posture every frame using a trained Random Forest model
- Live scoring โ posture score (0โ100) with drift detection and bad posture duration tracking
- Rule-based + ML hybrid โ neck tilt, shoulder imbalance, and probability smoothing work together
- Recovery exercises โ shoulder roll tracking with rep counting via motion verification
- WebSocket streaming โ zero-latency feedback from backend to frontend
- Auth + themes โ protected routes, login system, and light/dark mode
โโโโโโโโโโโโโโโโโโโโโโโ WebSocket (ws://localhost:8000/ws) โโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ React Frontend โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโบ โ FastAPI Backend โ
โ (Vite + Tailwind) โ โ server.py โ
โ React Webcam โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ โ
โโโโโโโโโโโโโโโโโโโโโโโ JSON (score, status, feedback) โโโโโโโโโโโโโโฌโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโโโ
โ ML Pipeline โ
โ MediaPipe (landmarks) โ
โ Random Forest (sklearn) โ
โ posture_model_v3.pkl โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Flow:
- Frontend captures webcam frames and streams them over WebSocket
- Backend runs MediaPipe to extract 33 pose landmarks (99 features: x, y, z per landmark)
- Random Forest model predicts posture class + confidence probability
- Rule-based checks (neck tilt, shoulder imbalance) layer on top of ML output
- JSON response streams back โ score, status, angles, and corrective feedback
AlignMate_NBA/
โ
โโโ AlignMate/ # Python backend
โ โโโ posture/ # Core posture analysis logic
โ โ โโโ exercise_verifier.py # Rep counting + motion verification for exercises
โ โ โโโ exercises.py # Exercise definitions (shoulder rolls, etc.)
โ โ โโโ geometry.py # Angle calculations from landmarks
โ โ โโโ posture_rules.py # Rule-based checks (neck tilt, shoulder imbalance)
โ โโโ utils/ # Shared utilities
โ โโโ vision/ # MediaPipe integration + landmark extraction
โ โโโ collect_data.py # Data collection script for training
โ โโโ train_model.py # Model training script
โ โโโ main.py # Entry point
โ โโโ server.py # FastAPI + WebSocket server
โ โโโ posture_model_v3.pkl # Trained Random Forest model
โ โโโ data.csv # Collected training data
โ โโโ requirements.txt
โ
โโโ alignmate-frontend/ # React frontend
โโโ src/
โ โโโ components/
โ โ โโโ auth/ # Login, signup, protected routes
โ โ โโโ camera/ # Webcam capture + live overlay
โ โ โโโ common/ # Navbar, ConnectionStatus, shared UI
โ โโโ assets/
โ โโโ public/
โโโ package.json
| File/Folder | What it does |
|---|---|
server.py |
FastAPI app with a WebSocket endpoint /ws โ receives frames, runs the full pipeline, streams JSON back |
vision/ |
Wraps MediaPipe Pose โ extracts 33 landmarks and converts them to a flat 99-feature vector |
posture/posture_rules.py |
Rule-based detector โ checks neck angle, shoulder tilt, and flags specific misalignments |
posture/geometry.py |
Pure math โ calculates angles between joints from (x, y, z) coordinates |
posture/exercises.py |
Defines recovery exercises with movement targets |
posture/exercise_verifier.py |
Tracks reps by verifying full motion arcs (not just position) |
train_model.py |
Loads data.csv, trains a RandomForestClassifier (100 trees, 80/20 split), saves as .pkl |
collect_data.py |
Webcam-based data collection โ press c (correct), i (incorrect), q (quit) |
| File/Folder | What it does |
|---|---|
components/camera/ |
Captures webcam feed, opens WebSocket connection, renders live posture overlay and score |
components/auth/ |
Login/signup forms, JWT handling, protected route wrappers |
components/common/ |
Navbar, ConnectionStatus indicator, reusable UI components |
python collect_data.pyControls: c โ correct posture ย |ย i โ incorrect posture ย |ย q โ quit
Saves labeled rows to data.csv. Collect at least 200โ300 samples per class for reliable results.
MediaPipe detects 33 body landmarks, each with (x, y, z) coordinates:
33 landmarks ร 3 values = 99 features per frame
python train_model.py- RandomForestClassifier โ 100 estimators
- 80/20 train/test split
- Outputs accuracy report + saves
posture_model_v3.pkl
Webcam frame โ MediaPipe โ 99 features โ predict_proba() โ posture score + class
โ
Rule-based checks (neck/shoulder)
โ
Final feedback JSON over WebSocket
- Python 3.10+
- Node.js 18+
- Webcam
cd AlignMate
pip install -r requirements.txt
uvicorn server:app --reloadRuns at: http://localhost:8000
WebSocket: ws://localhost:8000/ws
cd alignmate-frontend
npm install
npm run devRuns at: http://localhost:5173
The backend streams this JSON to the frontend on every frame:
{
"score": 87,
"status": "good",
"angles": {
"neck": 12.4,
"shoulder": 3.1
},
"feedback": "Great posture! Keep it up."
}| Field | Values |
|---|---|
status |
"good" / "drift" / "bad" |
score |
0โ100 |
angles.neck |
degrees of forward tilt |
angles.shoulder |
degrees of imbalance |
| Layer | Technology |
|---|---|
| Frontend | React (Vite), Tailwind CSS, Framer Motion |
| Webcam | React Webcam |
| Routing & Auth | React Router, protected routes |
| Backend | FastAPI, WebSockets |
| Pose Detection | MediaPipe |
| Image Processing | OpenCV |
| ML Model | Scikit-learn (Random Forest) |
| Data | NumPy, Pandas, Joblib |
- Requires good lighting for accurate MediaPipe landmark detection
- Model accuracy depends on training data quality โ more diverse samples = better results
- Currently classifies only two posture states (correct / incorrect); no nuanced multi-class support yet
- No posture history persistence โ data resets on server restart
- Posture history dashboard with session analytics
- LSTM/CNN model for temporal posture patterns
- Personalized baselines per user (calibration mode)
- Mobile app with on-device ML (MediaPipe + TFLite)
- Email/notification alerts after prolonged bad posture
MIT โ do whatever you want with it.