# farzadnekouee/yolov8_traffic_density_estimation architecture

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

- Interactive diagram: https://gitdiagram.com/farzadnekouee/yolov8_traffic_density_estimation
- Repository: https://github.com/farzadnekouee/yolov8_traffic_density_estimation

## Overview

1. **Project shape**
   - This repository is a small end-to-end computer vision ML project centered on **vehicle detection and traffic density estimation** using **YOLOv8**.
   - It has two main execution paths: an **experimental/training notebook** and a **runtime inference script** for local video analysis.

2. **Core architecture**
   - The system takes **aerial/top-view traffic images or video frames** as input.
   - A fine-tuned YOLOv8 model detects vehicles in each frame.
   - The application then **counts detected vehicles** and uses that count as a proxy for **traffic density / intensity**.

3. **Training and experimentation boundary**
   - `real-time_traffic_density_estimation_yolov8.ipynb` is the model development workspace.
   - It covers the pipeline described in the README: dataset preparation, augmentation, training/fine-tuning, evaluation, and inference examples.
   - This notebook is the primary place where model quality is established before deployment.

4. **Deployment/runtime boundary**
   - `real_time_traffic_analysis.py` is the operational script for running the trained detector on a video source.
   - Its role is narrower than the notebook: load the exported model, process frames, count vehicles, and display the live analysis window.
   - This is the boundary between offline ML development and real-time/local inference.

5. **Model artifacts**
   - `models/best.pt` is the trained **PyTorch YOLOv8** checkpoint used as the main fine-tuned model artifact.
   - `models/best.onnx` is the exported **ONNX** version for broader runtime compatibility.
   - These artifacts represent the productionized output of the notebook pipeline.

6. **Data flow**
   - The training side uses a curated **top-view vehicle dataset** of images annotated in YOLO format.
   - The runtime side uses `sample_video.mp4` as a representative video input for traffic analysis.
   - Frames from the video are fed through the detector, and detections are converted into a count/density signal for display.

7. **Dataset and supervision model**
   - The dataset is single-class: **Vehicle**.
   - The problem is framed as object detection rather than classification, because the system needs to localize multiple vehicles in each frame.
   - The README indicates the dataset includes cars, trucks, and buses in top-view scenes, which is important because the model is specialized for **aerial traffic views**, not generic street-level imagery.

8. **Evaluation and validation**
   - The notebook-based workflow emphasizes model assessment through **learning curves, confusion matrices, and performance metrics**.
   - Validation images and an unseen test image/video are used to confirm generalization before runtime use.
   - This means the project is not just a demo; it includes a validation layer between training and deployment.

9. **Inference output and user-visible behavior**
   - During runtime, the primary output is a video window showing detected vehicles and traffic analysis in real time.
   - The system is intended for **local execution**, not a hosted service or API.
   - User interaction is minimal: run the script, view results, exit with a keypress.

10. **External dependencies and ecosystem**
   - The project is built around the **Ultralytics YOLOv8** ecosystem and standard Python ML tooling.
   - ONNX export is a notable architectural choice because it separates model training format from deployment format and supports portability.

11. **Repository contents as architecture signals**
   - `images/` holds visual documentation assets, not core logic.
   - `sample_video.mp4` is a test fixture for validating the inference pipeline.
   - The repository is therefore organized around **artifact-based ML delivery**: notebook → trained weights → exported model → runtime script.

12. **How to draw the architecture**
   - A useful diagram would show three layers:
     - **Data layer:** curated vehicle dataset and sample video
     - **Modeling layer:** notebook for training/evaluation and model export
     - **Inference layer:** Python script consuming the trained model on video frames
   - The key flow is from curated annotated images to fine-tuned YOLOv8 weights, then to ONNX/PyTorch model artifacts, then to live traffic density estimation on video.

Main entry points: [`real-time_traffic_density_estimation_yolov8.ipynb`](https://github.com/farzadnekouee/yolov8_traffic_density_estimation/blob/master/real-time_traffic_density_estimation_yolov8.ipynb) (Training notebook), [`real_time_traffic_analysis.py`](https://github.com/farzadnekouee/yolov8_traffic_density_estimation/blob/master/real_time_traffic_analysis.py) (Traffic analysis), [`README.md`](https://github.com/farzadnekouee/yolov8_traffic_density_estimation/blob/master/README.md) (README), [`models/best.pt`](https://github.com/farzadnekouee/yolov8_traffic_density_estimation/blob/master/models/best.pt) (YOLO weights)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Data"]
  node_vehicle_dataset[("Vehicle data<br/>annotated dataset")]
  node_sample_video["Sample video<br/>video input<br/>[sample_video.mp4]"]
end

subgraph group_modeling["Modeling"]
  node_notebook["Training notebook<br/>experimentation"]
  node_eval_views["Eval views<br/>validation"]
  node_export_step["Export step<br/>model export"]
end

subgraph group_runtime["Runtime"]
  node_runtime_script["Traffic analysis<br/>inference script"]
  node_frame_pipeline["Frame pipeline<br/>video processing"]
  node_display["Live window<br/>visual output"]
end

subgraph group_artifacts["Artifacts"]
  node_best_pt["YOLO weights<br/>torch checkpoint<br/>[best.pt]"]
  node_best_onnx["ONNX model<br/>portable runtime<br/>[best.onnx]"]
end

subgraph group_docs["Docs"]
  node_images["Images<br/>documentation assets"]
  node_readme["README<br/>project overview<br/>[README.md]"]
end

node_vehicle_dataset -->|"train"| node_notebook
node_notebook -->|"validate"| node_eval_views
node_notebook -->|"export"| node_export_step
node_export_step -->|"save"| node_best_pt
node_export_step -->|"convert"| node_best_onnx
node_best_onnx -->|"load"| node_runtime_script
node_sample_video -->|"input"| node_runtime_script
node_runtime_script -->|"process"| node_frame_pipeline
node_frame_pipeline -->|"render"| node_display
node_best_pt -->|"refine"| node_notebook
node_readme -.->|"describe"| node_notebook
node_readme -.->|"describe"| node_runtime_script
node_images -.->|"illustrate"| node_readme

click node_sample_video "https://github.com/farzadnekouee/yolov8_traffic_density_estimation/blob/master/sample_video.mp4"
click node_notebook "https://github.com/farzadnekouee/yolov8_traffic_density_estimation/blob/master/real-time_traffic_density_estimation_yolov8.ipynb"
click node_best_pt "https://github.com/farzadnekouee/yolov8_traffic_density_estimation/blob/master/models/best.pt"
click node_best_onnx "https://github.com/farzadnekouee/yolov8_traffic_density_estimation/blob/master/models/best.onnx"
click node_runtime_script "https://github.com/farzadnekouee/yolov8_traffic_density_estimation/blob/master/real_time_traffic_analysis.py"
click node_images "https://github.com/farzadnekouee/yolov8_traffic_density_estimation/tree/master/images"
click node_readme "https://github.com/farzadnekouee/yolov8_traffic_density_estimation/blob/master/README.md"

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_vehicle_dataset,node_sample_video toneBlue
class node_notebook,node_eval_views,node_export_step toneAmber
class node_runtime_script,node_frame_pipeline,node_display toneMint
class node_best_pt,node_best_onnx toneRose
class node_images,node_readme toneIndigo
```

## Components

### Data

Training and runtime inputs

- **Vehicle data**: annotated dataset · Top-view vehicle images labeled in YOLO format for training and validation.
- [Sample video](https://github.com/farzadnekouee/yolov8_traffic_density_estimation/blob/master/sample_video.mp4): video input · Representative local video used to exercise the inference pipeline. · `sample_video.mp4`

### Modeling

Notebook-based experiment and export workflow

- [Training notebook](https://github.com/farzadnekouee/yolov8_traffic_density_estimation/blob/master/real-time_traffic_density_estimation_yolov8.ipynb): experimentation · End-to-end workspace for preparation, augmentation, fine-tuning, evaluation, and example inference. · `real-time_traffic_density_estimation_yolov8.ipynb`
- **Eval views**: validation · Learning curves, confusion matrices, and test examples used to assess model quality.
- **Export step**: model export · Produces deployable model formats from the trained YOLOv8 checkpoint.

### Runtime

Local video inference and display

- [Traffic analysis](https://github.com/farzadnekouee/yolov8_traffic_density_estimation/blob/master/real_time_traffic_analysis.py): inference script · Loads the exported model, processes video frames, counts vehicles, and displays results locally. · `real_time_traffic_analysis.py`
- **Frame pipeline**: video processing · Reads frames, runs detection, and turns detections into a traffic density signal.
- **Live window**: visual output · Shows detected vehicles and traffic intensity during local execution.

### Artifacts

Trained checkpoints and exported model

- [YOLO weights](https://github.com/farzadnekouee/yolov8_traffic_density_estimation/blob/master/models/best.pt): torch checkpoint · Fine-tuned PyTorch model used as the primary trained artifact. · `models/best.pt`
- [ONNX model](https://github.com/farzadnekouee/yolov8_traffic_density_estimation/blob/master/models/best.onnx): portable runtime · Exported ONNX version for broader inference compatibility. · `models/best.onnx`

### Docs

Repository documentation and visuals

- [Images](https://github.com/farzadnekouee/yolov8_traffic_density_estimation/tree/master/images): documentation assets · Visual assets supporting the README and project explanation. · `images`
- [README](https://github.com/farzadnekouee/yolov8_traffic_density_estimation/blob/master/README.md): project overview · Narrative description of the dataset, training workflow, and inference usage. · `README.md`

## Connections

- Vehicle data → Training notebook: train · Annotated vehicle images feed the model development workflow.
- Training notebook → Eval views: validate · The notebook produces evaluation evidence and performance diagnostics.
- Training notebook → Export step: export · Trained model is converted into deployment-ready formats.
- Export step → YOLO weights: save · Exported workflow yields the main PyTorch checkpoint.
- Export step → ONNX model: convert · Exported workflow also produces an ONNX runtime artifact.
- ONNX model → Traffic analysis: load · The runtime script consumes the portable model artifact.
- Sample video → Traffic analysis: input · Video frames provide the local traffic stream for analysis.
- Traffic analysis → Frame pipeline: process · The script orchestrates frame-level detection and counting.
- Frame pipeline → Live window: render · Detected vehicles and density estimates are shown in a live window.
- YOLO weights → Training notebook: refine · The trained checkpoint is the primary output of experimentation.
- README → Training notebook: describe · Repository documentation explains the training and evaluation flow.
- README → Traffic analysis: describe · Documentation also covers local inference usage.
- Images → README: illustrate · Visual assets support the project overview and results narrative.
