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.
- 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.
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).
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
- Pose Detection: Captures the live video feed and maps 33 distinct landmarks on the human body.
- Movement Analysis: Computes the geometric angles between specific joints to determine body state.
- Form Validation: Compares calculated angles against biomechanical thresholds to provide feedback (e.g., "Keep your back straight").
- Progress Tracking: Manages state machines for repetition counting and calculates caloric burn.
- Data Persistence: Aggregates session statistics and saves them for long-term progress tracking.
- 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.
- Python: Version 3.8 or higher
- Hardware: Standard 720p webcam
- OS: Windows, Linux, or macOS
- Clone the Repository
git clone https://github.com/PathakAman66/ai-fitness-trainer.git
cd ai-fitness-trainer- Install Dependencies
# Recommended installation
pip install -r config/requirements.txt
# Manual installation
pip install mediapipe opencv-python numpy streamlit matplotlib fastapi uvicorn- Verify Setup Run the simple test script to ensure the camera and libraries are functioning correctly.
python backend/tests/dependencies_test.pyThis version runs locally using OpenCV windows and offers the best performance.
# Launch the desktop application (6 exercises + analytics)
python backend/core/app.pyRun 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 8000Access the API documentation at: http://localhost:8000/docs
A modern, multi-page browser-based dashboard powered by Streamlit.
# Run Streamlit interface
streamlit run frontend/streamlit_interface/app.pyAccess 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
GET /- Health checkPOST /analyze-pose- Analyze pose from image dataGET /exercises- List supported exercisesPOST /start-session- Start a workout sessionPOST /end-session- End a workout sessionGET /session/{session_id}- Get session details
For detailed API documentation, visit http://localhost:8000/docs when the server is running.
| 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 |
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
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.pyBuild 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-trainerThe application can be deployed to Azure using Azure Container Apps for both backend and frontend services, with Azure Storage for data persistence.
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.
- Fork the repository.
- Create a feature branch (
git checkout -b feature/NewFeature). - Commit changes and ensure tests pass.
- Submit a Pull Request.
This project is open-source and licensed under the MIT License. See the LICENSE file for details.
- MediaPipe for pose estimation
- OpenCV for computer vision
- Streamlit for web interface
- FastAPI for API framework
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

