Skip to content

masouduut94/volleyball_analytics

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ Volleyball Analytics

image source

๐Ÿš€ Quick Start

๐Ÿ“ฅ Clone the Repository (Important!)

This repository uses Git submodules for the ML Manager. You must clone it correctly:

# Clone with submodules (RECOMMENDED)
git clone --recursive https://github.com/masouduut94/volleyball_analytics.git

# OR if already cloned, initialize submodules
git submodule update --init --recursive

๐ŸŽฏ What You'll Get

  • ๐Ÿ ML Pipeline: Video classification + Object detection for volleyball analysis
  • ๐ŸŽฎ Game State Classification: Service, Play, No-Play detection using VideoMAE
  • ๐ŸŽญ Action Detection: 6 volleyball actions (serve, receive, set, spike, block, ball)
  • ๐ŸŸ๏ธ Court Segmentation: Court boundary detection
  • ๐Ÿ“Š Analytics: Real-time statistics and insights
  • ๐ŸŒ API Backend: FastAPI-based backend for data storage and retrieval

๐Ÿ—๏ธ Project Architecture

This machine learning project runs in real-time on top of 2 deep learning models:

๐ŸŽฌ Video Classification Model (VideoMAE)

In a live broadcast game, it's important to run processes only when the game is on. To extract the periods when the game is active, HuggingFace VideoMAE is utilized.

๐ŸŽฏ Purpose: Game state classification with 3 labels

  • ๐Ÿ SERVICE: Start of play when a player tosses the ball to serve
  • ๐ŸŽฎ PLAY: Active game periods where players are playing
  • โธ๏ธ NO-PLAY: Inactive periods where players are not playing

๐Ÿ—๏ธ Architecture: videomae architecture

image source

๐ŸŽฏ YOLOv8 Object Detection Model

This state-of-the-art model is trained on a custom volleyball dataset for object detection and action recognition.

๐Ÿ—๏ธ Architecture: Yolov8

image source

๐ŸŽจ Detection Classes (6 different objects with color-coded bounding boxes):

  • ๐Ÿ”ด Red box: Volleyball ball
  • ๐ŸŸค Brown box: Volleyball service action
  • ๐ŸŸข Green box: Volleyball reception action
  • ๐Ÿ”ต Blue box: Volleyball setting action
  • ๐ŸŸฃ Purple box: Volleyball blocking action
  • ๐ŸŸ  Orange box: Volleyball spike action

๐ŸŽฅ Demo Results

The system provides real-time analysis with:

  • ๐ŸŽฎ Game State: Displayed in top-left corner (SERVICE/PLAY/NO-PLAY)
  • ๐ŸŽฏ Object Detection: Color-coded bounding boxes (only on SERVICE and PLAY frames)
  • ๐Ÿ“Š Analytics: Real-time statistics and insights

๐Ÿ† Demo Videos

๐Ÿ‡ซ๐Ÿ‡ท France vs ๐Ÿ‡ต๐Ÿ‡ฑ Poland

demo1

๐Ÿ‡บ๐Ÿ‡ธ USA vs ๐Ÿ‡จ๐Ÿ‡ฆ Canada

demo2

๐Ÿ‡บ๐Ÿ‡ธ USA vs ๐Ÿ‡ต๐Ÿ‡ฑ Poland

demo3

๐ŸŽฏ Ace Point Detection

The system can extract specific game moments, like ace points: ace

๐Ÿ› ๏ธ Setup & Installation

๐Ÿ“‹ Prerequisites

  • ๐Ÿ Python: 3.11 or higher
  • ๐Ÿ’พ PostgreSQL: For database functionality (optional)
  • ๐ŸŽฎ GPU: Recommended for real-time inference (CUDA compatible)

๐Ÿš€ Installation Steps

  1. Clone with Submodules

    git clone --recursive https://github.com/masouduut94/volleyball_analytics.git
    cd volleyball_analytics
  2. Install Dependencies

    # Using uv (recommended)
    uv sync
    
    # Or using pip with pyproject.toml
    pip install -e .
    
    # Or using pip with dependencies from pyproject.toml
    pip install torch torchvision ultralytics transformers opencv-python pillow numpy
  3. Fix PyTorchVideo Compatibility (โšก IMPORTANT!)

    # Uninstall old pytorchvideo to avoid compatibility issues
    pip uninstall pytorchvideo -y
    
    # Install latest version from GitHub (fixes torchvision compatibility)
    pip install git+https://github.com/facebookresearch/pytorchvideo
  4. Download Model Weights

    # Create weights directory
    mkdir -p weights
    
    # Download weights (see Weights section below)

๐ŸŽฏ Model Weights Setup

The system requires pre-trained models for inference. Weights are automatically downloaded when you first run the system, or you can download them manually.

๐Ÿš€ Automatic Download (Recommended)

from ml_manager import MLManager

# Weights are automatically downloaded if missing
manager = MLManager()

๐Ÿ“ฅ Manual Download

from ml_manager.utils.downloader import download_all_models

# Download all models in one ZIP file
success = download_all_models()

๐Ÿ“ Final Directory Structure

weights/
โ”œโ”€โ”€ ๐Ÿ ball/weights/best.pt           # Ball detection & segmentation
โ”œโ”€โ”€ ๐ŸŽญ action/weights/best.pt         # Action detection (6 classes)  
โ”œโ”€โ”€ ๐ŸŸ๏ธ court/weights/best.pt          # Court segmentation
โ””โ”€โ”€ ๐ŸŽฎ game_state/                    # Game state classification
    โ””โ”€โ”€ [checkpoint files]

Download Source: Complete Weights ZIP

๐Ÿงช Testing & Inference

๐ŸŽฌ Quick Test Script

Use our comprehensive test script to validate the ML Manager with your video:

# Run complete test with your video
python test_ml_manager.py --video_path path/to/your/video.mp4

# Test only object detection
python test_ml_manager.py --video_path path/to/your/video.mp4 --mode detection

# Test only video classification
python test_ml_manager.py --video_path path/to/your/video.mp4 --mode classification

# Custom output directory
python test_ml_manager.py --video_path path/to/your/video.mp4 --output_dir my_results/

๐Ÿ“บ Download Test Video

For testing the ML models, download a volleyball video clip from this YouTube video (starts at 3:13). This video contains excellent examples of volleyball actions and game states for testing.

Why this video is perfect for testing:

  • ๐Ÿ Clear volleyball actions (serve, spike, block, set)
  • ๐ŸŽฎ Multiple game states (service, play, no-play)
  • ๐Ÿ“น Good video quality and camera angles
  • โฑ๏ธ Multiple rallies and game situations

How to use:

  1. Download the video using any YouTube downloader
  2. Save it as volleyball_test.mp4 (or any name you prefer)
  3. Run the test script: python test_ml_manager.py --video_path volleyball_test.mp4

๐ŸŽฏ Individual Model Testing

Test specific components individually:

# Basic inference test using demo.py
python src/demo.py --video_path path/to/your/video.mp4

# VideoMAE only (game state classification)
python src/VideoMAE_inference.py

# YOLO only (object detection)
python src/yolo_inference.py

๐ŸŽฏ ML Manager Integration

The system uses a unified ML Manager module for all machine learning operations:

from src.ml_manager import MLManager

# Initialize ML Manager
ml_manager = MLManager(verbose=True)

# Game state classification
game_state = ml_manager.classify_game_state(frames)

# Action detection
actions = ml_manager.detect_actions(frame)

# Ball detection
ball_detections = ml_manager.detect_ball(frame)

๐Ÿ“š ML Manager Documentation: See src/ml_manager/README.md for comprehensive usage.

๐Ÿงช Testing the Setup

The test_ml_manager.py script provides comprehensive testing of all ML Manager functionality:

๐Ÿ” What the test script does:

  • โœ… Validates video file format and properties
  • โœ… Tests ML Manager initialization and model loading
  • โœ… Verifies all detection models (ball, actions, players)
  • โœ… Tests game state classification with video sequences
  • โœ… Runs full pipeline demos with visualization
  • โœ… Generates output videos for verification

๐Ÿ“Š Test modes available:

  • full: Complete testing with both detection and classification demos
  • detection: Object detection testing only (ball, actions, players)
  • classification: Video classification testing only (game states)
# Comprehensive test (recommended for first-time setup)
python test_ml_manager.py --video_path path/to/volleyball_video.mp4

# Quick model validation (no video output)
python test_ml_manager.py --video_path path/to/volleyball_video.mp4 --mode detection

# Legacy testing methods
cd src/ml_manager
python test_ml_manager.py

# Test integration
cd ../..
python src/ml_manager/example_usage.py

๐Ÿ—„๏ธ Database Setup (Optional)

For storing results and analytics, set up PostgreSQL:

1. Install PostgreSQL

Follow the PostgreSQL installation guide

2. Create Configuration

# Create database config
cp conf/sample.env conf/.env

# Edit conf/.env with your database credentials
MODE=development
DEV_USERNAME=your_username
DEV_PASSWORD=your_password
DEV_HOST=localhost
DEV_DB=volleyball_development
DEV_PORT=5432
DEV_DRIVER=postgresql
TEST_DB_URL=sqlite:///./vb.db

3. Initialize Database

# Start the API server
uvicorn src.backend.app.app:app --reload

# In another terminal, seed the database
python src/api_init_data.py

# Run the main pipeline
python src/main.py

๐Ÿ“Š Data Sources

The video clips used for training and testing are sourced from:

  • ๐Ÿ“บ YouTube Channel: Volleyball Watchdog
  • ๐Ÿ† Content: International volleyball matches and tournaments

๐Ÿ”ฎ Future Development

๐Ÿ“ˆ Planned Features

  1. ๐Ÿ“Š Data Analysis: Advanced KPIs and analytics

    • Service success rate analysis
    • Service zone analysis
    • Reception success rate
    • Player performance metrics
    • Team strategy analysis
  2. ๐Ÿ“š Dataset Publication: Open-source datasets

    • Video classification dataset
    • Volleyball object detection dataset
    • Annotated training data
  3. ๐ŸŽฏ Real-time Analytics: Live match analysis

    • Real-time statistics
    • Live performance metrics
    • Instant insights for coaches

๐Ÿ—๏ธ Project Structure

volleyball_analytics/
โ”œโ”€โ”€ ๐Ÿง  src/                           # Main source code
โ”‚   โ”œโ”€โ”€ ๐ŸŽฎ ml_manager/               # ML Manager submodule
โ”‚   โ”œโ”€โ”€ ๐ŸŒ backend/                  # FastAPI backend
โ”‚   โ”œโ”€โ”€ ๐ŸŽฌ main.py                   # Main ML pipeline
โ”‚   โ”œโ”€โ”€ ๐ŸŽฅ demo.py                   # Demo application
โ”‚   โ””โ”€โ”€ ๐Ÿ› ๏ธ utilities/               # Utility functions
โ”œโ”€โ”€ ๐Ÿ“Š data/                         # Datasets and processed data
โ”œโ”€โ”€ ๐ŸŽฏ weights/                      # Model weights (download required)
โ”œโ”€โ”€ ๐Ÿ“š notebooks/                    # Jupyter notebooks
โ”œโ”€โ”€ ๐Ÿ› ๏ธ scripts/                     # Utility scripts
โ”œโ”€โ”€ ๐Ÿ“‹ conf/                         # Configuration files
โ””โ”€โ”€ ๐Ÿ“– README.md                     # This file

๐Ÿ†˜ Troubleshooting

Common Issues

  1. ๐Ÿšซ Import Errors

    # Ensure submodules are initialized
    git submodule update --init --recursive
    
    # Check Python path
    python -c "from src.ml_manager import MLManager; print('Success!')"
  2. ๐ŸŽฏ Model Weights Missing

    • Verify weights directory structure
    • Check download links above
    • Ensure file permissions are correct
  3. ๐Ÿ Dependencies Issues

    # Reinstall dependencies
    uv sync --reinstall
    
    # Or using pip
    pip install -e .
    
    # Or reinstall specific packages
    pip install torch torchvision ultralytics transformers pytorchvideo opencv-python pillow numpy
  4. ๐ŸŽฏ PyTorchVideo Compatibility Error (โšก EASIEST SOLUTION)

    ModuleNotFoundError: No module named 'torchvision.transforms.functional_tensor'
    

    ๐Ÿ’ก Quick Fix (Recommended):

    # Uninstall old pytorchvideo
    pip uninstall pytorchvideo -y
    
    # Install latest version from GitHub (fixes compatibility)
    pip install git+https://github.com/facebookresearch/pytorchvideo

    ๐Ÿ“ Alternative Manual Fix: If you encounter this error in your own code, replace:

    import torchvision.transforms.functional_tensor as F_t

    with:

    import torchvision.transforms.functional as F_t
  5. ๐Ÿ’พ Database Connection

    • Verify PostgreSQL is running
    • Check .env file configuration
    • Ensure database exists and is accessible

๐Ÿค Contributing

  1. ๐Ÿ”€ Fork the repository
  2. ๐ŸŒฟ Create a feature branch
  3. ๐Ÿ’ป Make your changes
  4. ๐Ÿงช Add tests if applicable
  5. ๐Ÿ“ Submit a pull request

๐Ÿ“„ License

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

๐Ÿ”— Additional Resources


๐Ÿ Happy Volleyball Analytics! Let's revolutionize the game with AI! ๐Ÿš€โœจ

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages