A comprehensive computer vision project for detecting football players, referees, goalkeepers, and balls in football match images using YOLOv8 object detection model.
This project implements a state-of-the-art object detection system specifically designed for football/soccer analytics. The model can identify and locate:
- Players - Field players from both teams
- Referees - Match officials
- Goalkeepers - Goal keepers
- Ball - The football
The dataset contains high-resolution (1920x1080) football match images with bounding box annotations:
- Training Set: ~7,134 annotations
- Validation Set: ~1,175 annotations
- Test Set: ~600 annotations
- Format: CSV annotations with
filename,width,height,class,xmin,ymin,xmax,ymax
pip install -r requirements.txtConvert CSV annotations to YOLO format:
python src/data_preprocessing.pyThis will:
- Convert CSV annotations to YOLO format
- Organize dataset into proper directory structure
- Generate
dataset.yamlconfiguration file
python src/train_model.py --epochs 100 --batch-size 16 --model yolov8nAvailable model sizes: yolov8n, yolov8s, yolov8m, yolov8l, yolov8x
python src/evaluate.py --model runs/detect/train/weights/best.pt --data dataset.yaml# Single image
python src/inference.py --model runs/detect/train/weights/best.pt --source path/to/image.jpg
# Batch processing
python src/inference.py --model runs/detect/train/weights/best.pt --source path/to/images/ --save-viz
# Video processing
python src/inference.py --model runs/detect/train/weights/best.pt --source path/to/video.mp4Football AI Analytics/
βββ src/
β βββ data_preprocessing.py # Convert CSV to YOLO format
β βββ train_model.py # Model training script
β βββ evaluate.py # Model evaluation
β βββ inference.py # Inference and prediction
β βββ visualize.py # Dataset visualization
βββ train/ # Training images and annotations
βββ valid/ # Validation images and annotations
βββ test/ # Test images and annotations
βββ requirements.txt # Python dependencies
βββ config.yaml # YOLOv8 configuration
βββ README.md # This file
Key training parameters in train_model.py:
epochs = 100 # Number of training epochs
batch_size = 16 # Batch size
image_size = 640 # Input image size
learning_rate = 0.01 # Initial learning rateThe config.yaml file defines:
- Dataset paths
- Class names and indices
- Number of classes
The model is evaluated using standard object detection metrics:
- mAP@0.5: Mean Average Precision at IoU threshold 0.5
- mAP@0.5:0.95: Mean Average Precision across IoU thresholds 0.5-0.95
- Precision: True positives / (True positives + False positives)
- Recall: True positives / (True positives + False negatives)
python src/visualize.py --dataset . --analysis stats samples characteristics heatmapThis provides:
- Dataset statistics and class distribution
- Sample annotated images
- Class characteristics analysis
- Annotation density heatmaps
Training progress is automatically logged and can be visualized:
python src/evaluate.py --model best.pt --data dataset.yaml --results-dir runs/detect/train--model: Path to trained model weights--source: Input source (image/video/directory)--conf: Confidence threshold (default: 0.25)--iou: IoU threshold for NMS (default: 0.45)--save-viz: Save visualization images--output: Output directory for results
from src.inference import FootballPlayerInference
# Initialize inference
detector = FootballPlayerInference('path/to/model.pt')
# Predict single image
result = detector.predict_image('image.jpg')
# Visualize predictions
detector.visualize_predictions('image.jpg', result)This model can be used for:
- Sports Analytics: Player tracking and performance analysis
- Broadcast Enhancement: Automatic player identification
- Tactical Analysis: Formation and movement analysis
- Real-time Detection: Live match analysis
- Academic Research: Sports computer vision studies
- Data Augmentation: Enabled by default in YOLOv8
- Transfer Learning: Uses pre-trained COCO weights
- Multi-scale Training: Automatic in YOLOv8
- Hyperparameter Tuning: Use
--hypparameter
- Model Export: Export to ONNX/TensorRT for faster inference
- Batch Processing: Process multiple images together
- GPU Acceleration: Automatic CUDA support if available
- CUDA Out of Memory: Reduce batch size or image size
- Low mAP: Increase training epochs or check annotation quality
- Slow Training: Ensure GPU is being used (
torch.cuda.is_available())
- Check annotation quality and consistency
- Ensure balanced class distribution
- Consider data augmentation strategies
- Validate dataset splits
This project is for educational and research purposes. Please ensure you have appropriate rights to use the football match images.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
For issues and questions:
- Check the troubleshooting section
- Review YOLOv8 documentation
- Open an issue with detailed description
- v1.0: Initial release with YOLOv8 implementation
- v1.1: Added comprehensive evaluation metrics
- v1.2: Enhanced visualization tools
- v1.3: Added video processing capabilities
Happy Detecting! β½