# abdelrahmanatef01/tactic_zone architecture

> Architecture diagram of the GitHub repository abdelrahmanatef01/tactic_zone, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/abdelrahmanatef01/tactic_zone
- Repository: https://github.com/abdelrahmanatef01/tactic_zone

## Overview

1. **Overall shape**
   - This repository is a football match analysis pipeline with three output layers: computer-vision event extraction, recommendation generation, and LLM-based narration/chat.
   - The architecture is centered on processing match video into structured match data, then feeding that data into downstream analytics and Gemini-powered text generation.

2. **Primary entry points**
   - `main.py` appears to be the local orchestration entry point for the full pipeline.
   - `end_points.py` is the API surface for running the project through endpoints.
   - `chat_bot/app.py` is a separate Flask app focused on the coach chatbot experience.
   - The repo therefore has two interaction modes: batch/offline processing and API/chat serving.

3. **Computer vision backbone**
   - The `trackers/` package is the core frame-by-frame object tracking layer for players, referees, ball, and goalkeepers.
   - `camera_movement_estimator/` compensates for broadcast camera motion so player positions can be interpreted consistently across frames.
   - `view_transformer/` converts image coordinates into a field-aware perspective, which is important for position, distance, and formation reasoning.

4. **Detection and event extraction modules**
   - Specialized detectors live as separate packages: `pass_detector/`, `shot_detector/`, `substitution_detector/`, `formation_detector/`, `team_process/`, `goal_and_line_processor/`, and `event_process/`.
   - These modules likely consume tracked objects and frame metadata to produce event-level outputs such as passes, shots, substitutions, formations, and match state.
   - `new_data_handler/` looks like the event-model input adapter for less common classes like dribbles, tackles, injuries, and substitution-board signals.

5. **Team identity and player association**
   - `team_assigner/` classifies players into teams using jersey-color segmentation and clustering.
   - `player_ball_assigner/` links the ball to the nearest/most likely player and team on a per-frame basis.
   - `player_number_detector/` uses OCR/object detection to identify jersey numbers, which is crucial for merging tracks and mapping events to specific players.

6. **Player-level analytics**
   - `speed_and_distance_estimator/` computes movement metrics from tracked positions and perspective-transformed coordinates.
   - `player_stats/` aggregates per-player outputs across the pipeline and merges tracks that share the same shirt number.
   - This package acts as a boundary between raw CV outputs and higher-level team analytics.

7. **Team-level analytics**
   - `team_stats/` combines player stats, team identity, and formation-related features into a team summary dataset.
   - `team_process/` determines match result state such as win, loss, or draw.
   - These modules produce the structured team-facing data that the recommendation layer consumes.

8. **Recommendation subsystem**
   - `recommendation_systems/` is a separate analytical engine that uses processed team data plus historical football datasets.
   - The README indicates it recommends formation, play style, starting XI, and bench players.
   - This subsystem is decoupled from the video pipeline and depends on cleaned tabular inputs in `recommendation_systems_input_files/`.

9. **LLM and prompt generation layer**
   - `generate_prompt/` prepares the prompt content sent to Gemini for summary-style outputs.
   - `utils/Gemini_utils.py` and `chat_bot/` contain the LLM integration and interactive coach assistant.
   - The LLM layer sits on top of the structured analytics and translates them into human-readable opponent weakness analysis, training plans, and summaries.

10. **Shared utilities**
   - `utils/` contains supporting code for bounding-box handling, recommendation helpers, main workflow utilities, and Gemini helpers.
   - These utilities are cross-cutting and likely reduce duplication between detection, analytics, and LLM preparation layers.

11. **Data and model assets**
   - The project ships with input folders such as `input_videos/` and CSV datasets under `recommendation_systems_input_files/`.
   - `download_models.sh` and the README’s model links indicate external trained YOLO weights are required for deployment.
   - `fire_base/fire_base.json` suggests a Firebase credential or integration artifact, but the repo’s main architecture is not Firebase-centric.

12. **Output boundaries**
   - Outputs are organized into separate directories for computer vision and recommendation artifacts, which keeps raw analytics distinct from decision-making outputs.
   - The likely flow is: video in → detections/tracking → event and player stats → team stats → recommendations and LLM-generated reports → CSV/JSON/chat outputs.

13. **Key architectural dependency chain**
   - The most important data flow is: frame/video processing → tracking and perspective correction → event detectors and team/player attribution → aggregation into stats → recommendations and LLM narration.
   - This layered design means failures or inaccuracies in early CV stages propagate into all downstream outputs.

14. **Implementation stack that materially affects architecture**
   - The project is Python-based and depends on Flask for serving, OpenCV for video processing, Ultralytics YOLO for detection, supervision for tracking, pandas/numpy for analytics, Pulp for optimization, pytesseract for OCR, and google-generativeai for Gemini integration.
   - Those libraries define the system boundaries: real-time-ish video analytics on one side, tabular recommendation and text generation on the other.

Main entry points: [`main.py`](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/main.py) (Main run), [`event_process/event_process.py`](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/event_process/event_process.py) (Event process), [`trackers/tracker.py`](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/trackers/tracker.py) (Trackers), [`team_stats/team_stats.py`](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/team_stats/team_stats.py) (Team stats)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_ingest_cv["Video Intelligence"]
  node_node_trackers["Trackers<br/>cv tracking<br/>[tracker.py]"]
  node_node_camera["Camera motion<br/>cv correction"]
  node_node_view["Field view<br/>perspective transform"]
  node_node_team_assigner["Team assigner<br/>team classification<br/>[team_assigner.py]"]
  node_node_ball_assigner["Ball assigner<br/>association"]
  node_node_number_detector["Number OCR<br/>ocr identity"]
  node_node_event_process["Event process<br/>event extraction<br/>[event_process.py]"]
  node_node_pass["Pass detector<br/>event detector<br/>[pass_detector.py]"]
  node_node_shot["Shot detector<br/>event detector<br/>[shot_detector.py]"]
  node_node_substitution["Substitution<br/>event detector"]
  node_node_goal_line["Goal/line<br/>event detector"]
  node_node_new_data["New data<br/>event adapter"]
  node_node_formation["Formation<br/>analysis"]
end

subgraph group_group_analytics["Analytics & Recommendation"]
  node_node_speed["Speed stats<br/>movement metrics"]
  node_node_player_stats["Player stats<br/>aggregation<br/>[player_stats.py]"]
  node_node_team_process["Match state<br/>team result<br/>[team_process.py]"]
  node_node_team_stats["Team stats<br/>aggregation<br/>[team_stats.py]"]
  node_node_reco["Recommendations<br/>optimizer"]
end

subgraph group_group_serving["Serving & LLM"]
  node_node_main["Main run<br/>orchestrator<br/>[main.py]"]
  node_node_endpoints["API routes<br/>http api<br/>[end_points.py]"]
  node_node_chatbot["Coach chat<br/>flask app<br/>[app.py]"]
  node_node_prompt["Prompt builder<br/>llm prep<br/>[generate_prompt.py]"]
  node_node_gemini_utils["Gemini utils<br/>llm integration<br/>[Gemini_utils.py]"]
  node_node_utils["Shared utils<br/>helpers<br/>[main_utils.py]"]
end

subgraph group_group_assets["Data & Assets"]
  node_node_input_videos["Input videos<br/>asset<br/>[Atef.txt]"]
  node_node_reco_inputs["Reco data<br/>dataset<br/>[data_cleaned.csv]"]
  node_node_cv_outputs["CV outputs<br/>artifact<br/>[Atef.txt]"]
  node_node_reco_outputs["Reco outputs<br/>artifact<br/>[Atef.txt]"]
end

node_node_main -->|"orchestrates"| node_node_trackers
node_node_main -->|"corrects motion"| node_node_camera
node_node_main -->|"projects field"| node_node_view
node_node_trackers -->|"tracked players"| node_node_team_assigner
node_node_trackers -->|"tracked ball"| node_node_ball_assigner
node_node_trackers -->|"player crops"| node_node_number_detector
node_node_view -->|"coordinates"| node_node_speed
node_node_team_assigner -->|"team labels"| node_node_player_stats
node_node_number_detector -->|"identity merge"| node_node_player_stats
node_node_speed -->|"movement metrics"| node_node_player_stats
node_node_trackers -->|"frame state"| node_node_event_process
node_node_event_process -->|"detects"| node_node_pass
node_node_event_process -->|"detects"| node_node_shot
node_node_event_process -->|"detects"| node_node_substitution
node_node_event_process -->|"detects"| node_node_goal_line
node_node_event_process -->|"adapts"| node_node_new_data
node_node_player_stats -->|"roll up"| node_node_team_stats
node_node_formation -->|"formation data"| node_node_team_stats
node_node_team_process -->|"match result"| node_node_team_stats
node_node_team_stats -->|"structured team data"| node_node_reco
node_node_reco_inputs -->|"historical data"| node_node_reco
node_node_team_stats -->|"summary inputs"| node_node_prompt
node_node_prompt -->|"Gemini payload"| node_node_gemini_utils
node_node_gemini_utils -->|"chat responses"| node_node_chatbot
node_node_utils -.->|"shared workflow"| node_node_main
node_node_utils -.->|"shared helpers"| node_node_reco
node_node_input_videos -->|"video input"| node_node_main
node_node_main -->|"writes"| node_node_cv_outputs
node_node_reco -->|"writes"| node_node_reco_outputs
node_node_endpoints -->|"invokes"| node_node_main
node_node_endpoints -->|"serves"| node_node_reco
node_node_chatbot -->|"builds context"| node_node_prompt

click node_node_main "https://github.com/abdelrahmanatef01/tactic_zone/blob/master/main.py"
click node_node_endpoints "https://github.com/abdelrahmanatef01/tactic_zone/blob/master/end_points.py"
click node_node_chatbot "https://github.com/abdelrahmanatef01/tactic_zone/blob/master/chat_bot/app.py"
click node_node_trackers "https://github.com/abdelrahmanatef01/tactic_zone/blob/master/trackers/tracker.py"
click node_node_camera "https://github.com/abdelrahmanatef01/tactic_zone/blob/master/camera_movement_estimator/camera_movement_estimator.py"
click node_node_view "https://github.com/abdelrahmanatef01/tactic_zone/blob/master/view_transformer/view_transformer.py"
click node_node_team_assigner "https://github.com/abdelrahmanatef01/tactic_zone/blob/master/team_assigner/team_assigner.py"
click node_node_ball_assigner "https://github.com/abdelrahmanatef01/tactic_zone/blob/master/player_ball_assigner/player_ball_assigner.py"
click node_node_number_detector "https://github.com/abdelrahmanatef01/tactic_zone/blob/master/player_number_detector/player_number_detector.py"
click node_node_event_process "https://github.com/abdelrahmanatef01/tactic_zone/blob/master/event_process/event_process.py"
click node_node_pass "https://github.com/abdelrahmanatef01/tactic_zone/blob/master/pass_detector/pass_detector.py"
click node_node_shot "https://github.com/abdelrahmanatef01/tactic_zone/blob/master/shot_detector/shot_detector.py"
click node_node_substitution "https://github.com/abdelrahmanatef01/tactic_zone/blob/master/substitution_detector/substitution_detector.py"
click node_node_goal_line "https://github.com/abdelrahmanatef01/tactic_zone/blob/master/goal_and_line_processor/goal_and_line_processor.py"
click node_node_new_data "https://github.com/abdelrahmanatef01/tactic_zone/blob/master/new_data_handler/new_data_handler.py"
click node_node_speed "https://github.com/abdelrahmanatef01/tactic_zone/blob/master/speed_and_distance_estimator/speed_and_distance_estimator.py"
click node_node_player_stats "https://github.com/abdelrahmanatef01/tactic_zone/blob/master/player_stats/player_stats.py"
click node_node_team_process "https://github.com/abdelrahmanatef01/tactic_zone/blob/master/team_process/team_process.py"
click node_node_team_stats "https://github.com/abdelrahmanatef01/tactic_zone/blob/master/team_stats/team_stats.py"
click node_node_formation "https://github.com/abdelrahmanatef01/tactic_zone/blob/master/formation_detector/formation_detector.py"
click node_node_reco "https://github.com/abdelrahmanatef01/tactic_zone/blob/master/recommendation_systems/recommendation_systems.py"
click node_node_prompt "https://github.com/abdelrahmanatef01/tactic_zone/blob/master/generate_prompt/generate_prompt.py"
click node_node_gemini_utils "https://github.com/abdelrahmanatef01/tactic_zone/blob/master/utils/Gemini_utils.py"
click node_node_utils "https://github.com/abdelrahmanatef01/tactic_zone/blob/master/utils/main_utils.py"
click node_node_input_videos "https://github.com/abdelrahmanatef01/tactic_zone/blob/master/input_videos/Atef.txt"
click node_node_reco_inputs "https://github.com/abdelrahmanatef01/tactic_zone/blob/master/recommendation_systems_input_files/data_cleaned.csv"
click node_node_cv_outputs "https://github.com/abdelrahmanatef01/tactic_zone/blob/master/output_files_computer_vision/Atef.txt"
click node_node_reco_outputs "https://github.com/abdelrahmanatef01/tactic_zone/blob/master/output_files_recommendation_systems/Atef.txt"

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_node_trackers,node_node_camera,node_node_view,node_node_team_assigner,node_node_ball_assigner,node_node_number_detector,node_node_event_process,node_node_pass,node_node_shot,node_node_substitution,node_node_goal_line,node_node_new_data,node_node_formation toneBlue
class node_node_speed,node_node_player_stats,node_node_team_process,node_node_team_stats,node_node_reco toneAmber
class node_node_main,node_node_endpoints,node_node_chatbot,node_node_prompt,node_node_gemini_utils,node_node_utils toneMint
class node_node_input_videos,node_node_reco_inputs,node_node_cv_outputs,node_node_reco_outputs toneRose
```

## Components

### Video Intelligence

Frame processing, tracking, and event extraction from match video.

- [Trackers](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/trackers/tracker.py): cv tracking · Core player, ball, referee, and goalkeeper tracking. · `trackers/tracker.py`
- [Camera motion](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/camera_movement_estimator/camera_movement_estimator.py): cv correction · Estimates broadcast camera movement for stable coordinates. · `camera_movement_estimator/camera_movement_estimator.py`
- [Field view](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/view_transformer/view_transformer.py): perspective transform · Maps image coordinates into field-aware space. · `view_transformer/view_transformer.py`
- [Team assigner](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/team_assigner/team_assigner.py): team classification · Assigns players to teams from appearance cues. · `team_assigner/team_assigner.py`
- [Ball assigner](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/player_ball_assigner/player_ball_assigner.py): association · Links the ball to likely players and teams. · `player_ball_assigner/player_ball_assigner.py`
- [Number OCR](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/player_number_detector/player_number_detector.py): ocr identity · Detects jersey numbers for player identity merging. · `player_number_detector/player_number_detector.py`
- [Event process](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/event_process/event_process.py): event extraction · General event processing for match actions and state. · `event_process/event_process.py`
- [Pass detector](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/pass_detector/pass_detector.py): event detector · Detects passing events from tracked play. · `pass_detector/pass_detector.py`
- [Shot detector](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/shot_detector/shot_detector.py): event detector · Detects shot attempts and related actions. · `shot_detector/shot_detector.py`
- [Substitution](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/substitution_detector/substitution_detector.py): event detector · Detects substitution events and board signals. · `substitution_detector/substitution_detector.py`
- [Goal/line](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/goal_and_line_processor/goal_and_line_processor.py): event detector · Handles goal-line and score-related processing. · `goal_and_line_processor/goal_and_line_processor.py`
- [New data](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/new_data_handler/new_data_handler.py): event adapter · Adapts less common event classes into the pipeline. · `new_data_handler/new_data_handler.py`
- [Formation](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/formation_detector/formation_detector.py): analysis · Infers tactical formation from team structure. · `formation_detector/formation_detector.py`

### Analytics & Recommendation

Aggregation layers and the recommendation engine built on structured match data.

- [Speed stats](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/speed_and_distance_estimator/speed_and_distance_estimator.py): movement metrics · Computes distance and speed from tracked positions. · `speed_and_distance_estimator/speed_and_distance_estimator.py`
- [Player stats](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/player_stats/player_stats.py): aggregation · Aggregates player-level metrics and merges same-number tracks. · `player_stats/player_stats.py`
- [Match state](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/team_process/team_process.py): team result · Derives win/loss/draw state and team match context. · `team_process/team_process.py`
- [Team stats](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/team_stats/team_stats.py): aggregation · Builds team summaries from player and formation data. · `team_stats/team_stats.py`
- [Recommendations](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/recommendation_systems/recommendation_systems.py): optimizer · Generates formation, style, XI, and bench recommendations. · `recommendation_systems/recommendation_systems.py`

### Serving & LLM

API, chatbot, and prompt generation for human-facing outputs.

- [Main run](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/main.py): orchestrator · Local end-to-end pipeline entry point. · `main.py`
- [API routes](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/end_points.py): http api · Project API surface for running pipeline actions. · `end_points.py`
- [Coach chat](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/chat_bot/app.py): flask app · Standalone chatbot service for interactive analysis. · `chat_bot/app.py`
- [Prompt builder](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/generate_prompt/generate_prompt.py): llm prep · Assembles structured context for Gemini outputs. · `generate_prompt/generate_prompt.py`
- [Gemini utils](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/utils/Gemini_utils.py): llm integration · Shared helpers for Gemini-based generation and chat. · `utils/Gemini_utils.py`
- [Shared utils](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/utils/main_utils.py): helpers · Common utilities for bounding boxes, workflow, and recommendations. · `utils/main_utils.py`

### Data & Assets

Input media, datasets, and generated output folders.

- [Input videos](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/input_videos/Atef.txt): asset · Video inputs for analysis runs. · `input_videos/Atef.txt`
- [Reco data](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/recommendation_systems_input_files/data_cleaned.csv): dataset · Cleaned tabular inputs for recommendation models. · `recommendation_systems_input_files/data_cleaned.csv`
- [CV outputs](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/output_files_computer_vision/Atef.txt): artifact · Computer-vision output files and run artifacts. · `output_files_computer_vision/Atef.txt`
- [Reco outputs](https://github.com/abdelrahmanatef01/tactic_zone/blob/master/output_files_recommendation_systems/Atef.txt): artifact · Recommendation output files and run artifacts. · `output_files_recommendation_systems/Atef.txt`

## Connections

- Main run → Trackers: orchestrates · Main pipeline starts tracking from match video.
- Main run → Camera motion: corrects motion · Compensates broadcast camera movement before analysis.
- Main run → Field view: projects field · Transforms positions into field-aware coordinates.
- Trackers → Team assigner: tracked players · Tracked detections feed team classification.
- Trackers → Ball assigner: tracked ball · Ball and player tracks support ball possession logic.
- Trackers → Number OCR: player crops · Cropped player images support jersey number OCR.
- Field view → Speed stats: coordinates · Perspective-correct positions drive movement metrics.
- Team assigner → Player stats: team labels · Team identity is used while aggregating player output.
- Number OCR → Player stats: identity merge · Shirt numbers help merge repeated player tracks.
- Speed stats → Player stats: movement metrics · Speed and distance are added to player summaries.
- Trackers → Event process: frame state · Event processing consumes tracking and match-state signals.
- Event process → Pass detector: detects · Pass detector extracts passing events.
- Event process → Shot detector: detects · Shot detector extracts shot events.
- Event process → Substitution: detects · Substitution detector extracts substitution events.
- Event process → Goal/line: detects · Goal-line processor handles score-related events.
- Event process → New data: adapts · New data handler normalizes additional event classes.
- Player stats → Team stats: roll up · Player-level summaries feed team-level analytics.
- Formation → Team stats: formation data · Formation inference contributes to team summaries.
- Match state → Team stats: match result · Match state informs team summary outputs.
- Team stats → Recommendations: structured team data · Recommendation engine uses aggregated team inputs.
- Reco data → Recommendations: historical data · Cleaned datasets support recommendation logic.
- Team stats → Prompt builder: summary inputs · Structured analytics are turned into LLM prompt context.
- Prompt builder → Gemini utils: Gemini payload · Prompt content is handed to Gemini helpers.
- Gemini utils → Coach chat: chat responses · Chat app uses Gemini integration for interactive outputs.
- Shared utils → Main run: shared workflow · Utilities support the main execution path.
- Shared utils → Recommendations: shared helpers · Recommendation helpers are reused across the pipeline.
- Input videos → Main run: video input · Main run consumes source match videos.
- Main run → CV outputs: writes · Computer vision pipeline emits artifacts.
- Recommendations → Reco outputs: writes · Recommendation engine emits output artifacts.
- API routes → Main run: invokes · API layer triggers the core pipeline.
- API routes → Recommendations: serves · API can expose recommendations directly.
- Coach chat → Prompt builder: builds context · Chat flow prepares structured prompts from analytics.
