# whereitswrong/smart_plate_project architecture

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

- Interactive diagram: https://gitdiagram.com/whereitswrong/smart_plate_project
- Repository: https://github.com/whereitswrong/smart_plate_project

## Overview

##### Project type and purpose
- From the README (“한이음 2022 공모전 Helth-fit 프로젝트”) and the file tree, this is a cross-platform health-fitness system combining:
- Embedded/IoT (Arduino sketches for AT command checking and calibration)
- Mobile (two Android apps: “Main” for core user experience including object detection and server communication, and “beacontry” for BLE beacon scanning)
- Machine Learning (YOLOv5 codebase for model training/export, with TorchScript models bundled into the “Main” app)

##### File structure and key directories
- Arduino/: sensor/actuator firmware for device-side data acquisition
- Main/: Android Studio application
- app/src/main/java/com/example/main: Activities and Fragments (CameraX integration, object detection, login/signup, network API via RetrofitClient/ServiceApi)
- app/src/main/assets: TorchScript model files (.ptl), class definitions, HTML templates for SNS integration
- beacontry/: lightweight Android app for BLE beacon discovery (BeaconScan.java, PermissionSupport.java)
- yolov5/: full YOLOv5 Python repository used offline for model training, evaluation, exporting to TorchScript/ONNX, plus utility scripts and a Flask REST API example for inference

##### README for additional insights
- No detailed architecture description, but the presence of RetrofitClient/ServiceApi implies a backend REST service to which the “Main” app reports or fetches data (e.g., user login/signup, result upload).

##### Main components
a. IoT Devices (Arduino)
– ATcommandCheck module
– Calibration module
b. Mobile Layer
– “beacontry” BLE Scanner App
– “Main” Android App
- CameraX subsystem
- PrePostProcessor & TorchScript Model Inference
- UI Fragments (Food, Record, Communication)
- RetrofitClient → ServiceApi (REST API)
c. Backend Service
– RESTful API endpoints for authentication (LoginData/LoginResponse, JoinData/JoinResponse)
– Data storage (user profiles, detection records, SNS posts)
d. ML Pipeline (Offline)
– YOLOv5 training scripts (train.py, classify/train.py)
– Model export (export.py → TorchScript, ONNX)
– (Optional) Flask REST API example for cloud inference
e. External Services
– SNS integration (HTML templates for SNS main/upload/write)
– (Optional) Cloud hosting for Flask API or other services

##### Relationships and interactions
- Arduino → BLE → beacontry App (alarms, presence detection)
- User uses “Main” App:
- Camera feed → PrePostProcessor → TorchScript Model → ResultView
- User data (Login/Signup) & detection results → RetrofitClient → Backend REST API
- Optionally, SNS HTML pages served/rendered locally or via backend
- ML pipeline (offline) produces model artifacts → copied into Main/app/src/main/assets

##### Architectural patterns & design principles
- Modular separation:
- Firmware (Arduino)
- Mobile Presentation Layer (Activities/Fragments)
- Domain Layer (PrePostProcessor, object detection pipeline)
- Data Layer (RetrofitClient/ServiceApi)
- Use of TorchScript for embedding ML in mobile (no runtime Python dependency)
- BLE beacon scanning as a separate micro-app for decoupling presence detection
- Offline/Online dichotomy: offline training vs. on-device inference + online backend communication

##### Final recommendations
- Be exhaustive: break “Main” app into UI, processing, networking, and assets.
- Both the development pipeline (YOLOv5 → export → assets) and the runtime flow.
- Clear labels on all ports/interfaces (e.g., BLE, REST, file assets).

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Layer 1: IoT Devices
    subgraph "IoT Devices"
        direction TB
        AT["ATcommandCheck Module"]:::hardware
        CAL["Calibration Module"]:::hardware
    end

    %% Layer 2: Mobile Apps
    subgraph "Mobile Layer"
        direction TB

        subgraph "beacontry App" 
            direction TB
            BS["BeaconScan"]:::mobile
            PS["PermissionSupport"]:::mobile
            UIb["Activity Main UI"]:::mobile
        end

        subgraph "Main App"
            direction TB

            subgraph "CameraX Subsystem"
                direction TB
                AC["AbstractCameraXActivity"]:::mobile
                BM["BaseModuleActivity"]:::mobile
                OD["ObjectDetectionActivity"]:::mobile
                TX["texture_view.xml"]:::mobile
            end

            subgraph "ML Inference"
                direction TB
                PP["PrePostProcessor"]:::mobile
                TS["TorchScript Model"]:::mobile
            end

            subgraph "UI Fragments"
                direction TB
                FF["FoodFragment"]:::mobile
                RF["RecordFragment"]:::mobile
                CF["CommunicationFragment"]:::mobile
                Lf1["fragment_food.xml"]:::mobile
                Lf2["fragment_record.xml"]:::mobile
                Lf3["fragment_communication.xml"]:::mobile
            end

            subgraph "Authentication Flow"
                direction TB
                LA["LoginActivity"]:::mobile
                SU["signup"]:::mobile
                LD["LoginData"]:::mobile
                LR["LoginResponse"]:::mobile
                JD["JoinData"]:::mobile
                JR["JoinResponse"]:::mobile
                Ll["activity_login.xml"]:::mobile
                Sl["signup_dialog.xml"]:::mobile
            end

            subgraph "Networking Layer"
                direction TB
                RC["RetrofitClient"]:::mobile
                SA["ServiceApi"]:::mobile
            end

            subgraph "Entry & Global"
                direction TB
                MA["MainActivity"]:::mobile
                GA["globalApplication"]:::mobile
            end

            subgraph "SNS Integration Assets"
                direction TB
                SN1["sns_main.html"]:::mobile
                SN2["sns_upload.html"]:::mobile
                SN3["sns_write.html"]:::mobile
            end
        end
    end

    %% Layer 3: Backend Service
    subgraph "Backend Service"
        direction TB
        API["REST API Endpoints"]:::backend
        DB[(Data Storage)]:::backend
    end

    %% Layer 4: ML Pipeline
    subgraph "ML Pipeline"
        direction TB
        T1["train.py"]:::ml
        T2["classify/train.py"]:::ml
        EX["export.py"]:::ml
        R1["restapi.py"]:::ml
        R2["example_request.py"]:::ml
    end

    %% Relationships
    AT -->|"BLE"| BS
    CAL -->|"BLE"| BS

    BS --> MA
    LA -->|"\dashed REST"| API
    RC -->|"\dashed REST"| API
    SA -->|"\dashed REST"| API
    API -->|"\dashed REST"| LA
    API -->|"\dashed REST"| SU
    EX ==> TS

    AC --> PP
    PP --> TS
    TS --> OD

    OD --> OD

    %% Click Events
    click AT "https://github.com/whereitswrong/smart_plate_project/blob/master/Arduino/ATcommandCheck/ATcommandCheck.ino"
    click CAL "https://github.com/whereitswrong/smart_plate_project/blob/master/Arduino/calibraion set/calibraion set.ino"
    click BS "https://github.com/whereitswrong/smart_plate_project/blob/master/beacontry/app/src/main/java/com/example/beacontry/BeaconScan.java"
    click PS "https://github.com/whereitswrong/smart_plate_project/blob/master/beacontry/app/src/main/java/com/example/beacontry/PermissionSupport.java"
    click UIb "https://github.com/whereitswrong/smart_plate_project/blob/master/beacontry/app/src/main/res/layout/activity_main.xml"
    click AC "https://github.com/whereitswrong/smart_plate_project/blob/master/Main/app/src/main/java/com/example/main/AbstractCameraXActivity.java"
    click BM "https://github.com/whereitswrong/smart_plate_project/blob/master/Main/app/src/main/java/com/example/main/BaseModuleActivity.java"
    click OD "https://github.com/whereitswrong/smart_plate_project/blob/master/Main/app/src/main/java/com/example/main/ObjectDetectionActivity.java"
    click TX "https://github.com/whereitswrong/smart_plate_project/blob/master/Main/app/src/main/res/layout/texture_view.xml"
    click PP "https://github.com/whereitswrong/smart_plate_project/blob/master/Main/app/src/main/java/com/example/main/PrePostProcessor.java"
    click TS "https://github.com/whereitswrong/smart_plate_project/blob/master/Main/app/src/main/assets/yolov5s.torchscript.ptl"
    click FF "https://github.com/whereitswrong/smart_plate_project/blob/master/Main/app/src/main/java/com/example/main/FoodFragment.java"
    click RF "https://github.com/whereitswrong/smart_plate_project/blob/master/Main/app/src/main/java/com/example/main/RecordFragment.java"
    click CF "https://github.com/whereitswrong/smart_plate_project/blob/master/Main/app/src/main/java/com/example/main/CommunicationFragment.java"
    click Lf1 "https://github.com/whereitswrong/smart_plate_project/blob/master/Main/app/src/main/res/layout/fragment_food.xml"
    click Lf2 "https://github.com/whereitswrong/smart_plate_project/blob/master/Main/app/src/main/res/layout/fragment_record.xml"
    click Lf3 "https://github.com/whereitswrong/smart_plate_project/blob/master/Main/app/src/main/res/layout/fragment_communication.xml"
    click LA "https://github.com/whereitswrong/smart_plate_project/blob/master/Main/app/src/main/java/com/example/main/LoginActivity.java"
    click SU "https://github.com/whereitswrong/smart_plate_project/blob/master/Main/app/src/main/java/com/example/main/signup.java"
    click LD "https://github.com/whereitswrong/smart_plate_project/blob/master/Main/app/src/main/java/com/example/main/LoginData.java"
    click LR "https://github.com/whereitswrong/smart_plate_project/blob/master/Main/app/src/main/java/com/example/main/LoginResponse.java"
    click JD "https://github.com/whereitswrong/smart_plate_project/blob/master/Main/app/src/main/java/com/example/main/JoinData.java"
    click JR "https://github.com/whereitswrong/smart_plate_project/blob/master/Main/app/src/main/java/com/example/main/JoinResponse.java"
    click Ll "https://github.com/whereitswrong/smart_plate_project/blob/master/Main/app/src/main/res/layout/activity_login.xml"
    click Sl "https://github.com/whereitswrong/smart_plate_project/blob/master/Main/app/src/main/res/layout/signup_dialog.xml"
    click RC "https://github.com/whereitswrong/smart_plate_project/blob/master/Main/app/src/main/java/com/example/main/RetrofitClient.java"
    click SA "https://github.com/whereitswrong/smart_plate_project/blob/master/Main/app/src/main/java/com/example/main/ServiceApi.java"
    click MA "https://github.com/whereitswrong/smart_plate_project/blob/master/Main/app/src/main/java/com/example/main/MainActivity.java"
    click GA "https://github.com/whereitswrong/smart_plate_project/blob/master/Main/app/src/main/java/com/example/main/globalApplication.java"
    click SN1 "https://github.com/whereitswrong/smart_plate_project/blob/master/Main/app/src/main/assets/sns_main.html"
    click SN2 "https://github.com/whereitswrong/smart_plate_project/blob/master/Main/app/src/main/assets/sns_upload.html"
    click SN3 "https://github.com/whereitswrong/smart_plate_project/blob/master/Main/app/src/main/assets/sns_write.html"
    click T1 "https://github.com/whereitswrong/smart_plate_project/blob/master/yolov5/train.py"
    click T2 "https://github.com/whereitswrong/smart_plate_project/blob/master/yolov5/classify/train.py"
    click EX "https://github.com/whereitswrong/smart_plate_project/blob/master/yolov5/export.py"
    click R1 "https://github.com/whereitswrong/smart_plate_project/blob/master/yolov5/utils/flask_rest_api/restapi.py"
    click R2 "https://github.com/whereitswrong/smart_plate_project/blob/master/yolov5/utils/flask_rest_api/example_request.py"

    %% Styles
    classDef hardware fill:#9f6,stroke:#333,stroke-width:1px
    classDef mobile fill:#6cf,stroke:#333,stroke-width:1px
    classDef backend fill:#f96,stroke:#333,stroke-width:1px
    classDef ml fill:#c9f,stroke:#333,stroke-width:1px
```
