Skip to content

PathakAman66/ai-fitness

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

127 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

AI Fitness Trainer with Real-Time Pose Estimation

License: MIT Python 3.8+ OpenCV MediaPipe Streamlit FastAPI

An intelligent computer vision application that leverages MediaPipe and OpenCV to analyze human exercise form in real-time. This system provides live feedback, repetition counting, form correction, and workout session tracking, making fitness training more accessible and data-driven.

Designed with modularity and extensibility in mind, this project serves as a comprehensive resource for developers, students, and researchers interested in the intersection of AI, computer vision, and fitness technology.

πŸš€ Key Features

  • Real-time Pose Detection: Utilizes MediaPipe to track 33 body landmarks with high precision.
  • Exercise Form Analysis: Calculates joint angles to validate posture and movement quality.
  • Automatic Repetition Counting: Logic-driven counting for dynamic exercises based on flexion/extension states.
  • Time-based Tracking: Duration monitoring for static isometric exercises (e.g., Plank).
  • Calorie Estimation: Metrics based on exercise intensity and duration.
  • Data Persistence: Workout sessions are automatically serialized and stored in JSON format for historical analysis.
  • Dual Interfaces: Includes both a lightweight Desktop OpenCV interface and a web-based Streamlit dashboard.
  • Resource Efficient: Optimized for CPU performance; no dedicated GPU is required.
  • RESTful API: FastAPI-based backend for integration with web/mobile apps.
  • Extensible Architecture: Modular design for easy addition of new exercises and features.

Supported Exercises

The system currently supports analysis for the following exercises, each with dedicated form validation logic:

  • Bicep Curls: Analysis of elbow flexion and extension.
  • Squats: Tracking of hip and knee flexion depth.
  • Push-ups: Measurement of chest depth and body alignment.
  • Shoulder Press: Analysis of vertical arm movement and symmetry.
  • Lunges: Validation of lower body stability and knee angles.
  • Plank: Monitoring of core stability and spinal alignment (Time-based).

πŸ—οΈ Architecture

graph TD
%% Services
svcazurecontainerapps_backend["`Name: backend
Path: backend
Language: python
Port: 8000`"]
svcazurecontainerapps_frontend["`Name: frontend
Path: frontend
Language: python
Port: 8501`"]
subgraph "Compute Resources"
%% Resources
subgraph containerappenv["Azure Container Apps (ACA) Environment"]
azurecontainerapps_backend("`backend (Azure Container App)`")
azurecontainerapps_frontend("`frontend (Azure Container App)`")
end
containerappenv:::cluster
end
subgraph "Dependency Resources"
%% Dependency Resources
azurestorageaccount_storage["`storage (Azure Storage Account)`"]
end
%% Relationships
svcazurecontainerapps_backend --> |"hosted on"| azurecontainerapps_backend
azurecontainerapps_backend -.-> |"http"| azurecontainerapps_frontend
azurecontainerapps_backend -.-> |"secret"| azurestorageaccount_storage
svcazurecontainerapps_frontend --> |"hosted on"| azurecontainerapps_frontend
azurecontainerapps_frontend -.-> |"http"| azurecontainerapps_backend
Loading

System Overview

  1. Pose Detection: Captures the live video feed and maps 33 distinct landmarks on the human body.
  2. Movement Analysis: Computes the geometric angles between specific joints to determine body state.
  3. Form Validation: Compares calculated angles against biomechanical thresholds to provide feedback (e.g., "Keep your back straight").
  4. Progress Tracking: Manages state machines for repetition counting and calculates caloric burn.
  5. Data Persistence: Aggregates session statistics and saves them for long-term progress tracking.

🎯 Target Audience

  • Developers: Those exploring real-time computer vision applications.
  • Students: Individuals studying AI, kinematics, or software engineering.
  • Contributors: Open-source enthusiasts looking for a modular codebase to extend.
  • Fitness Enthusiasts: Users seeking objective analysis of their workout form.

πŸ“Έ Screenshots

Desktop Application

Desktop Interface

Web Dashboard

Streamlit Dashboard

πŸ› οΈ Quick Start

Prerequisites

  • Python: Version 3.8 or higher
  • Hardware: Standard 720p webcam
  • OS: Windows, Linux, or macOS

Installation

  1. Clone the Repository
git clone https://github.com/PathakAman66/ai-fitness-trainer.git
cd ai-fitness-trainer
  1. Install Dependencies
# Recommended installation
pip install -r config/requirements.txt

# Manual installation
pip install mediapipe opencv-python numpy streamlit matplotlib fastapi uvicorn
  1. Verify Setup Run the simple test script to ensure the camera and libraries are functioning correctly.
python backend/tests/dependencies_test.py

πŸ“– Usage Guide

Option A: Desktop Application

This version runs locally using OpenCV windows and offers the best performance.

# Launch the desktop application (6 exercises + analytics)
python backend/core/app.py

Option B: FastAPI REST API

Run the backend API server for web/mobile applications.

# Start the API server
python -m backend.api.main

# Or using uvicorn directly
uvicorn backend.api.main:app --reload --host 0.0.0.0 --port 8000

Access the API documentation at: http://localhost:8000/docs

Option C: Web Interface (Streamlit)

A modern, multi-page browser-based dashboard powered by Streamlit.

# Run Streamlit interface
streamlit run frontend/streamlit_interface/app.py

Access the interface at: http://localhost:8501

Features:

  • Multi-page navigation (Home, Workout, History, Stats)
  • Real-time workout sessions with quality feedback
  • Workout history with filtering and statistics
  • Responsive design with dark mode support

Endpoints

  • GET / - Health check
  • POST /analyze-pose - Analyze pose from image data
  • GET /exercises - List supported exercises
  • POST /start-session - Start a workout session
  • POST /end-session - End a workout session
  • GET /session/{session_id} - Get session details

For detailed API documentation, visit http://localhost:8000/docs when the server is running.

πŸ”§ Technical Details

Exercise Detection Logic

Exercise Detection Method Key Measurements
Bicep Curl Elbow Angle Shoulder β†’ Elbow β†’ Wrist angle
Squat Hip/Knee Flexion Hip β†’ Knee β†’ Ankle angle
Push-up Chest Depth Shoulder β†’ Elbow angle + Spine alignment
Shoulder Press Vertical Motion Wrist β†’ Elbow β†’ Shoulder trajectory
Lunge Leg Coordination Bilateral knee and hip angles
Plank Body Alignment Shoulder β†’ Hip β†’ Ankle linearity

Project Structure

ai-fitness-trainer/
β”‚
β”œβ”€β”€ backend/                   # Backend Components
β”‚   β”œβ”€β”€ api/                   # FastAPI REST API
β”‚   β”‚   β”œβ”€β”€ main.py           # API endpoints and application
β”‚   β”‚   β”œβ”€β”€ models.py         # Pydantic data models
β”‚   β”‚   β”œβ”€β”€ pose_detector.py  # Pose detection service
β”‚   β”‚   β”œβ”€β”€ exercise_analyzer.py  # Exercise analysis service
β”‚   β”‚   β”œβ”€β”€ pose_quality_evaluator.py  # Form quality scoring
β”‚   β”‚   β”œβ”€β”€ workout_session.py    # Session management
β”‚   β”‚   └── session_manager.py    # Session state manager
β”‚   β”‚
β”‚   β”œβ”€β”€ core/                  # Standalone Desktop App
β”‚   β”‚   └── app.py            # Desktop application with CV2 GUI
β”‚   β”‚
β”‚   β”œβ”€β”€ utils/                 # Shared utilities
β”‚   β”‚   └── image_processor.py # Image encoding/decoding
β”‚   β”‚
β”‚   β”œβ”€β”€ data/                  # Data storage
β”‚   β”‚   └── reports/          # JSON workout reports
β”‚   β”‚
β”‚   └── tests/                 # Test suite
β”‚       └── test_*.py         # Unit and integration tests
β”‚
β”œβ”€β”€ frontend/                  # User Interfaces
β”‚   β”œβ”€β”€ streamlit_interface/   # Modern Streamlit web app
β”‚   β”‚   β”œβ”€β”€ app.py            # Main application entry
β”‚   β”‚   β”œβ”€β”€ pages/            # Multi-page navigation
β”‚   β”‚   β”‚   β”œβ”€β”€ 1_Home.py    # Welcome page
β”‚   β”‚   β”‚   β”œβ”€β”€ 2_Workout.py # Live workout session
β”‚   β”‚   β”‚   β”œβ”€β”€ 3_History.py # Workout history
β”‚   β”‚   β”‚   └── 4_Stats.py   # Analytics dashboard
β”‚   β”‚   β”œβ”€β”€ components/       # Reusable UI components
β”‚   β”‚   β”œβ”€β”€ services/         # Business logic layer
β”‚   β”‚   β”œβ”€β”€ styles/           # Theming and CSS
β”‚   β”‚   β”œβ”€β”€ utils/            # Helper functions
β”‚   β”‚   └── tests/            # Frontend tests
β”‚   β”‚
β”‚   β”œβ”€β”€ web-interface/         # HTML/CSS landing pages
β”‚   └── assets/                # Images and static resources
β”‚
β”œβ”€β”€ config/                    # Configuration
β”‚   └── requirements.txt       # Python dependencies
β”‚
└── docs/                      # Documentation
    β”œβ”€β”€ architecture.md        # Detailed architecture docs
    β”œβ”€β”€ CONTRIBUTING.md        # Contribution guidelines
    └── CODE_OF_CONDUCT.md     # Code of conduct

πŸ§ͺ Testing

Run the test suite to ensure everything is working correctly:

# Run all tests
python -m pytest backend/tests/

# Run specific test
python backend/tests/test_pose_detector.py

πŸš€ Deployment

Docker

Build and run with Docker:

# Build the image
docker build -t ai-fitness-trainer .

# Run the container
docker run -p 8000:8000 -p 8501:8501 ai-fitness-trainer

Azure Deployment

The application can be deployed to Azure using Azure Container Apps for both backend and frontend services, with Azure Storage for data persistence.

🀝 Contributing

Contributions are welcome! Please see our Contributing Guide for detailed guidelines.

We are specifically looking for:

  • New Exercise Modules: Logic for additional exercises.
  • Algorithm Optimization: Improving the accuracy of pose detection.
  • UI/UX Improvements: Enhancements to the Streamlit dashboard.
  • Analytics: Advanced data visualization for workout history.
  • Mobile App: React Native or Flutter integration.

Development Workflow

  1. Fork the repository.
  2. Create a feature branch (git checkout -b feature/NewFeature).
  3. Commit changes and ensure tests pass.
  4. Submit a Pull Request.

πŸ“„ License

This project is open-source and licensed under the MIT License. See the LICENSE file for details.

πŸ™ Acknowledgments

πŸ“ž Support

If you have any questions or need help, please:

  • Open an issue on GitHub
  • Check the documentation
  • Join our community discussions

Made with ❀️ for the fitness and AI community

About

No description, website, or topics provided.

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors