# ilungamulala/ai-driven-traffic-light architecture

> Architecture diagram of the GitHub repository ilungamulala/ai-driven-traffic-light, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ilungamulala/ai-driven-traffic-light
- Repository: https://github.com/ilungamulala/ai-driven-traffic-light

## Overview

This repository implements two traffic-signal inputs: video-based vehicle counting and audio-based emergency-sound classification. The video loop detects and tracks vehicles, counts unique tracks, and sends a signal when the count threshold is exceeded; the audio script records a sample, classifies it, and sends the predicted class to the ESP32. Firmware then controls the LED signals. The README describes broader real-time use, but the sampled video code reads hard-coded video files. Audio and firmware links follow the code and README; their internals were not sampled.

Main entry points: [`main.py`](https://github.com/ilungamulala/ai-driven-traffic-light/blob/main/main.py) (Video processing), [`EmergencySounds.py`](https://github.com/ilungamulala/ai-driven-traffic-light/blob/main/EmergencySounds.py) (Audio classifier flow), [`Simple_ESP32_Code/Simple_ESP32_Code.ino`](https://github.com/ilungamulala/ai-driven-traffic-light/blob/main/Simple_ESP32_Code/Simple_ESP32_Code.ino) (ESP32 firmware), [`yolov8n.pt`](https://github.com/ilungamulala/ai-driven-traffic-light/blob/main/yolov8n.pt) (YOLO weights)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_traffic["Traffic Analysis"]
  node_main["Video processing<br/>[main.py]"]
  node_yolo["YOLO weights<br/>[yolov8n.pt]"]
  node_classes["Class names<br/>[classes.txt]"]
  node_sort["SORT tracker<br/>[sort.py]"]
  node_display["Video display<br/>[main.py]"]
end

subgraph group_audio["Emergency Audio"]
  node_emergency["Audio classifier flow<br/>[EmergencySounds.py]"]
  node_classifier["Siren model"]
end

subgraph group_device["Signal Control"]
  node_esp["ESP32 firmware"]
end

node_operator(("Operator"))
node_video(("Video files"))
node_microphone(("Microphone"))
node_esp_service(("ESP32 controller"))
node_leds(("Traffic LEDs"))

node_operator -->|"starts"| node_main
node_video -->|"provides frames"| node_main
node_main -->|"loads weights"| node_yolo
node_main -->|"reads labels"| node_classes
node_main -->|"updates tracks"| node_sort
node_main -->|"renders frames"| node_display
node_main -->|"sends count code"| node_esp_service
node_operator -->|"starts"| node_emergency
node_microphone -->|"provides audio"| node_emergency
node_emergency -->|"loads and predicts"| node_classifier
node_emergency -->|"sends class ID"| node_esp_service
node_esp_service -->|"receives commands"| node_esp
node_esp -->|"controls signals"| node_leds

click node_main "https://github.com/ilungamulala/ai-driven-traffic-light/blob/main/main.py"
click node_yolo "https://github.com/ilungamulala/ai-driven-traffic-light/blob/main/yolov8n.pt"
click node_classes "https://github.com/ilungamulala/ai-driven-traffic-light/blob/main/classes.txt"
click node_sort "https://github.com/ilungamulala/ai-driven-traffic-light/blob/main/sort.py"
click node_display "https://github.com/ilungamulala/ai-driven-traffic-light/blob/main/main.py"
click node_emergency "https://github.com/ilungamulala/ai-driven-traffic-light/blob/main/EmergencySounds.py"
click node_classifier "https://github.com/ilungamulala/ai-driven-traffic-light/blob/main/siren_sound_classifier.h5"
click node_esp "https://github.com/ilungamulala/ai-driven-traffic-light/blob/main/Simple_ESP32_Code/Simple_ESP32_Code.ino"

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,node_yolo,node_classes,node_sort,node_display toneBlue
class node_emergency,node_classifier toneAmber
class node_esp toneMint
class node_operator,node_video,node_microphone,node_esp_service,node_leds toneIndigo
```

## Components

### Traffic Analysis

- [Video processing](https://github.com/ilungamulala/ai-driven-traffic-light/blob/main/main.py): `main.py`
- [YOLO weights](https://github.com/ilungamulala/ai-driven-traffic-light/blob/main/yolov8n.pt): `yolov8n.pt`
- [Class names](https://github.com/ilungamulala/ai-driven-traffic-light/blob/main/classes.txt): `classes.txt`
- [SORT tracker](https://github.com/ilungamulala/ai-driven-traffic-light/blob/main/sort.py): `sort.py`
- [Video display](https://github.com/ilungamulala/ai-driven-traffic-light/blob/main/main.py): `main.py`

### Emergency Audio

- [Audio classifier flow](https://github.com/ilungamulala/ai-driven-traffic-light/blob/main/EmergencySounds.py): `EmergencySounds.py`
- [Siren model](https://github.com/ilungamulala/ai-driven-traffic-light/blob/main/siren_sound_classifier.h5): `siren_sound_classifier.h5`

### Signal Control

- [ESP32 firmware](https://github.com/ilungamulala/ai-driven-traffic-light/blob/main/Simple_ESP32_Code/Simple_ESP32_Code.ino): `Simple_ESP32_Code/Simple_ESP32_Code.ino`

### Other components

- **Operator**
- **Video files**
- **Microphone**
- **ESP32 controller**
- **Traffic LEDs**

## Connections

- Operator → Video processing: starts
- Video files → Video processing: provides frames
- Video processing → YOLO weights: loads weights
- Video processing → Class names: reads labels
- Video processing → SORT tracker: updates tracks
- Video processing → Video display: renders frames
- Video processing → ESP32 controller: sends count code
- Operator → Audio classifier flow: starts
- Microphone → Audio classifier flow: provides audio
- Audio classifier flow → Siren model: loads and predicts
- Audio classifier flow → ESP32 controller: sends class ID
- ESP32 controller → ESP32 firmware: receives commands
- ESP32 firmware → Traffic LEDs: controls signals
