Skip to content

cartibhati/AlignMate-Fullstack

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

6 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿง  AlignMate โ€” AI-Powered Real-Time Posture Coach

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.


โœจ Features

  • 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

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”        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:

  1. Frontend captures webcam frames and streams them over WebSocket
  2. Backend runs MediaPipe to extract 33 pose landmarks (99 features: x, y, z per landmark)
  3. Random Forest model predicts posture class + confidence probability
  4. Rule-based checks (neck tilt, shoulder imbalance) layer on top of ML output
  5. JSON response streams back โ€” score, status, angles, and corrective feedback

๐Ÿ“ Project Structure

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

Backend (AlignMate/)

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)

Frontend (alignmate-frontend/)

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

๐Ÿค– Machine Learning Pipeline

Data Collection

python collect_data.py

Controls: 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.

Feature Extraction

MediaPipe detects 33 body landmarks, each with (x, y, z) coordinates:

33 landmarks ร— 3 values = 99 features per frame

Model Training

python train_model.py
  • RandomForestClassifier โ€” 100 estimators
  • 80/20 train/test split
  • Outputs accuracy report + saves posture_model_v3.pkl

Inference

Webcam frame โ†’ MediaPipe โ†’ 99 features โ†’ predict_proba() โ†’ posture score + class
                                                          โ†“
                                              Rule-based checks (neck/shoulder)
                                                          โ†“
                                              Final feedback JSON over WebSocket

๐Ÿš€ Setup & Installation

Prerequisites

  • Python 3.10+
  • Node.js 18+
  • Webcam

1. Backend Setup

cd AlignMate
pip install -r requirements.txt
uvicorn server:app --reload

Runs at: http://localhost:8000
WebSocket: ws://localhost:8000/ws

2. Frontend Setup

cd alignmate-frontend
npm install
npm run dev

Runs at: http://localhost:5173


๐Ÿ”Œ WebSocket Data Format

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

๐Ÿ› ๏ธ Tech Stack

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

โš ๏ธ Known Limitations

  • 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

๐Ÿ”ฎ Possible Future Improvements

  • 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

๐Ÿ“„ License

MIT โ€” do whatever you want with it.

About

AI-powered real-time posture detection system using computer vision and machine learning (FastAPI + React)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors