# makar190/yolo-football-match-analysis architecture

> Architecture diagram of the GitHub repository makar190/yolo-football-match-analysis, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/makar190/yolo-football-match-analysis
- Repository: https://github.com/makar190/yolo-football-match-analysis

## Overview

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: [`modules/trackers.py`](https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/trackers.py) (Trackers), [`modules/stats/stats_manager.py`](https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/stats/stats_manager.py) (Stats Manager), [`main.py`](https://github.com/makar190/yolo-football-match-analysis/blob/main/main.py) (main.py), [`modules/camera_movement_estimator.py`](https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/camera_movement_estimator.py) (Camera Motion)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g1["Pipeline"]
  node_main_py["main.py<br/>entrypoint<br/>[main.py]"]
  node_trackers_mod["Trackers<br/>detection+tracking<br/>[trackers.py]"]
end

subgraph group_g2["Football logic"]
  node_camera_est["Camera Motion<br/>motion correction"]
  node_team_assigner["Team Assign<br/>jersey clustering<br/>[team_assigner.py]"]
  node_player_ball_assigner["Ball Assign<br/>possession heuristic"]
end

subgraph group_g3["Tracking internals"]
  node_ball_prefilter["Ball Prefilter<br/>tracking filter<br/>[ball_prefilter.py]"]
  node_tracking_cache["Tracking Cache<br/>[cache.py]"]
  node_tracking_configs["Tracking Config<br/>[configs.py]"]
end

subgraph group_g4["Stats"]
  node_stats_manager["Stats Manager<br/>aggregation<br/>[stats_manager.py]"]
  node_possession_stats["Possession<br/>metric<br/>[possession.py]"]
  node_touches_stats["Touches<br/>metric<br/>[touches.py]"]
  node_pass_entry_stats["Pass Entry<br/>metric<br/>[pass_entry.py]"]
  node_ball_visibility_stats["Ball Visibility<br/>metric<br/>[ball_visibility.py]"]
  node_streamlit_stats["Stats UI<br/>inspection UI<br/>[streamlit_app.py]"]
end

subgraph group_g5["Media and output"]
  node_video_helpers["Video I/O<br/>media I/O<br/>[video.py]"]
  node_ffmpeg_helpers["FFmpeg<br/>encoding helper<br/>[ffmpeg.py]"]
  node_annotations_mod["Annotations<br/>rendering<br/>[annotations.py]"]
  node_helpers_annotations["Helper Draw<br/>rendering helper<br/>[annotations.py]"]
  node_frame_writer["Frame Writer<br/>video output<br/>[frame_writer.py]"]
  node_draw_mod["Track Draw<br/>overlay drawing<br/>[draw.py]"]
end

subgraph group_g6["Support"]
  node_bench_scripts["Benchmarks<br/>benchmarking<br/>[bench_streaming.py]"]
  node_smoke_scripts["Smoke Test<br/>benchmarking<br/>[smoke_streaming.py]"]
  node_train_notebooks["Training"]
end

node_main_py -->|"loads video"| node_video_helpers
node_main_py -->|"runs pipeline"| node_trackers_mod
node_main_py -->|"applies motion correction"| node_camera_est
node_main_py -->|"assigns teams"| node_team_assigner
node_main_py -->|"infers possession"| node_player_ball_assigner
node_main_py -->|"builds stats"| node_stats_manager
node_main_py -->|"writes output"| node_frame_writer
node_trackers_mod -->|"uses config"| node_tracking_configs
node_trackers_mod -->|"filters ball"| node_ball_prefilter
node_trackers_mod -->|"reuses results"| node_tracking_cache
node_trackers_mod -->|"overlays tracks"| node_draw_mod
node_trackers_mod -->|"annotates frames"| node_annotations_mod
node_draw_mod -->|"uses helpers"| node_helpers_annotations
node_frame_writer -->|"encodes video"| node_ffmpeg_helpers
node_frame_writer -->|"streams frames"| node_video_helpers
node_camera_est -->|"corrects geometry"| node_player_ball_assigner
node_camera_est -->|"stabilizes stats"| node_stats_manager
node_team_assigner -->|"labels entities"| node_stats_manager
node_player_ball_assigner -->|"feeds events"| node_stats_manager
node_stats_manager -->|"computes"| node_possession_stats
node_stats_manager -->|"computes"| node_touches_stats
node_stats_manager -->|"computes"| node_pass_entry_stats
node_stats_manager -->|"computes"| node_ball_visibility_stats
node_stats_manager -->|"exports"| node_streamlit_stats
node_tracking_cache -->|"replays outputs"| node_streamlit_stats
node_bench_scripts -->|"measures"| node_trackers_mod
node_smoke_scripts -->|"verifies"| node_trackers_mod
node_train_notebooks -.->|"trains detector"| node_trackers_mod

click node_main_py "https://github.com/makar190/yolo-football-match-analysis/blob/main/main.py"
click node_trackers_mod "https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/trackers.py"
click node_video_helpers "https://github.com/makar190/yolo-football-match-analysis/blob/main/helpers/video.py"
click node_ffmpeg_helpers "https://github.com/makar190/yolo-football-match-analysis/blob/main/helpers/ffmpeg.py"
click node_annotations_mod "https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/annotations.py"
click node_helpers_annotations "https://github.com/makar190/yolo-football-match-analysis/blob/main/helpers/annotations.py"
click node_frame_writer "https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/tracking/frame_writer.py"
click node_draw_mod "https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/tracking/draw.py"
click node_camera_est "https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/camera_movement_estimator.py"
click node_team_assigner "https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/team_assigner.py"
click node_player_ball_assigner "https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/player_ball_assigner.py"
click node_ball_prefilter "https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/tracking/ball_prefilter.py"
click node_tracking_cache "https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/tracking/cache.py"
click node_tracking_configs "https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/tracking/configs.py"
click node_stats_manager "https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/stats/stats_manager.py"
click node_possession_stats "https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/stats/possession.py"
click node_touches_stats "https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/stats/touches.py"
click node_pass_entry_stats "https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/stats/pass_entry.py"
click node_ball_visibility_stats "https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/stats/ball_visibility.py"
click node_streamlit_stats "https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/stats/streamlit_app.py"
click node_bench_scripts "https://github.com/makar190/yolo-football-match-analysis/blob/main/scripts/bench_streaming.py"
click node_smoke_scripts "https://github.com/makar190/yolo-football-match-analysis/blob/main/scripts/smoke_streaming.py"
click node_train_notebooks "https://github.com/makar190/yolo-football-match-analysis/blob/main/train/train_yolov11.ipynb"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main_py,node_trackers_mod toneBlue
class node_camera_est,node_team_assigner,node_player_ball_assigner toneAmber
class node_ball_prefilter,node_tracking_cache,node_tracking_configs toneMint
class node_stats_manager,node_possession_stats,node_touches_stats,node_pass_entry_stats,node_ball_visibility_stats,node_streamlit_stats toneRose
class node_video_helpers,node_ffmpeg_helpers,node_annotations_mod,node_helpers_annotations,node_frame_writer,node_draw_mod toneIndigo
class node_bench_scripts,node_smoke_scripts,node_train_notebooks toneTeal
```

## Components

### Pipeline

Top-level analysis flow from video input to outputs.

- [main.py](https://github.com/makar190/yolo-football-match-analysis/blob/main/main.py): entrypoint · Top-level run_analysis orchestration for the full pipeline. · `main.py`
- [Trackers](https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/trackers.py): detection+tracking · Wraps player/ball detection and multi-object tracking workflow. · `modules/trackers.py`

### Football logic

Domain-specific reasoning layered on top of detections and tracking.

- [Camera Motion](https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/camera_movement_estimator.py): motion correction · Estimates broadcast camera movement to stabilize downstream heuristics. · `modules/camera_movement_estimator.py`
- [Team Assign](https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/team_assigner.py): jersey clustering · Assigns players to teams from appearance/color cues. · `modules/team_assigner.py`
- [Ball Assign](https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/player_ball_assigner.py): possession heuristic · 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](https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/tracking/ball_prefilter.py): tracking filter · Pre-filters ball candidates before downstream tracking logic. · `modules/tracking/ball_prefilter.py`
- [Tracking Cache](https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/tracking/cache.py): cache · Stores reusable tracking artifacts for repeat analysis and rendering. · `modules/tracking/cache.py`
- [Tracking Config](https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/tracking/configs.py): config · Central tracking configuration and runtime parameters. · `modules/tracking/configs.py`

### Stats

Match-derived metric aggregation and UI consumption.

- [Stats Manager](https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/stats/stats_manager.py): aggregation · Aggregates match statistics from tracked and assigned objects. · `modules/stats/stats_manager.py`
- [Possession](https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/stats/possession.py): metric · Computes team possession-style statistics. · `modules/stats/possession.py`
- [Touches](https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/stats/touches.py): metric · Counts or derives ball touch events. · `modules/stats/touches.py`
- [Pass Entry](https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/stats/pass_entry.py): metric · Derives pass-entry or progression cues. · `modules/stats/pass_entry.py`
- [Ball Visibility](https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/stats/ball_visibility.py): metric · Tracks when the ball is visible to the pipeline. · `modules/stats/ball_visibility.py`
- [Stats UI](https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/stats/streamlit_app.py): inspection 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](https://github.com/makar190/yolo-football-match-analysis/blob/main/helpers/video.py): media I/O · Reads input clips and handles video boundary concerns. · `helpers/video.py`
- [FFmpeg](https://github.com/makar190/yolo-football-match-analysis/blob/main/helpers/ffmpeg.py): encoding helper · Optional ffmpeg-backed media acceleration and encoding support. · `helpers/ffmpeg.py`
- [Annotations](https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/annotations.py): rendering · Shared annotation and drawing utilities for output frames. · `modules/annotations.py`
- [Helper Draw](https://github.com/makar190/yolo-football-match-analysis/blob/main/helpers/annotations.py): rendering helper · Additional annotation helpers used by visualization code. · `helpers/annotations.py`
- [Frame Writer](https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/tracking/frame_writer.py): video output · Writes annotated frames back into video output. · `modules/tracking/frame_writer.py`
- [Track Draw](https://github.com/makar190/yolo-football-match-analysis/blob/main/modules/tracking/draw.py): overlay drawing · Tracking-specific overlay and drawing routines. · `modules/tracking/draw.py`

### Support

Benchmarking, caching, and training artifacts.

- [Benchmarks](https://github.com/makar190/yolo-football-match-analysis/blob/main/scripts/bench_streaming.py): benchmarking · Streaming performance and smoke-test scripts. · `scripts/bench_streaming.py`
- [Smoke Test](https://github.com/makar190/yolo-football-match-analysis/blob/main/scripts/smoke_streaming.py): benchmarking · Basic robustness check for streaming execution. · `scripts/smoke_streaming.py`
- [Training](https://github.com/makar190/yolo-football-match-analysis/blob/main/train/train_yolov11.ipynb): model training · Notebook-based YOLO training artifacts, separate from inference. · `train/train_yolov11.ipynb`

## Connections

- 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.
