A comprehensive real-time sports analytics platform that uses computer vision and machine learning to analyze sports videos, track players, identify teams, detect jersey numbers, and provide detailed performance insights.
This system transforms raw sports video footage into actionable analytics by:
- π₯ Video Analysis: Processes sports videos frame-by-frame using advanced computer vision
- π₯ Player Tracking: Uses YOLO v13 object detection to track players throughout the game
- πββοΈ Team Identification: Automatically classifies players into teams using machine learning clustering
- π’ Jersey Number Detection: Recognizes jersey numbers using OCR technology
- π Spatial Mapping: Maps player positions from video coordinates to real pitch coordinates using homography
- π Real-time Dashboard: Provides live analytics through an interactive web interface
- β‘ Performance Metrics: Tracks player movements, team formations, and game statistics
The system is built with a clean, modular architecture:
βββ src/
β βββ core/ # Core business logic (Config, Homography)
β βββ models/ # AI/ML models (YOLO, Segmentation)
β βββ analytics/ # Analytics processing (Team ID, Jersey Detection)
β βββ api/ # Web server & API (Dashboard, WebSocket)
β βββ utils/ # Utility functions (Logging, Video, Performance)
βββ config/ # Configuration files
βββ static/ # Web assets (CSS, JavaScript)
βββ templates/ # HTML templates
βββ tests/ # Test suite
βββ uploads/ # Video upload directory
- Python 3.8+
- pip or uv package manager
- At least 4GB RAM (8GB+ recommended)
- GPU support optional but recommended for better performance
-
Clone the repository:
git clone <repository-url>
-
Install dependencies:
# Using pip pip install -r requirements.txt# Or using uv (faster) uv add -r requirements.txt
Start the interactive web dashboard:
python main.pyThen open your browser to: http://localhost:8000
Features:
- Upload video files for analysis
- Real-time analytics streaming
- Interactive data visualization
- Team and player statistics
- Performance metrics dashboard
Analyze a video directly from command line:
python analyze_video.py path/to/your/video.mp4 --output results.csvUse the original dashboard interface:
python run_dashboard.pyEdit config/config.yaml to customize:
video:
supported_formats: ['.mp4', '.avi', '.mov', '.mkv']
min_resolution: [320, 240]
max_resolution: [4096, 2160]
models:
yolo_path: 'yolov8n.pt'
jersey_yolo_path: null # Path to custom jersey detection model
processing:
team_n_clusters: 3
frame_skip_interval: 2
enable_jersey_detection: true
enable_ocr: true
enable_homography: true- Start the server:
python main.py - Open browser: Navigate to
http://localhost:8000 - Upload video: Click "Upload Video" and select your sports video
- View analytics: Watch real-time analysis results as they stream in
- Export data: Download results as CSV for further analysis
# Basic analysis
python analyze_video.py game_footage.mp4
# Custom output location
python analyze_video.py game_footage.mp4 --output detailed_analysis.csv
# Custom configuration
python analyze_video.py game_footage.mp4 --config custom_config.yamlThe dashboard supports real-time analytics streaming via WebSocket:
// Connect to live analytics stream
const ws = new WebSocket('ws://localhost:8000/ws')
ws.onmessage = function (event) {
const data = JSON.parse(event.data)
// Process real-time analytics data
}Replace default models with your own:
- YOLO Model: Place custom YOLO model in project root
- Jersey Detection: Train custom model for jersey number recognition
- Segmentation: Use custom pitch line detection model
For better performance:
- Use GPU-enabled PyTorch installation
- Increase
frame_skip_intervalfor faster processing - Reduce video resolution for real-time analysis
- Enable hardware acceleration if available
The system generates comprehensive analytics including:
- Player ID and tracking confidence
- Team classification (Team A/Team B)
- Jersey number (when detectable)
- Position coordinates (video and pitch space)
- Movement patterns and speed
- Team formation analysis
- Player distribution on field
- Possession statistics
- Movement heatmaps
- Processing speed (FPS)
- Detection accuracy
- System resource usage
- Error rates and quality metrics
Run the test suite to verify functionality:
# Run specific tests
python -m pytest tests/test_sports_analytics.py -v
# Run all tests
python -m pytest tests/ -vThe codebase follows clean architecture principles:
- Separation of Concerns: Each module has a specific responsibility
- Dependency Injection: Components are loosely coupled
- Testability: All components can be unit tested
- Extensibility: Easy to add new features and models
- New Analytics: Add to
src/analytics/ - New Models: Add to
src/models/ - New APIs: Add to
src/api/ - New Utilities: Add to
src/utils/
- Follow PEP 8 Python style guidelines
- Use type hints for better code documentation
- Add docstrings to all public methods
- Write unit tests for new functionality
- Fork the repository
- Create a feature branch:
git checkout -b feature-name - Make your changes and add tests
- Run the test suite:
python test_modular_structure.py - Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
Import Errors: Ensure you're running from the project root directory
Memory Issues: Reduce video resolution or increase frame skip interval
Model Download Failures: Check internet connection for YOLO model downloads
Performance Issues: Enable GPU support or reduce processing quality
- Review configuration:
config/config.yaml - Check logs:
sports_analytics.log - Verify system requirements and dependencies
- β Real-time Processing: Stream analytics as video plays
- β Modular Architecture: Clean, maintainable codebase
- β Web Dashboard: Interactive browser-based interface
- β Multiple Input Formats: Support for various video formats
- β Automated Team Detection: No manual team labeling required
- β Jersey Number Recognition: OCR-based number detection
- β Spatial Mapping: Video-to-pitch coordinate transformation
- β Performance Monitoring: Built-in performance metrics
- β Export Capabilities: CSV output for further analysis
- β Extensible Design: Easy to add new features and models
Ready to analyze your sports videos? Start with python main.py and open http://localhost:8000! π