# tomatoft/image-captioning architecture

> Architecture diagram of the GitHub repository tomatoft/image-captioning, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-02-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tomatoft/image-captioning
- Repository: https://github.com/tomatoft/image-captioning

## Overview

##### Main Components
- Frontend/UI Layer: Represented by the Streamlit interface. The primary file is web.py, which launches the demo.
- Image Captioning Model: Encapsulated within the model directory. This includes the pre-trained weights (model_IC.h5), caption generation script (generate_caption.py), and potentially the transformer and CNN modules (transformer_cnn.py).
- Data Storage: PostgreSQL integration as noted in the README. Indicate using a “Database” component.
- Image Assets & Datasets: Indicate the Flicker8k_Dataset directory and the tested_image directory. Also include the Upload folder where extra input files may be stored.
- Configuration & Secrets: The .streamlit directory (specifically secrets.toml) holds the database credentials.
- Supporting Materials: Although not part of the run-time architecture, note the presence of instructional/report documents (README.md and the submitted report PDF) that may be referenced for further context.

##### Relationships and Interactions
- Client/User initiates interaction with the application via the Streamlit frontend.
- The frontend (web.py) sends requests to invoke the model’s functionality for captioning images.
- The image captioning model, using TensorFlow, processes the input image – by first extracting features (using EfficientNet or a similar CNN) and then passing them to a Transformer-based module for natural language generation.
- Configuration files (from .streamlit) provide a secure connection set up, especially for the database interactions.

##### Architectural Patterns and Design Principles
- Layered Architecture: Illustrate layers – a presentation layer (Streamlit), a business/data processing layer (image captioning with TensorFlow), and a persistence layer (PostgreSQL).
- Modularity: The model module is self-contained (with its own directory and cache) indicating that image processing and caption generation are modularized from the rest of the application.
- External Integrations: Show how external tutorials and resources (such as installation guides and the original model tutorial) have influenced the configuration, even though they’re not runtime modules.

##### Technologies and Frameworks
- Frontend: Streamlit (web.py, .streamlit/secrets.toml).
- ML/Backend: TensorFlow, EfficientNet, Transformer (all in the model directory).
- Database: PostgreSQL.
- Development Tools: Anaconda, Visual Studio Code, and Google Colab (as a development/training note).
- Optionally, note the integration with external video tutorials for setup which hints at deployment best practices.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Entities
    User["User/Client"]:::user

    %% Frontend Layer Subgraph
    subgraph "Frontend Layer"
        direction TB
        UI["Streamlit Frontend"]:::frontend
        Config["Configuration"]:::frontend
    end

    %% Machine Learning / Image Captioning Model Subgraph
    subgraph "Image Captioning Model"
        direction TB
        GC["Caption Generator"]:::ml
        TC["Transformer CNN"]:::ml
        MW["Model Weights"]:::ml
        GC -->|"uses"| TC
        GC -->|"loads"| MW
        %% Return caption flow from ML back to Frontend
        GC ---|"return_caption"| UI
    end

    %% Database Component
    DB["PostgreSQL Database"]:::database

    %% Assets & Datasets Subgraph
    subgraph "Image Assets & Datasets"
        direction TB
        F8["Flicker8k_Dataset"]:::assets
        TI["Tested Images"]:::assets
        UP["Upload Directory"]:::assets
    end

    %% Supporting Materials Subgraph
    subgraph "Supporting Materials"
        direction TB
        README["README"]:::support
        Report["Submitted Report"]:::support
    end

    %% Connections between layers
    User -->|"upload_image"| UI
    UI -->|"invoke_captioning"| GC
    UI -->|"store_metadata"| DB
    Config -->|"provides_credentials"| DB
    F8 -->|"provides_data"| GC
    TI -->|"provides_data"| GC
    UP -->|"provides_data"| GC

    %% Click Events
    click UI "https://github.com/tomatoft/image-captioning/blob/master/web.py"
    click Config "https://github.com/tomatoft/image-captioning/blob/master/.streamlit/secrets.toml"
    click GC "https://github.com/tomatoft/image-captioning/blob/master/model/generate_caption.py"
    click MW "https://github.com/tomatoft/image-captioning/blob/master/model/model_IC.h5"
    click TC "https://github.com/tomatoft/image-captioning/blob/master/model/transformer_cnn.py"
    click F8 "https://github.com/tomatoft/image-captioning/tree/master/Flicker8k_Dataset"
    click TI "https://github.com/tomatoft/image-captioning/tree/master/tested_image"
    click UP "https://github.com/tomatoft/image-captioning/tree/master/Upload"
    click README "https://github.com/tomatoft/image-captioning/blob/master/README.md"
    click Report "https://github.com/tomatoft/image-captioning/blob/master/Tan_Chau.pdf"

    %% Styles
    classDef user fill:#f9c,stroke:#333,stroke-width:2px;
    classDef frontend fill:#bbf,stroke:#333,stroke-width:2px;
    classDef ml fill:#bfb,stroke:#333,stroke-width:2px;
    classDef database fill:#fbb,stroke:#333,stroke-width:2px;
    classDef assets fill:#dfd,stroke:#333,stroke-width:2px;
    classDef support fill:#ffd,stroke:#333,stroke-width:2px;
```
