# xennonite/ai-smart-parking architecture

> Architecture diagram of the GitHub repository xennonite/ai-smart-parking, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/xennonite/ai-smart-parking
- Repository: https://github.com/xennonite/ai-smart-parking

## Overview

#### Purpose and Entry Points
This repository implements a camera-based smart-parking prototype: detect vehicles from periodic ESP32-CAM images, map detections to configured parking polygons, and send occupancy state to an entrance ESP32. The production control-loop entry point is `AI_Vision_System/master_system.py`. Setup workflows are `AI_Vision_System/setup_slots.py` and `AI_Vision_System/capture_high_res.py`; embedded gate/display firmware is `esp32.cpp`.

#### Camera Data Acquisition
The vision server obtains high-resolution still images from the ESP32-CAM over HTTP rather than consuming a continuous stream. `AI_Vision_System/capture_high_res.py` supports collecting site-specific training images, while `AI_Vision_System/master_system.py` consumes camera images during runtime. The camera is an external ESP32-CAM running a camera web server and reachable on the local network.

#### Slot Configuration Boundary
Parking geometry is a site-specific configuration boundary. `AI_Vision_System/setup_slots.py` presents camera imagery for an operator to click each bay’s corners and persists those polygons in `AI_Vision_System/parking_data.json`. Runtime logic treats this JSON map as the authoritative inventory and avoids any physical sensor dependency per slot.

#### Vision Inference
The central Python process loads custom YOLOv8 vehicle-detection weights from `AI_Vision_System/best.pt` and runs inference on each fetched frame in `AI_Vision_System/master_system.py`. Model training is external to the runtime repository: images are annotated through Roboflow and trained with Ultralytics YOLOv8, typically using GPU-backed Google Colab.

#### Occupancy Resolution
`AI_Vision_System/master_system.py` converts vehicle bounding boxes into center pixels, then evaluates each center against each configured slot polygon using OpenCV point-in-polygon/ray-casting behavior. This translates model detections into an ordered binary slot-state array, such as occupied versus available, which is the core domain output.

#### Hardware Command Path
The Python server serializes and sends occupancy state through local-network HTTP requests to the entrance controller. `esp32.cpp` is the secondary ESP32 firmware boundary: it receives server updates and integrates physical I/O for the IR-triggered barrier servo and I2C LCD guidance display. The controller owns real-world actuation; Python owns camera interpretation and parking-state decisions.

#### Runtime and Deployment Shape
The prototype spans Python/OpenCV/Ultralytics on a central vision host and C++ firmware on two ESP32-class devices. JSON carries slot geometry and state-oriented payloads; HTTP REST and local Wi-Fi/mDNS provide device connectivity. The camera endpoint can be replaced by RTSP/CCTV sources, and the inference stage can move to a centralized GPU server processing multiple camera feeds.

Main entry points: [`AI_Vision_System/master_system.py`](https://github.com/xennonite/ai-smart-parking/blob/main/AI_Vision_System/master_system.py) (Master system), [`esp32.cpp`](https://github.com/xennonite/ai-smart-parking/blob/main/esp32.cpp) (Entrance controller firmware), [`AI_Vision_System/setup_slots.py`](https://github.com/xennonite/ai-smart-parking/blob/main/AI_Vision_System/setup_slots.py) (Slot setup), [`AI_Vision_System/capture_high_res.py`](https://github.com/xennonite/ai-smart-parking/blob/main/AI_Vision_System/capture_high_res.py) (Training capture)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_vision["Vision host"]
  node_master["Master system<br/>Python control loop<br/>[master_system.py]"]
  node_runtime_camera_client["Camera image fetch<br/>HTTP client<br/>[master_system.py]"]
  node_yolo_inference["Vehicle inference<br/>YOLOv8 inference<br/>[master_system.py]"]
  node_occupancy_resolution["Occupancy resolution<br/>polygon mapping<br/>[master_system.py]"]
  node_slot_setup["Slot setup<br/>Python operator tool<br/>[setup_slots.py]"]
  node_training_capture["Training capture<br/>Python capture tool"]
  node_parking_data["Parking polygons<br/>JSON configuration<br/>[parking_data.json]"]
  node_model_weights["Custom YOLO weights<br/>model artifact<br/>[best.pt]"]
end

subgraph group_devices["ESP32 devices"]
  node_camera["ESP32-CAM<br/>camera web server"]
  node_entrance_firmware["Entrance controller firmware<br/>ESP32 C++ firmware<br/>[esp32.cpp]"]
  node_barrier["Barrier servo<br/>physical actuator"]
  node_lcd["Guidance LCD<br/>I2C display"]
end

subgraph group_external["External ML workflow"]
  node_roboflow["Roboflow annotation<br/>annotation service"]
  node_colab_training["Ultralytics training<br/>GPU training workflow"]
end

node_camera -->|"HTTP still images"| node_runtime_camera_client
node_runtime_camera_client -->|"frames"| node_master
node_master -->|"frame"| node_yolo_inference
node_model_weights -->|"loads"| node_yolo_inference
node_yolo_inference -->|"vehicle boxes"| node_occupancy_resolution
node_parking_data -->|"slot polygons"| node_occupancy_resolution
node_occupancy_resolution -->|"binary slot states"| node_master
node_master -->|"HTTP occupancy update"| node_entrance_firmware
node_entrance_firmware -->|"servo control"| node_barrier
node_entrance_firmware -->|"I2C guidance"| node_lcd
node_camera -->|"camera imagery"| node_slot_setup
node_slot_setup -->|"persists polygons"| node_parking_data
node_camera -->|"high-resolution images"| node_training_capture
node_training_capture -->|"training images"| node_roboflow
node_roboflow -->|"annotated dataset"| node_colab_training
node_colab_training -->|"trained weights"| node_model_weights

click node_master "https://github.com/xennonite/ai-smart-parking/blob/main/AI_Vision_System/master_system.py"
click node_runtime_camera_client "https://github.com/xennonite/ai-smart-parking/blob/main/AI_Vision_System/master_system.py"
click node_yolo_inference "https://github.com/xennonite/ai-smart-parking/blob/main/AI_Vision_System/master_system.py"
click node_occupancy_resolution "https://github.com/xennonite/ai-smart-parking/blob/main/AI_Vision_System/master_system.py"
click node_slot_setup "https://github.com/xennonite/ai-smart-parking/blob/main/AI_Vision_System/setup_slots.py"
click node_training_capture "https://github.com/xennonite/ai-smart-parking/blob/main/AI_Vision_System/capture_high_res.py"
click node_parking_data "https://github.com/xennonite/ai-smart-parking/blob/main/AI_Vision_System/parking_data.json"
click node_model_weights "https://github.com/xennonite/ai-smart-parking/blob/main/AI_Vision_System/best.pt"
click node_entrance_firmware "https://github.com/xennonite/ai-smart-parking/blob/main/esp32.cpp"

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_master,node_runtime_camera_client,node_yolo_inference,node_occupancy_resolution,node_slot_setup,node_training_capture,node_parking_data,node_model_weights toneBlue
class node_camera,node_entrance_firmware,node_barrier,node_lcd toneAmber
class node_roboflow,node_colab_training toneMint
```

## Components

### Vision host

Python control loop and site setup tools.

- [Master system](https://github.com/xennonite/ai-smart-parking/blob/main/AI_Vision_System/master_system.py): Python control loop · Fetches frames, detects vehicles, resolves occupancy, and sends updates. · `AI_Vision_System/master_system.py`
- [Camera image fetch](https://github.com/xennonite/ai-smart-parking/blob/main/AI_Vision_System/master_system.py): HTTP client · Obtains periodic high-resolution still images. · `AI_Vision_System/master_system.py`
- [Vehicle inference](https://github.com/xennonite/ai-smart-parking/blob/main/AI_Vision_System/master_system.py): YOLOv8 inference · Runs custom vehicle detection on each frame. · `AI_Vision_System/master_system.py`
- [Occupancy resolution](https://github.com/xennonite/ai-smart-parking/blob/main/AI_Vision_System/master_system.py): polygon mapping · Maps bounding-box centers to ordered binary slot states. · `AI_Vision_System/master_system.py`
- [Slot setup](https://github.com/xennonite/ai-smart-parking/blob/main/AI_Vision_System/setup_slots.py): Python operator tool · Captures operator-selected parking-bay polygons. · `AI_Vision_System/setup_slots.py`
- [Training capture](https://github.com/xennonite/ai-smart-parking/blob/main/AI_Vision_System/capture_high_res.py): Python capture tool · Collects site-specific high-resolution images. · `AI_Vision_System/capture_high_res.py`
- [Parking polygons](https://github.com/xennonite/ai-smart-parking/blob/main/AI_Vision_System/parking_data.json): JSON configuration · Authoritative site parking inventory. · `AI_Vision_System/parking_data.json`
- [Custom YOLO weights](https://github.com/xennonite/ai-smart-parking/blob/main/AI_Vision_System/best.pt): model artifact · Vehicle-detection weights loaded by runtime. · `AI_Vision_System/best.pt`

### ESP32 devices

Networked camera and entrance controller.

- **ESP32-CAM**: camera web server · Serves still images over local-network HTTP.
- [Entrance controller firmware](https://github.com/xennonite/ai-smart-parking/blob/main/esp32.cpp): ESP32 C++ firmware · Receives occupancy updates and controls physical I/O. · `esp32.cpp`
- **Barrier servo**: physical actuator · IR-triggered entrance barrier.
- **Guidance LCD**: I2C display · Displays parking guidance.

### External ML workflow

Model preparation occurs outside runtime.

- **Roboflow annotation**: annotation service · Annotates collected training imagery.
- **Ultralytics training**: GPU training workflow · Trains YOLOv8 weights externally.

## Connections

- ESP32-CAM → Camera image fetch: HTTP still images
- Camera image fetch → Master system: frames
- Master system → Vehicle inference: frame
- Custom YOLO weights → Vehicle inference: loads
- Vehicle inference → Occupancy resolution: vehicle boxes
- Parking polygons → Occupancy resolution: slot polygons
- Occupancy resolution → Master system: binary slot states
- Master system → Entrance controller firmware: HTTP occupancy update
- Entrance controller firmware → Barrier servo: servo control
- Entrance controller firmware → Guidance LCD: I2C guidance
- ESP32-CAM → Slot setup: camera imagery
- Slot setup → Parking polygons: persists polygons
- ESP32-CAM → Training capture: high-resolution images
- Training capture → Roboflow annotation: training images
- Roboflow annotation → Ultralytics training: annotated dataset
- Ultralytics training → Custom YOLO weights: trained weights
