How makar190/yolo-football-match-analysis works
1. Overall shape
This is a single-video analysis pipeline for broadcast football footage. The runtime path is: input MP4 → frame streaming → player/ball detection → multi-object tracking → team/ball heuristics → statistics aggregation → annotated video + JSON output.
2. Main orchestration boundary
main.py is the top-level entry point. It exposes run_analysis(), sets default paths/parameters, and coordinates the full pipeline. The repository is organized around this function rather than around a web server or service API.
3. Detection and tracking core
The central subsystem is modules/trackers.py, which wraps the detection/tracking workflow. The README describes a custom Ultralytics YOLO model for detecting players, referees, and the ball, with Supervision ByteTrack used to keep stable identities across frames.
4. Streaming-first processing
The modules/tracking/ package and benchmark scripts suggest the pipeline is designed to process frames incrementally, not by loading the whole video into memory. This boundary matters for architecture: detection/tracking operate on a stream, while outputs are written progressively.
5. Ball-to-player and team reasoning
modules/player_ball_assigner.py and modules/team_assigner.py encode the domain heuristics. Team assignment is based on jersey appearance clustering, while ball ownership relies on spatial plausibility, overlap, and motion cues. These are the key “football logic” layers on top of raw detections.
6. Camera motion compensation
modules/camera_movement_estimator.py handles broadcast pan/zoom effects. It is an important correction layer because the project does not use calibrated pitch coordinates; this module reduces errors in possession/touch-style stats when the camera moves.
7. Statistics subsystem
modules/stats/ is a separate analysis layer that consumes tracked/assigned objects and produces match-derived metrics. The visible stats modules include possession, touches, pass-entry cues, and ball visibility, with stats_manager.py acting as the aggregator/orchestrator.
8. Rendering and annotation
Visual output is handled by annotation/drawing utilities in modules/annotations.py, helpers/annotations.py, and modules/tracking/draw.py, with modules/tracking/frame_writer.py likely responsible for writing annotated frames back to video. This keeps visualization separate from analysis logic.
9. I/O and media utilities
helpers/video.py and helpers/ffmpeg.py form the media boundary: reading input clips, handling encoding/decoding, and optionally using ffmpeg acceleration or a fallback binary. helpers/foot_roi.py supports the geometry used in ball/player proximity logic.
10. Caching and repeatability
There are explicit cache artifacts in outputs/.streamlit_video_cache/ and the README mentions reusing cached tracking results. This shows a performance boundary where expensive tracking can be reused across later renders or stats views.
11. User-facing consumption
streamlit_stats.py is a lightweight inspection UI, not the core pipeline. It reads processed outputs and stats JSON from outputs/stats/ to let a user browse analyzed clips and view derived match statistics.
12. Benchmarking and training are separate concerns
scripts/bench_streaming.py and scripts/smoke_streaming.py measure pipeline performance/robustness. train/ contains notebooks for model training, but those artifacts are not part of the inference architecture used for match analysis.
13. External dependencies that shape the design
The architecture is strongly influenced by Ultralytics YOLO for detection, Supervision ByteTrack for tracking, OpenCV/ffmpeg for video I/O, Streamlit for visualization, and scikit-learn for color clustering. The repo also assumes a custom YOLO weight file in models/, which is a critical runtime asset.
14. Primary outputs
The two durable outputs are an annotated MP4 in outputs/ and a stats JSON in outputs/stats/. Everything else in the pipeline exists to produce or consume those artifacts.
Main entry points: trackers.py Trackers · stats_manager.py Stats Manager · main.py main.py · camera_movement_estimator.py Camera Motion
Components 23
Pipeline
Top-level analysis flow from video input to outputs.
Football logic
Domain-specific reasoning layered on top of detections and tracking.
- Camera Motion · Estimates broadcast camera movement to stabilize downstream heuristics.
modules/camera_movement_estimator.py - Team Assign · Assigns players to teams from appearance/color cues.
modules/team_assigner.py - Ball Assign · Infers ball-to-player ownership from spatial and motion cues.
modules/player_ball_assigner.py
Tracking internals
Streaming tracking and frame-handling support modules.
- Ball Prefilter · Pre-filters ball candidates before downstream tracking logic.
modules/tracking/ball_prefilter.py - Tracking Cache · Stores reusable tracking artifacts for repeat analysis and rendering.
modules/tracking/cache.py - Tracking Config · Central tracking configuration and runtime parameters.
modules/tracking/configs.py
Stats
Match-derived metric aggregation and UI consumption.
- Stats Manager · Aggregates match statistics from tracked and assigned objects.
modules/stats/stats_manager.py - Possession · Computes team possession-style statistics.
modules/stats/possession.py - Touches · Counts or derives ball touch events.
modules/stats/touches.py - Pass Entry · Derives pass-entry or progression cues.
modules/stats/pass_entry.py - Ball Visibility · Tracks when the ball is visible to the pipeline.
modules/stats/ball_visibility.py - Stats UI · Streamlit viewer for processed clips and JSON stats.
modules/stats/streamlit_app.py
Media and output
Video I/O, annotation, and rendering utilities.
- Video I/O · Reads input clips and handles video boundary concerns.
helpers/video.py - FFmpeg · Optional ffmpeg-backed media acceleration and encoding support.
helpers/ffmpeg.py - Annotations · Shared annotation and drawing utilities for output frames.
modules/annotations.py - Helper Draw · Additional annotation helpers used by visualization code.
helpers/annotations.py - Frame Writer · Writes annotated frames back into video output.
modules/tracking/frame_writer.py - Track Draw · Tracking-specific overlay and drawing routines.
modules/tracking/draw.py
Support
Benchmarking, caching, and training artifacts.
- Benchmarks · Streaming performance and smoke-test scripts.
scripts/bench_streaming.py - Smoke Test · Basic robustness check for streaming execution.
scripts/smoke_streaming.py - Training · Notebook-based YOLO training artifacts, separate from inference.
train/train_yolov11.ipynb
Connections 28
- main.py → Video I/O: loads video · Opens the input clip for analysis.
- main.py → Trackers: runs pipeline · Coordinates detection and tracking over the video stream.
- main.py → Camera Motion: applies motion correction · Uses camera movement estimates in the analysis flow.
- main.py → Team Assign: assigns teams · Adds team labels to tracked players.
- main.py → Ball Assign: infers possession · Derives ball ownership heuristics.
- main.py → Stats Manager: builds stats · Aggregates match metrics from tracked results.
- main.py → Frame Writer: writes output · Produces the annotated output video.
- Trackers → Tracking Config: uses config · Reads runtime parameters and tracking settings.
- Trackers → Ball Prefilter: filters ball · Reduces false ball candidates before tracking.
- Trackers → Tracking Cache: reuses results · Stores or loads expensive tracking artifacts.
- Trackers → Track Draw: overlays tracks · Draws tracked objects and labels during streaming.
- Trackers → Annotations: annotates frames · Applies shared drawing primitives to frames.
- Track Draw → Helper Draw: uses helpers · Reuses lower-level annotation helpers.
- Frame Writer → FFmpeg: encodes video · Uses ffmpeg path when available for writing output media.
- Frame Writer → Video I/O: streams frames · Consumes frame stream and produces rendered media.
- Camera Motion → Ball Assign: corrects geometry · Improves proximity-based possession reasoning under camera motion.
- Camera Motion → Stats Manager: stabilizes stats · Helps possession and touch metrics remain robust during pans.
- Team Assign → Stats Manager: labels entities · Supplies team identity to downstream statistics.
- Ball Assign → Stats Manager: feeds events · Provides ball ownership and touch cues to stats aggregation.
- Stats Manager → Possession: computes · Delegates possession metric calculation.
- Stats Manager → Touches: computes · Delegates touch metric calculation.
- Stats Manager → Pass Entry: computes · Delegates pass-entry/progression metric calculation.
- Stats Manager → Ball Visibility: computes · Delegates ball visibility metric calculation.
- Stats Manager → Stats UI: exports · Provides stats JSON for the inspection UI.
- Tracking Cache → Stats UI: replays outputs · Supports browsing cached processed clips in the UI.
- Benchmarks → Trackers: measures · Benchmarks streaming performance of the tracking pipeline.
- Smoke Test → Trackers: verifies · Exercises the streaming pipeline for robustness.
- Training → Trackers: trains detector · Produces the custom YOLO model used at runtime.