Skip to content

sarweshshah/gait_analysis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

71 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Gait Analysis System

A comprehensive markerless gait analysis system using computer vision and machine learning techniques for analyzing human walking patterns.

🎯 Overview

This system provides a complete pipeline for markerless gait analysis using:

  • Computer Vision: Real-time pose estimation with MediaPipe
  • Machine Learning: Temporal Convolutional Networks (TCN) for gait pattern analysis
  • Data Processing: Advanced preprocessing and feature extraction
  • Visualization: Real-time pose visualization with trail effects

✨ Key Features

  • πŸ”„ Unified Pose Estimation: Extensible architecture supporting multiple pose estimation backends
  • 🧠 TCN Architecture: Temporal sequence modeling for gait analysis
  • πŸ“Š Advanced Analytics: Gait event detection, phase analysis, and performance metrics
  • 🎨 Real-time Visualization: Interactive pose visualization with trail effects
  • πŸ”§ Modular Design: Easy to extend with new pose models and analysis methods
  • πŸ“ˆ Cross-validation: Robust evaluation pipeline with comprehensive metrics
  • πŸ“ Organized Outputs: All results centralized in outputs/ directory

πŸ—οΈ System Architecture

Extensible Pose Processing

The unified pose processor manager makes it easy to add new pose estimation models:

  1. Create a new processor class inheriting from PoseProcessor
  2. Implement required abstract methods
  3. Add the model to the AVAILABLE_MODELS dictionary
  4. Update the create_processor method

Project Structure

gait_analysis/
β”œβ”€β”€ core/                                       # Core system modules
β”‚   β”œβ”€β”€ utils/                                  # Utility modules
β”‚   β”‚   β”œβ”€β”€ constants.py                        # Core constants
β”‚   β”‚   β”œβ”€β”€ config.py                           # Configuration management
β”‚   β”‚   └── logging_config.py                   # Logging configuration
β”‚   β”œβ”€β”€ pose_processor_manager.py               # Unified pose processor manager
β”‚   β”œβ”€β”€ mediapipe_integration.py                # MediaPipe pose estimation
β”‚   β”œβ”€β”€ gait_data_preprocessing.py              # Data preprocessing and feature extraction
β”‚   β”œβ”€β”€ gait_training.py                        # Training and evaluation module
β”‚   └── tcn_gait_model.py                       # Temporal Convolutional Network model
β”œβ”€β”€ usecases/                                   # Use case implementations
β”‚   β”œβ”€β”€ gait_analysis/                          # Main gait analysis use case
β”‚   β”‚   β”œβ”€β”€ features/                           # Feature-specific implementations
β”‚   β”‚   β”‚   └── realtime_pose_visualization.py  # Real-time visualization
β”‚   β”‚   β”œβ”€β”€ utils.py                            # Utilities for quick analysis
β”‚   β”‚   └── main_gait_analysis.py               # Main pipeline orchestrator
β”‚   └── testing/                                # Testing and validation
β”‚       β”œβ”€β”€ test_pose_models.py                 # Pose model testing and comparison
β”‚       └── test_system.py                      # System testing and validation
β”œβ”€β”€ scripts/                                    # Utility scripts
β”‚   β”œβ”€β”€ pose_model_comparison.py                # Pose model comparison tool
β”‚   └── run_gait_analysis.py                    # Gait analysis runner
β”œβ”€β”€ configs/                                    # Configuration files
β”‚   β”œβ”€β”€ default.json                            # Default configuration
β”‚   └── gait_analysis.json                      # Configuration for pose models
β”œβ”€β”€ docs/                                       # Documentation
β”‚   β”œβ”€β”€ visualizations/                         # Generated visualizations
β”‚   β”œβ”€β”€ README_RealTime_Visualization.md        # Real-time visualization docs
β”‚   β”œβ”€β”€ README_TCN_Gait_Analysis.md             # TCN system documentation
β”‚   β”œβ”€β”€ README_Installation.md                  # Installation guide
β”‚   └── README_Changelog.md                     # Project changelog and history
β”œβ”€β”€ archive/                                    # Legacy scripts (see archive/README.md)
β”œβ”€β”€ data/                                       # Input data directory
β”‚   └── models/                                 # Trained models
β”œβ”€β”€ videos/                                     # Video files directory
β”‚   β”œβ”€β”€ raw/                                    # Raw video files
β”‚   └── sneak/                                  # Sneak gait videos
└── outputs/                                    # Output results directory
    β”œβ”€β”€ gait_analysis/                          # Gait analysis results
    β”œβ”€β”€ mediapipe/                              # MediaPipe outputs
    β”œβ”€β”€ test_results/                           # Test results
    β”œβ”€β”€ logs/                                   # Application logs
    └── models/                                 # Trained models
    └── visualizations/                         # Generated visualizations

πŸš€ Quick Start

1. Setup Environment

On macOS/Linux:

./setup_environment.sh

On Windows:

setup_environment.bat

2. Activate Virtual Environment

source .venv/bin/activate  # macOS/Linux
# or
.venv\Scripts\activate     # Windows

3. Test Installation

# Test the complete system
python3 usecases/testing/test_system.py

# Test pose models specifically
python3 usecases/testing/test_pose_models.py

# Show available models
python3 scripts/pose_model_comparison.py --info

4. Run Analysis

# Basic gait analysis with MediaPipe
python3 usecases/gait_analysis/main_gait_analysis.py \
    --videos videos/raw/sample.mp4 \
    --output outputs/gait_analysis/

# Pose detection only
python3 usecases/gait_analysis/main_gait_analysis.py \
    --videos videos/raw/sample.mp4 \
    --pose-detection-only

# With real-time visualization
python3 usecases/gait_analysis/main_gait_analysis.py \
    --videos videos/raw/sample.mp4 \
    --with-visualization

🎯 Pose Estimation Models

MediaPipe (Default)

  • Speed: Fast, real-time processing
  • Accuracy: Good for most applications
  • Resource Usage: Low, works on CPU
  • Best For: Real-time applications, mobile/edge devices

Adding New Models

The system is designed to easily support additional pose estimation models:

  1. Create a new processor class that inherits from PoseProcessor
  2. Implement the required abstract methods
  3. Add the model to the AVAILABLE_MODELS dictionary in PoseProcessorManager
  4. Update the create_processor method to handle the new model type

Model Comparison

# Compare available models on the same video
python3 scripts/pose_model_comparison.py --video videos/raw/sample.mp4 --compare

# Process with specific model
python3 usecases/gait_analysis/main_gait_analysis.py \
    --videos videos/raw/sample.mp4 \
    --pose-model mediapipe

🎨 Real-time Pose Visualization

The system includes an interactive real-time pose visualization tool that displays pose keypoints as colored dots with trail effects.

Quick Demo

# Basic visualization with trail effect
python3 usecases/gait_analysis/features/realtime_pose_visualization.py videos/raw/sample.mp4

# Show confidence values
python3 usecases/gait_analysis/features/realtime_pose_visualization.py videos/raw/sample.mp4 --show-confidence

# Fast performance mode
python3 usecases/gait_analysis/features/realtime_pose_visualization.py videos/raw/sample.mp4 --model-complexity 0 --no-trail

Interactive Controls

  • 'q': Quit visualization
  • 't': Toggle trail effect
  • 'c': Toggle connections
  • 'r': Reset trail
  • SPACE: Pause/resume
  • '1', '2', '3': Change model complexity

πŸ“Š Output Structure

All results are organized in the outputs/ directory:

outputs/
β”œβ”€β”€ gait_analysis/                      # Main gait analysis results
β”‚   β”œβ”€β”€ cv_metrics.json                 # Cross-validation metrics
β”‚   β”œβ”€β”€ fold_scores.json                # Per-fold performance
β”‚   β”œβ”€β”€ training_histories.json         # Training curves data
β”‚   β”œβ”€β”€ classification_report.txt       # Detailed classification report
β”‚   β”œβ”€β”€ confusion_matrix.png            # Confusion matrix visualization
β”‚   β”œβ”€β”€ training_curves.png             # Training curves plot
β”‚   └── detailed_results.json           # Complete results summary
β”œβ”€β”€ mediapipe/                          # MediaPipe pose detection outputs
β”œβ”€β”€ test_results/                       # Testing and validation results
β”œβ”€β”€ logs/                               # Application logs
β”œβ”€β”€ visualizations/                     # Charts, graphs, and visual outputs
└── models/                             # Trained models and artifacts

πŸ“š Documentation

πŸ”§ Configuration

The system uses JSON configuration files for customization:

{
  "pose_model": "mediapipe",
  "task_type": "phase_detection",
  "num_classes": 4,
  "num_filters": 64,
  "kernel_size": 3,
  "num_blocks": 4,
  "dropout_rate": 0.2,
  "learning_rate": 0.001,
  "epochs": 100,
  "batch_size": 32
}

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Submit a pull request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • MediaPipe team for the pose estimation framework
  • TensorFlow/Keras community for the deep learning framework
  • OpenCV community for computer vision tools

Note: Legacy scripts from the initial development phase have been moved to the archive/ directory. See archive/README.md for details about the archived files and migration notes.

About

Gait analysis code using OpenCV-3.4. Implemented in Pycharm IDE

Resources

License

Stars

Watchers

Forks