# cvhub520/x-anylabeling architecture

> Architecture diagram of the GitHub repository cvhub520/x-anylabeling, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-07-06). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/cvhub520/x-anylabeling
- Repository: https://github.com/cvhub520/x-anylabeling

## Overview

##### Project Type and Purpose
- This is a **monolithic desktop application** written in Python.
- It follows an **MVC-style** separation:
- **Views (anylabeling/views):** Qt-based GUI components
- **Controllers/Application Core (anylabeling/app.py):** bootstraps the UI, routes events
- **Services (anylabeling/services):** AI inference engines, model management, business logic
- It integrates numerous AI models (YOLO, SAM, PP-OCR, etc.) for **automatic labeling** of images and videos.

##### Main Components
a. **UI Layer**
– anylabeling/views/**...**: Qt widgets, dialogs, canvases, chatbots, VQA interfaces
– anylabeling/mainwindow.py: top-level window
b. **Application Core / Controller**
– anylabeling/app.py & app_info.py: entry point, global app configuration, command-line arguments
– anylabeling/config.py: loads user preferences, defaults
c. **Service Layer**
– anylabeling/services/auto_labeling: model wrappers, inference pipelines
– anylabeling/services/model_manager.py: selects & instantiates model services based on YAML configs
– anylabeling/services/auto_labeling/engines: builds ONNX/DNN inference engines (GPU/CPU)
– anylabeling/services/auto_labeling/trackers: tracking algorithms (ByteTrack, BotSort)
d. **Configuration & Resources**
– anylabeling/configs/**/*.yaml: model definitions, hyperparameters
e. **Data & Assets**
– assets/: sample images/videos, annotation formats (COCO, MOT, VOC, etc.)
– examples/: use-case templates per task category
f. **Tools & Scripts**
– scripts/*.sh / *.py: build/publish, compile UI translations, format code
– tools/onnx_exporter: convert models to ONNX for inference engines
g. **External Dependencies**
– Python libs: PyQt5/PySide6, ONNXRuntime, PyTorch, OpenCV
– Hardware: GPU (CUDA/CUDNN) or CPU fallback
– Remote APIs: grounding_dino_api.py for cloud-based inference (optional)

##### Interactions & Data Flow
1. **User Action** (UI)
– Opens project / loads images/videos
– Picks a model & clicks “Auto-label”
2. **Controller**
– Receives UI event, reads config (anylabeling/config.py)
– Calls ModelManager to create a service instance
3. **ModelManager**
– Reads YAML (anylabeling/configs/…yaml) to identify model class
– Uses Factory pattern to load the corresponding service (base + engine)
4. **Inference Engine Creation**
– Builds ONNX or DNN engine via build_onnx_engine.py or build_dnn_engine.py
– Initializes runtime (device selection: GPU vs. CPU)
5. **Model Execution**
– Service.run(image/video) → ONNXRuntime / PyTorch model → raw predictions
6. **Results to UI**
– Controller receives predictions, wraps into annotation primitives
7. **Export / Save**
– UI triggers io modules (anylabeling/views/labeling/utils/export.py)
– Outputs in COCO, YOLO, VOC, etc.

##### Architectural Patterns & Principles
- **MVC**: clear separation of Views, Controllers, Services
- **Plugin/Factory**: new models or post-processors can be added by dropping in a YAML + service class
- **Builder**: engine builders for ONNX vs DNN runtime
- **Single Responsibility**: each folder has a well-defined concern (views, services, configs, resources)

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "UI Layer"
        UIViews["UI Layer (Views)"]:::ui
        MainWindow["Main Window"]:::ui
    end

    subgraph "Application Core / Controller"
        App["App Entry Point"]:::controller
        AppInfo["Global App Configuration & Metadata"]:::controller
        ConfigLoad["User Preferences & Defaults Loader"]:::controller
    end

    subgraph "Service Layer"
        ModelMgr["Service Factory (ModelManager)"]:::backend
        BaseClasses["Auto-Labeling Service Base Classes"]:::backend
        Engines["AI Inference Engine Builders (ONNX/DNN)"]:::backend
        Trackers["Tracking Algorithms"]:::backend
    end

    subgraph "Config & Resources"
        YAMLDefs["Model Definitions & Hyperparameters (YAMLs)"]:::config
        GlobalConfigs["Global Configuration Files"]:::config
        Resources["UI Resources (icons, translations)"]:::config
    end

    subgraph "Data & Assets"
        AssetsData["Sample Data & Annotation Assets"]:::data
        Examples["Task-Specific Examples Templates"]:::data
    end

    subgraph "Tools & Scripts"
        Scripts["Build/Publish & Utility Scripts"]:::tools
        ONNXExporter["ONNX Exporter Tools"]:::tools
    end

    subgraph "External Dependencies"
        GPU["GPU/CPU Runtime"]:::external
        RemoteAPI["Remote API (cloud inference)"]:::external
    end

    UIViews -->|"userAction()"| App
    MainWindow -->|loads UI| UIViews
    App -->|"init()"| ConfigLoad
    App -->|"loadMetadata()"| AppInfo
    App -->|"routeEvent()"| ModelMgr
    ConfigLoad --> YAMLDefs
    ModelMgr -->|"readConfig()"| YAMLDefs
    ModelMgr --> BaseClasses
    BaseClasses --> Engines
    BaseClasses --> Trackers
    Engines -->|"buildEngine()"| GPU
    Engines -->|"buildEngine()"| RemoteAPI
    GPU -->|"runtimeInit()"| Engines
    RemoteAPI -->|"apiInit()"| Engines
    Engines -->|"runInference(input)"| ModelMgr
    Trackers -->|"track()"| ModelMgr
    ModelMgr -->|"predictions"| App
    App -->|"render(predictions)"| UIViews
    UIViews -->|"export()"| Exporter["Exporter (IO Modules)"]:::backend
    Exporter -->|"saveFormats"| AssetsData

    click UIViews "https://github.com/cvhub520/x-anylabeling/tree/main/anylabeling/views/"
    click MainWindow "https://github.com/cvhub520/x-anylabeling/blob/main/anylabeling/mainwindow.py"
    click App "https://github.com/cvhub520/x-anylabeling/blob/main/anylabeling/app.py"
    click AppInfo "https://github.com/cvhub520/x-anylabeling/blob/main/anylabeling/app_info.py"
    click ConfigLoad "https://github.com/cvhub520/x-anylabeling/blob/main/anylabeling/config.py"
    click ModelMgr "https://github.com/cvhub520/x-anylabeling/blob/main/anylabeling/services/auto_labeling/model_manager.py"
    click BaseClasses "https://github.com/cvhub520/x-anylabeling/tree/main/anylabeling/services/auto_labeling/__base__/"
    click Engines "https://github.com/cvhub520/x-anylabeling/tree/main/anylabeling/services/auto_labeling/engines/"
    click Trackers "https://github.com/cvhub520/x-anylabeling/tree/main/anylabeling/services/auto_labeling/trackers/"
    click YAMLDefs "https://github.com/cvhub520/x-anylabeling/tree/main/anylabeling/configs/auto_labeling/"
    click GlobalConfigs "https://github.com/cvhub520/x-anylabeling/blob/main/anylabeling/configs/models.yaml"
    click Resources "https://github.com/cvhub520/x-anylabeling/tree/main/anylabeling/resources/"
    click AssetsData "https://github.com/cvhub520/x-anylabeling/tree/main/assets/"
    click Examples "https://github.com/cvhub520/x-anylabeling/tree/main/examples/"
    click Scripts "https://github.com/cvhub520/x-anylabeling/tree/main/scripts/"
    click ONNXExporter "https://github.com/cvhub520/x-anylabeling/tree/main/tools/onnx_exporter/"

    classDef ui fill:#b6fcd5,stroke:#333,stroke-width:1px
    classDef controller fill:#b3d1ff,stroke:#333,stroke-width:1px
    classDef backend fill:#a8c0ff,stroke:#333,stroke-width:1px
    classDef config fill:#fff4b3,stroke:#333,stroke-width:1px
    classDef data fill:#fff4b3,stroke:#333,stroke-width:1px
    classDef tools fill:#ffd1dc,stroke:#333,stroke-width:1px
    classDef external fill:#d3d3d3,stroke:#333,stroke-width:1px
```
