# ngducok/food-classfication architecture

> Architecture diagram of the GitHub repository ngducok/food-classfication, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ngducok/food-classfication
- Repository: https://github.com/ngducok/food-classfication

## Overview

##### Project type and purpose
- This is a Python-based machine learning application for classifying food items in a canteen setting.
- It provides a simple UI (`rielUI.py` or a similar script) that lets end users supply images (upload from disk, webcam, or an ESP32-Cam) and receive food classification results along with price data.
- There is also a training component (`train_kaggle.py`) to re-train or update the model using Kaggle or custom datasets.

##### File structure
- Top-level files:
- `rielmodel.pt`: the serialized PyTorch model used at inference time
- `food_price.json` & `data/food_price.json`: static mapping of food labels → prices
- `train_kaggle.py`: training pipeline for building or updating the model
- `readme.md`, `LICENSE`: documentation and licensing
- Directories:
- `core/`: contains `detector.py`, the central inference logic that loads the model and runs predictions
- `models/`: contains any auxiliary model artifacts or scripts (e.g. `1.lua` indicates possible Lua/Torch compatibility or conversion utilities)
- `data/`: sample data or supporting JSON files
- Utility scripts:
- `lib2.py`: presumably shared helper functions (I/O, image preprocessing, JSON parsing)

##### README for additional insights
- The README describes how to install dependencies, run the user interface, and swap models.
- Notes support for webcam and ESP32-Cam inputs, indicating external-device integration.

##### Main components
a. User Interface (rielUI.py)
- Accepts image input from:
- Local file browser
- Webcam device
- ESP32-Cam (via HTTP or serial)
- Displays classification results + price
b. Core Detector Module (core/detector.py)
- Loads the PyTorch model (`rielmodel.pt`)
- Processes input images (via `lib2.py` helpers)
- Returns class probabilities/labels
c. Data Store (food_price.json)
- Maps class labels → price values
d. Model Artifact (rielmodel.pt)
- Pretrained inference model
e. Training Pipeline (train_kaggle.py)
- Consumes labeled dataset (e.g., Kaggle food images)
- Outputs a new `.pt` model file
f. External Services / Devices
- Webcam driver
- ESP32-Cam HTTP/Socket interface

##### Relationships & interactions
- UI → Detector: forwards captured image
- Detector → Model Artifact: reads `.pt` file into memory
- Detector → Data Store: reads JSON to fetch pricing
- Detector → UI: returns classification + price
- Training Pipeline ↔ Dataset: reads raw images, writes new model
- (Optionally) ESP32-Cam → UI: streams image via HTTP

##### Architectural patterns & design principles
- Separation of concerns: clear split between UI, inference logic, data, and training
- Plug-and-play model swapping: UI and detector aren’t hard-coded to one model file
- External-device abstraction: treat ESP32-Cam input as any other image source
- JSON-backed configuration: easy updates to price lookup without code changes

##### Technologies & frameworks to annotate
- Python 3.X runtime
- PyTorch (inference & training)
- JSON for configuration/data
- Optional: OpenCV or PIL for image capture/preprocessing
- Networking libraries (requests or socket) for ESP32-Cam

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Subgraphs for clarity
    subgraph "User Interface Tier"
        UI["User Interface<br/>(rielUI.py)"]:::frontend
    end

    subgraph "External Devices"
        Webcam["Webcam"]:::external
        ESP["ESP32-Cam"]:::external
    end

    subgraph "Inference Tier"
        Detector["Detector Module<br/>(core/detector.py)"]:::backend
        Helpers["Utility Helpers<br/>(lib2.py)"]:::utility
        Model["Model Artifact<br/>(rielmodel.pt)"]:::model
        PriceLocal["Price Mapping<br/>(food_price.json)"]:::db
        PriceDir["Price Mapping Data<br/>(data/food_price.json)"]:::db
    end

    subgraph "Training Pipeline"
        Dataset["Dataset<br/>(Kaggle/Custom)"]:::external
        Trainer["Training Script<br/>(train_kaggle.py)"]:::training
        NewModel["New Model Artifact<br/>(rielmodel.pt)"]:::model
        Aux["Auxiliary Models<br/>(models/1.lua)"]:::utility
    end

    %% Data flow
    Webcam -->|Image| UI
    ESP -->|Image| UI
    UI -->|Image Input| Detector
    Detector -->|uses| Helpers
    Detector -->|loads model| Model
    Detector -->|reads price| PriceLocal
    Detector -->|reads price| PriceDir
    Detector -->|returns results| UI

    Dataset -->|images| Trainer
    Aux -->|scripts| Trainer
    Trainer -->|outputs| NewModel

    %% Click Events
    click Detector "https://github.com/ngducok/food-classfication/blob/main/core/detector.py"
    click Helpers "https://github.com/ngducok/food-classfication/blob/main/lib2.py"
    click Model "https://github.com/ngducok/food-classfication/blob/main/rielmodel.pt"
    click PriceLocal "https://github.com/ngducok/food-classfication/blob/main/food_price.json"
    click PriceDir "https://github.com/ngducok/food-classfication/blob/main/data/food_price.json"
    click Trainer "https://github.com/ngducok/food-classfication/blob/main/train_kaggle.py"
    click Aux "https://github.com/ngducok/food-classfication/blob/main/models/1.lua"
    click NewModel "https://github.com/ngducok/food-classfication/blob/main/rielmodel.pt"

    %% Styles
    classDef frontend fill:#cdeaff,stroke:#0294a5,stroke-width:2px;
    classDef backend fill:#ffe0b3,stroke:#e69900,stroke-width:2px;
    classDef db fill:#d5e8d4,stroke:#82b366,stroke-width:2px;
    classDef utility fill:#f2f2f2,stroke:#999999,stroke-width:1px;
    classDef model fill:#ffcccc,stroke:#cc0000,stroke-width:2px;
    classDef training fill:#e1d5e7,stroke:#9673b9,stroke-width:2px;
    classDef external fill:#f8cecc,stroke:#b85450,stroke-width:2px;
```
