# farizptr/building-detector architecture

> Architecture diagram of the GitHub repository farizptr/building-detector, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/farizptr/building-detector
- Repository: https://github.com/farizptr/building-detector

## Overview

##### Main Components
- Model Loader & Manager: Represented by functions in modules like detection.py and polygon_detection.py (load_model). This component is responsible for loading the YOLOv8 model (i.e., best.pt).
- Detection Modules:
- Image Detection: Implements detection on single images (detect_buildings, visualize_detections).
- Tile Processing: Processes multiple image tiles in a folder (process_tiles from detection.py).
- Polygon-Based Detection: Handles detection within a GeoJSON polygon (functions in polygon_detection.py).
- GeoJSON & Utility Handlers: Include geojson_utils.py, tile_utils.py, and even building_export.py which may assist in preparing GeoJSON data or visualizing areas.
- Validation & Export Components: Functions in validate.py and building_validation_map.html file suggest checking and exporting validation maps for the building detections.
- Data Storage & Output: Input GeoJSON files (example_area.geojson, padang.geojson, buildings.geojson) and output items like detection_results.json, visualization.png.

##### Relationships and Interactions
- Data Flow:
- The process begins with the model loader loading the YOLOv8 model.
- The input images (or tiles) and GeoJSON files are fed into the detection pipelines (image, tiles, or polygon-based).
- Results are passed onto the visualization module which creates annotated images, comprehensive maps, and saving detection data (e.g., JSON).
- That both detection modules depend on the model loader.
- That results generated from detection modules are consumed by the visualization components, and optionally, by the validation routines.
- Command-Line Interface (CLI):
- Although not a "component" in the service architecture, include an element representing the CLI commands that trigger these workflows.

##### Architectural Patterns and Design Principles
- Modular Design: Each .py file encapsulates a specific functionality (detection, visualization, validating, handling geospatial inputs), which is typical in well-separated modular tools.
- Reusability: Utility modules (tile_utils.py and geojson_utils.py) are used across multiple detection modes.
- Pipeline Flow: The data flows from input processing, through detection, to visualization and export.

##### Final Recommendations
- Notes that showcase design patterns like modular separation and pipeline flow.

## Diagram (Mermaid)

```mermaid
flowchart TD
    CLI["Command Line Interface (CLI)"]:::cli
    INPUT["Input Files (Images and GeoJSON)"]:::input
    MODEL["Model Loader (YOLOv8)"]:::loader

    subgraph "Detection Pipelines"
        IMG["Image Detection Pipeline"]:::detection
        TILE["Tile Processing Pipeline"]:::detection
        POLY["Polygon Detection Pipeline"]:::detection
    end

    UTIL["GeoJSON & Utility Module"]:::utility
    VIZ["Visualization & Export"]:::viz
    VALID["Validation/Export Endpoint"]:::validation
    STORAGE["Data Storage & Output Files"]:::storage

    CLI -->|"triggers"| INPUT
    CLI -->|"initiates"| MODEL
    INPUT -->|"raw_data"| IMG
    INPUT -->|"raw_data"| TILE
    INPUT -->|"raw_data"| POLY
    INPUT -->|"raw_data"| UTIL
    MODEL -->|"provides_model"| IMG
    MODEL -->|"provides_model"| TILE
    MODEL -->|"provides_model"| POLY
    IMG -->|"sends_results"| VIZ
    TILE -->|"sends_results"| VIZ
    POLY -->|"sends_results"| VIZ
    UTIL -->|"supplies_geo_data"| VIZ
    VIZ -->|"creates_validation"| VALID
    VIZ -->|"saves_output"| STORAGE

    click MODEL "https://github.com/farizptr/building-detector/blob/main/detection.py"
    click MODEL "https://github.com/farizptr/building-detector/blob/main/polygon_detection.py"

    click IMG "https://github.com/farizptr/building-detector/blob/main/detection.py"
    click TILE "https://github.com/farizptr/building-detector/blob/main/detection.py"
    click POLY "https://github.com/farizptr/building-detector/blob/main/polygon_detection.py"

    click UTIL "https://github.com/farizptr/building-detector/blob/main/geojson_utils.py"
    click UTIL "https://github.com/farizptr/building-detector/blob/main/tile_utils.py"
    click UTIL "https://github.com/farizptr/building-detector/blob/main/building_export.py"

    click VIZ "https://github.com/farizptr/building-detector/blob/main/visualization.py"

    click VALID "https://github.com/farizptr/building-detector/blob/main/validate.py"
    click VALID "https://github.com/farizptr/building-detector/blob/main/building_validation_map.html"

    click STORAGE "https://github.com/farizptr/building-detector/blob/main/buildings.geojson"
    click STORAGE "https://github.com/farizptr/building-detector/blob/main/padang.geojson"

    classDef cli fill:#ffccff,stroke:#333,stroke-width:2px;
    classDef input fill:#cce6ff,stroke:#333,stroke-width:2px;
    classDef loader fill:#ffebcc,stroke:#333,stroke-width:2px;
    classDef detection fill:#ccffcc,stroke:#333,stroke-width:2px;
    classDef utility fill:#ccffff,stroke:#333,stroke-width:2px;
    classDef viz fill:#ffffcc,stroke:#333,stroke-width:2px;
    classDef validation fill:#ffcccc,stroke:#333,stroke-width:2px;
    classDef storage fill:#cce6ff,stroke:#333,stroke-width:2px;
```
