# sonikdas745-ctrl/vision_based_inventory_management architecture

> Architecture diagram of the GitHub repository sonikdas745-ctrl/vision_based_inventory_management, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sonikdas745-ctrl/vision_based_inventory_management
- Repository: https://github.com/sonikdas745-ctrl/vision_based_inventory_management

## Overview

1. **What this repo actually is**
   This is a multi-layer smart inventory system, not a single app. The main production path is: camera/video input → YOLOv8 detection/inventory counting → backend/API stream → Next.js dashboard. Around that are ROS2 nodes for edge hardware deployment and a separate training/export toolchain for producing deployable models.

2. **Primary runtime boundaries**
   There are three real subsystems to diagram:
   - **Python ML/backend layer** in `src/`
   - **ROS2 edge/hardware layer** in `ros2_inventory/`
   - **Next.js dashboard layer** in `frontend/`
   The nested `Vision_Based_Inventory_Management/` folder looks like a parallel snapshot of the ML stack and model artifacts, so it should be treated as a training/packaging workspace rather than a separate service.

3. **Computer vision core**
   The detection engine is YOLOv8-based. The core model assets live under `models/` and `runs/detect/models/`, with multiple trained variants such as `rpc_real_labels`, `rpc_with_names`, and `emergency_fast`. The Python scripts in `src/` and `Vision_Based_Inventory_Management/src/` handle inference, preprocessing, training, and export.

4. **Training and model lifecycle**
   The ML pipeline runs from dataset config to training artifacts:
   - `config/dataset.yaml` defines dataset metadata
   - `src/preprocess.py`, `src/full_preprocess.py`, `src/train.py`, `src/full_train.py` prepare and train models
   - `src/export_model.py` produces deployment formats like TFLite, ONNX, OpenVINO, TensorRT
   - `runs/detect/models/*` stores training outputs, weights, and results
   This means the model lifecycle is a first-class part of the repo, not an external dependency.

5. **Edge/robotics layer**
   The ROS2 package `ros2_inventory` is the hardware-facing boundary. It contains:
   - `camera_node.py` for capture
   - `detection_node.py` / `new_detection_node.py` for inference
   - `inventory_node.py` for inventory state/count aggregation
   - `api_bridge_node.py` to connect ROS2 data to the backend/API world
   - `pan_tilt_controller.py` for hardware actuation
   Custom messages (`Detection.msg`, `DetectionArray.msg`, `InventoryCount.msg`) define the data contract between nodes.

6. **ROS2 data flow**
   The ROS2 launch file (`launch/inventory_system.launch.py`) ties the nodes together. The intended flow is:
   camera input on `/camera/image_raw` → detection results on `/detections` and annotated frames → inventory aggregation on `/inventory_status` → optional bridge out to API/backend. This is the edge compute path for Raspberry Pi / Ubuntu deployments.

7. **Backend/API boundary**
   The backend entry point is `src/api.py` plus supporting modules like `src/inference.py`, `src/utils.py`, `src/continual_learning.py`, and `src/create_class.py`. It exposes:
   - REST endpoints for health, detection, inventory counts/history, model info/switching, export
   - WebSocket endpoints for live video and inventory updates
   This layer is the integration point between ML inference and clients, and likely owns transient state such as current counts and history snapshots.

8. **Dashboard boundary**
   The frontend is a Next.js app using the App Router (`frontend/app/`). It is organized as a dashboard with components for:
   - live video streams (`video-stream.tsx`, `ROSVideoStream.tsx`, `detection-overlay.tsx`)
   - inventory analytics (`inventory-dashboard.tsx`, `stats-cards.tsx`, `realtime-chart.tsx`, `shelf-heatmap.tsx`)
   - operational views (`detections-table.tsx`, `activity-log.tsx`, `system-info.tsx`, `control-panel.tsx`)
   The presence of `frontend/app/api/inventory/*` route handlers and `frontend/lib/api-client.ts` suggests the UI talks to the backend through a thin client/BFF layer rather than embedding business logic in components.

9. **Frontend live-data path**
   The dashboard is built around streaming, not just polling. Hooks like `use-inventory-stream.ts` and components like `ROSVideoStream.tsx` indicate the UI subscribes to real-time inventory/video updates, which map cleanly to the backend WebSocket layer. There is also `mock-data.ts`, so some views can operate without live services during development.

10. **Configuration and deployment surfaces**
   Configuration is split by layer:
   - `.env.example` / `frontend/.env.example` for frontend API URLs
   - `ros2_inventory/config/params.yaml` for camera/model/device parameters
   - `config/dataset.yaml` for dataset setup
   The repo also contains many deployment guides (`DEPLOYMENT*.md`, `HARDWARE_CONNECTIONS.md`, `UBUNTU_SETUP.md`, etc.), which reinforces that the system is designed for edge deployment, not just local inference.

11. **What to treat as secondary**
   `gui.py` and several `*_backup` / duplicate folders are legacy or alternate packaging paths. They matter for completeness, but the architectural diagram should center on the three active planes: ML/backend, ROS2 edge, and Next.js dashboard.

12. **Big-picture system diagram**
   If you draw this repo accurately, the key arrows are:
   - camera/sensor hardware into ROS2
   - ROS2 detection/inventory into backend/API
   - backend streams and REST data into the dashboard
   - training/configuration pipelines feeding model artifacts used by both Python inference and ROS2 deployment
   The main design idea is a computer-vision pipeline split across edge execution, service/API integration, and a browser-based monitoring/control UI.

Main entry points: [`ros2_inventory/launch/inventory_system.launch.py`](https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/ros2_inventory/launch/inventory_system.launch.py) (System launch), [`src/api.py`](https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/src/api.py) (API server), [`ros2_inventory/ros2_inventory/camera_node.py`](https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/ros2_inventory/ros2_inventory/camera_node.py) (Camera node), [`runs/detect/models`](https://github.com/sonikdas745-ctrl/vision_based_inventory_management/tree/main/runs/detect/models) (Trained models)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ml_backend["Python ML/backend"]
  node_api_server["API server<br/>Python backend<br/>[api.py]"]
  node_inference_core["Inference core<br/>ML runtime<br/>[inference.py]"]
  node_ml_training["Training pipeline<br/>ML workflow<br/>[train.py]"]
  node_model_export["Model export<br/>ML packaging<br/>[export_model.py]"]
  node_ml_utils["ML support<br/>Python support<br/>[utils.py]"]
  node_dataset_cfg["Dataset config<br/>data config<br/>[dataset.yaml]"]
end

subgraph group_ros2_edge["ROS2 edge layer"]
  node_camera_input(("Camera input<br/>hardware source"))
  node_ros2_camera["Camera node<br/>ROS2 capture<br/>[camera_node.py]"]
  node_ros2_detect["Detection node<br/>ROS2 inference<br/>[detection_node.py]"]
  node_ros2_new_detect["Alt detector<br/>ROS2 inference"]
  node_ros2_inventory["Inventory node<br/>ROS2 aggregation<br/>[inventory_node.py]"]
  node_ros2_bridge["API bridge<br/>ROS2 integration<br/>[api_bridge_node.py]"]
  node_ros2_pan_tilt["Pan-tilt<br/>ROS2 actuation"]
  node_ros2_msgs["ROS messages<br/>ROS2 contracts<br/>[Detection.msg]"]
  node_ros2_launch["System launch<br/>ROS2 orchestration"]
end

subgraph group_frontend_ui["Next.js dashboard"]
  node_dashboard_app["Dashboard app<br/>Next.js app<br/>[page.tsx]"]
  node_live_stream["Live stream<br/>UI stream<br/>[ROSVideoStream.tsx]"]
  node_inventory_views["Inventory views<br/>UI analytics"]
  node_api_client["API client<br/>frontend integration<br/>[api-client.ts]"]
end

subgraph group_model_workspace["Model workspace"]
  node_model_artifacts[("Trained models<br/>weights/artifacts")]
  node_nested_workspace["Training workspace<br/>parallel stack<br/>[train.py]"]
end

node_camera_input -->|"frames"| node_ros2_camera
node_ros2_camera -->|"image stream"| node_ros2_detect
node_ros2_camera -->|"image stream"| node_ros2_new_detect
node_ros2_detect -->|"detections"| node_ros2_msgs
node_ros2_new_detect -->|"detections"| node_ros2_msgs
node_ros2_msgs -->|"counts"| node_ros2_inventory
node_ros2_inventory -->|"inventory state"| node_ros2_bridge
node_ros2_launch -->|"starts"| node_ros2_camera
node_ros2_launch -->|"starts"| node_ros2_detect
node_ros2_launch -->|"starts"| node_ros2_inventory
node_ros2_launch -->|"starts"| node_ros2_bridge
node_ros2_launch -->|"starts"| node_ros2_pan_tilt
node_dataset_cfg -->|"configures"| node_ml_training
node_ml_training -->|"produces"| node_model_artifacts
node_model_artifacts -->|"loads"| node_inference_core
node_model_export -->|"publishes"| node_model_artifacts
node_ml_utils -->|"supports"| node_ml_training
node_api_server -->|"serves"| node_inference_core
node_api_server -->|"manages"| node_ml_utils
node_api_client -->|"requests"| node_api_server
node_dashboard_app -->|"renders"| node_live_stream
node_dashboard_app -->|"renders"| node_inventory_views
node_live_stream -->|"subscribes"| node_api_client
node_inventory_views -->|"fetches"| node_api_client
node_nested_workspace -.->|"shares weights"| node_model_artifacts
node_nested_workspace -.->|"shares config"| node_dataset_cfg
node_ros2_bridge -->|"updates"| node_api_server
node_api_server -->|"streams data"| node_dashboard_app

click node_ros2_camera "https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/ros2_inventory/ros2_inventory/camera_node.py"
click node_ros2_detect "https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/ros2_inventory/ros2_inventory/detection_node.py"
click node_ros2_new_detect "https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/ros2_inventory/ros2_inventory/new_detection_node.py"
click node_ros2_inventory "https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/ros2_inventory/ros2_inventory/inventory_node.py"
click node_ros2_bridge "https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/ros2_inventory/ros2_inventory/api_bridge_node.py"
click node_ros2_pan_tilt "https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/ros2_inventory/ros2_inventory/pan_tilt_controller.py"
click node_ros2_msgs "https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/ros2_inventory/msg/Detection.msg"
click node_ros2_launch "https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/ros2_inventory/launch/inventory_system.launch.py"
click node_api_server "https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/src/api.py"
click node_inference_core "https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/src/inference.py"
click node_ml_training "https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/src/train.py"
click node_model_export "https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/src/export_model.py"
click node_ml_utils "https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/src/utils.py"
click node_dataset_cfg "https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/config/dataset.yaml"
click node_model_artifacts "https://github.com/sonikdas745-ctrl/vision_based_inventory_management/tree/main/runs/detect/models"
click node_nested_workspace "https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/Vision_Based_Inventory_Management/src/train.py"
click node_dashboard_app "https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/frontend/app/page.tsx"
click node_live_stream "https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/frontend/components/ROSVideoStream.tsx"
click node_inventory_views "https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/frontend/components/inventory-dashboard.tsx"
click node_api_client "https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/frontend/lib/api-client.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_api_server,node_inference_core,node_ml_training,node_model_export,node_ml_utils,node_dataset_cfg toneBlue
class node_camera_input,node_ros2_camera,node_ros2_detect,node_ros2_new_detect,node_ros2_inventory,node_ros2_bridge,node_ros2_pan_tilt,node_ros2_msgs,node_ros2_launch toneAmber
class node_dashboard_app,node_live_stream,node_inventory_views,node_api_client toneMint
class node_model_artifacts,node_nested_workspace toneRose
```

## Components

### Python ML/backend

Inference, API, training, and export workflows in the Python stack.

- [API server](https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/src/api.py): Python backend · Main backend entry point for REST, WebSocket, and inventory state APIs. · `src/api.py`
- [Inference core](https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/src/inference.py): ML runtime · Shared detection/runtime logic used by the backend and edge flows. · `src/inference.py`
- [Training pipeline](https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/src/train.py): ML workflow · Preprocessing and training scripts for building new detector models. · `src/train.py`
- [Model export](https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/src/export_model.py): ML packaging · Exports trained models into deployment formats for edge/runtime use. · `src/export_model.py`
- [ML support](https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/src/utils.py): Python support · Utility, continual learning, and class-management code around the backend ML stack. · `src/utils.py`
- [Dataset config](https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/config/dataset.yaml): data config · Dataset metadata and class configuration used by training and preprocessing. · `config/dataset.yaml`

### ROS2 edge layer

Camera, detection, inventory aggregation, and hardware control on the edge device.

- **Camera input**: hardware source · Video and image input entering the system from connected cameras or sensors.
- [Camera node](https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/ros2_inventory/ros2_inventory/camera_node.py): ROS2 capture · Captures frames from the camera stream and publishes them into ROS2. · `ros2_inventory/ros2_inventory/camera_node.py`
- [Detection node](https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/ros2_inventory/ros2_inventory/detection_node.py): ROS2 inference · Runs model inference on incoming frames and publishes detections. · `ros2_inventory/ros2_inventory/detection_node.py`
- [Alt detector](https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/ros2_inventory/ros2_inventory/new_detection_node.py): ROS2 inference · Alternative detection implementation for edge inference. · `ros2_inventory/ros2_inventory/new_detection_node.py`
- [Inventory node](https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/ros2_inventory/ros2_inventory/inventory_node.py): ROS2 aggregation · Aggregates detections into inventory counts and current stock state. · `ros2_inventory/ros2_inventory/inventory_node.py`
- [API bridge](https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/ros2_inventory/ros2_inventory/api_bridge_node.py): ROS2 integration · Bridges ROS2 data out toward backend/API consumers. · `ros2_inventory/ros2_inventory/api_bridge_node.py`
- [Pan-tilt](https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/ros2_inventory/ros2_inventory/pan_tilt_controller.py): ROS2 actuation · Controls camera movement hardware for tracking and positioning. · `ros2_inventory/ros2_inventory/pan_tilt_controller.py`
- [ROS messages](https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/ros2_inventory/msg/Detection.msg): ROS2 contracts · Detection and inventory message definitions shared between ROS2 nodes. · `ros2_inventory/msg/Detection.msg`
- [System launch](https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/ros2_inventory/launch/inventory_system.launch.py): ROS2 orchestration · Launches the edge node graph and binds the runtime together. · `ros2_inventory/launch/inventory_system.launch.py`

### Next.js dashboard

Browser dashboard, live streams, and API-facing UI components.

- [Dashboard app](https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/frontend/app/page.tsx): Next.js app · App Router entry for the browser-based monitoring and control UI. · `frontend/app/page.tsx`
- [Live stream](https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/frontend/components/ROSVideoStream.tsx): UI stream · Live video and overlay components for real-time camera/detection views. · `frontend/components/ROSVideoStream.tsx`
- [Inventory views](https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/frontend/components/inventory-dashboard.tsx): UI analytics · Charts, tables, and operational panels for inventory monitoring. · `frontend/components/inventory-dashboard.tsx`
- [API client](https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/frontend/lib/api-client.ts): frontend integration · Thin client and hooks used by the dashboard to reach backend inventory APIs. · `frontend/lib/api-client.ts`

### Model workspace

Parallel training and packaging workspace with trained weights and artifacts.

- [Trained models](https://github.com/sonikdas745-ctrl/vision_based_inventory_management/tree/main/runs/detect/models): weights/artifacts · Stored YOLOv8 weights and run outputs used for deployment and experimentation. · `runs/detect/models`
- [Training workspace](https://github.com/sonikdas745-ctrl/vision_based_inventory_management/blob/main/Vision_Based_Inventory_Management/src/train.py): parallel stack · Parallel ML workspace snapshot containing preprocessing, training, inference, and GUI scripts. · `Vision_Based_Inventory_Management/src/train.py`

## Connections

- Camera input → Camera node: frames · Camera hardware feeds the ROS2 capture node.
- Camera node → Detection node: image stream · Captured frames are sent into inference nodes.
- Camera node → Alt detector: image stream · Alternate detector can consume the same camera feed.
- Detection node → ROS messages: detections · Detection outputs are encoded as ROS2 messages.
- Alt detector → ROS messages: detections · Alternate detection outputs use the same message contracts.
- ROS messages → Inventory node: counts · Inventory aggregation consumes detection messages.
- Inventory node → API bridge: inventory state · Aggregated counts are forwarded toward backend/API consumers.
- System launch → Camera node: starts · Launch file orchestrates the camera node.
- System launch → Detection node: starts · Launch file orchestrates the detector node.
- System launch → Inventory node: starts · Launch file orchestrates inventory aggregation.
- System launch → API bridge: starts · Launch file includes the bridge node.
- System launch → Pan-tilt: starts · Launch file can include hardware control.
- Dataset config → Training pipeline: configures · Dataset metadata feeds preprocessing and training.
- Training pipeline → Trained models: produces · Training writes weights and run artifacts.
- Trained models → Inference core: loads · Inference uses trained weights from the artifact store.
- Model export → Trained models: publishes · Exported deployment variants are stored with model artifacts.
- ML support → Training pipeline: supports · Support code is used by training and continual learning.
- API server → Inference core: serves · Backend API wraps inference for live requests and streams.
- API server → ML support: manages · Backend owns model switching, class creation, and continual learning flows.
- API client → API server: requests · Dashboard consumes REST and stream endpoints via the client layer.
- Dashboard app → Live stream: renders · App composes live video and overlay components.
- Dashboard app → Inventory views: renders · App composes analytics and operational views.
- Live stream → API client: subscribes · Live stream components consume backend real-time data.
- Inventory views → API client: fetches · Inventory views fetch current state and history through the API client.
- Training workspace → Trained models: shares weights · Parallel workspace contains training outputs and model files.
- Training workspace → Dataset config: shares config · Nested workspace mirrors the dataset setup for training.
- API bridge → API server: updates · Edge inventory data is bridged into the backend/API layer.
- API server → Dashboard app: streams data · Backend provides live inventory and video data to the dashboard.
