# luunhattien2005/streamlit_aiot architecture

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

- Interactive diagram: https://gitdiagram.com/luunhattien2005/streamlit_aiot
- Repository: https://github.com/luunhattien2005/streamlit_aiot

## Overview

#### Purpose and Entrypoint
This is a Streamlit-hosted face-recognition door-access dashboard. Its deployed/local entrypoint is `source/streamlit_app.py`, launched with Streamlit. It combines an operator UI, a polling recognition engine, registration workflow, and access-history dashboard.

#### Streamlit Application Shell
`source/streamlit_app.py` owns the three-tab UI and coordinates application actions:
- Background-like auto-refresh polling for pending door requests.
- Access-history presentation.
- Face enrollment from uploaded images.
- Styling is supplied by `source/style.css`.

The engine executes within the Streamlit runtime rather than as a separately deployed worker, so recognition processing depends on an active app session/refresh cycle.

#### Door Request Processing Pipeline
Pending access requests originate outside the repository from an ESP32-CAM device. The device uploads a captured image to ImgBB, then creates a Firebase `new_request` record containing an image URL and `pending` status.

The application flow is:
1. Read pending requests through `source/firebase_manager.py`.
2. Download/process the image and derive a facial embedding through `source/face_engine.py`.
3. Compare it with all registered embeddings using cosine distance and the documented 0.68 threshold.
4. Mark the request completed and append an allow/deny result to `history_log`.

This separates device/image transport (ESP32-CAM and ImgBB) from recognition logic and durable state (Firebase).

#### Face Recognition Domain Layer
`source/face_engine.py` is the AI boundary. It extracts face vectors using DeepFace with the VGG-Face model and performs matching against enrolled embeddings. It must handle the important failure boundary where an uploaded/request image has no usable face or cannot be processed; downstream persistence should receive a rejection/unknown result rather than a successful match.

Enrollment follows the same embedding-generation path: an operator supplies a name and image in `source/streamlit_app.py`, then the generated vector is persisted as a registered identity.

#### Persistence and Mock Boundary
`source/firebase_manager.py` abstracts Firebase Realtime Database access and provides the project’s mock-mode fallback when Firebase secrets are unavailable. It is responsible for the logical database branches:
- `new_request`: device-originated pending/completed work queue.
- `registered`: identity names and embeddings.
- `history_log`: recognition/audit outcomes.

The local example/store artifact `source/Face_Database/registered_db.json` may support development data, but Firebase is the intended shared production datastore. Firebase credentials are expected via Streamlit secrets, not repository files.

#### Data Model and Audit Flow
Registered users are represented by a user ID, display name, and numeric embedding. A request carries its ID, image URL, and lifecycle status. Completion produces an immutable-style history entry containing timestamp, image URL, matched name or unknown identity, door action, and face bounding box. This preserves an audit trail independent of mutable request status.

#### Runtime and External Dependencies
The application runs on Python 3.13 with Streamlit and AI/database dependencies declared in `requirements.txt`. Material services are Firebase Realtime Database, ImgBB-hosted request images, DeepFace/VGG-Face inference, and the ESP32-CAM producer. Deployment targets Streamlit hosting, with `source/streamlit_app.py` fixed as the configured main file.

Main entry points: [`source/firebase_manager.py`](https://github.com/luunhattien2005/streamlit_aiot/blob/main/source/firebase_manager.py) (Firebase Manager), [`source/face_engine.py`](https://github.com/luunhattien2005/streamlit_aiot/blob/main/source/face_engine.py) (Face Engine), [`source/streamlit_app.py`](https://github.com/luunhattien2005/streamlit_aiot/blob/main/source/streamlit_app.py) (Dashboard Shell), [`source/style.css`](https://github.com/luunhattien2005/streamlit_aiot/blob/main/source/style.css) (Dashboard Styles)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Streamlit Runtime"]
  node_streamlit["Dashboard Shell<br/>Streamlit entrypoint<br/>[streamlit_app.py]"]
  node_styles["Dashboard Styles<br/>stylesheet<br/>[style.css]"]
  node_requirements["Python Dependencies<br/>runtime manifest<br/>[requirements.txt]"]
end

subgraph group_domain["Recognition Domain"]
  node_firebase_manager["Firebase Manager<br/>persistence adapter"]
  node_face_engine["Face Engine<br/>recognition service<br/>[face_engine.py]"]
  node_deepface{{"DeepFace VGG-Face<br/>inference model"}}
  node_matcher["Cosine Matcher<br/>matching rule<br/>[face_engine.py]"]
end

subgraph group_storage["Data &amp; External Services"]
  node_esp32{{"ESP32-CAM<br/>request producer"}}
  node_imgbb[("ImgBB<br/>image hosting")]
  node_firebase[("Firebase Realtime DB<br/>shared datastore")]
  node_request_queue["new_request<br/>request queue"]
  node_registered[("registered<br/>identity store")]
  node_history[("history_log<br/>audit log")]
  node_local_db["Local registered DB<br/>development artifact<br/>[registered_db.json]"]
end

node_esp32 -->|"uploads capture"| node_imgbb
node_esp32 -->|"creates pending request"| node_request_queue
node_imgbb -->|"image URL"| node_request_queue
node_streamlit -->|"polls, enrolls, shows history"| node_firebase_manager
node_streamlit -->|"submits enrollment and requests"| node_face_engine
node_styles -->|"styles UI"| node_streamlit
node_requirements -.->|"provides runtime"| node_streamlit
node_firebase_manager -->|"reads and writes"| node_firebase
node_firebase_manager -.->|"mock/development fallback"| node_local_db
node_firebase -->|"stores branch"| node_request_queue
node_firebase -->|"stores branch"| node_registered
node_firebase -->|"stores branch"| node_history
node_request_queue -->|"pending image URL"| node_face_engine
node_face_engine -->|"extracts embedding"| node_deepface
node_face_engine -->|"request embedding"| node_matcher
node_registered -->|"enrolled embeddings"| node_matcher
node_matcher -->|"allow, deny, or unknown result"| node_firebase_manager
node_firebase_manager -->|"marks completed"| node_request_queue
node_firebase_manager -->|"appends audit outcome"| node_history
node_face_engine -->|"persists enrollment vector"| node_registered

click node_local_db "https://github.com/luunhattien2005/streamlit_aiot/blob/main/source/Face_Database/registered_db.json"
click node_streamlit "https://github.com/luunhattien2005/streamlit_aiot/blob/main/source/streamlit_app.py"
click node_styles "https://github.com/luunhattien2005/streamlit_aiot/blob/main/source/style.css"
click node_firebase_manager "https://github.com/luunhattien2005/streamlit_aiot/blob/main/source/firebase_manager.py"
click node_face_engine "https://github.com/luunhattien2005/streamlit_aiot/blob/main/source/face_engine.py"
click node_matcher "https://github.com/luunhattien2005/streamlit_aiot/blob/main/source/face_engine.py"
click node_requirements "https://github.com/luunhattien2005/streamlit_aiot/blob/main/requirements.txt"

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_streamlit,node_styles,node_requirements toneBlue
class node_firebase_manager,node_face_engine,node_deepface,node_matcher toneAmber
class node_esp32,node_imgbb,node_firebase,node_request_queue,node_registered,node_history,node_local_db toneMint
```

## Components

### Streamlit Runtime

Recognition runs during active app refresh cycles.

- [Dashboard Shell](https://github.com/luunhattien2005/streamlit_aiot/blob/main/source/streamlit_app.py): Streamlit entrypoint · Three-tab operator UI, enrollment, history, and polling coordination. · `source/streamlit_app.py`
- [Dashboard Styles](https://github.com/luunhattien2005/streamlit_aiot/blob/main/source/style.css): stylesheet · `source/style.css`
- [Python Dependencies](https://github.com/luunhattien2005/streamlit_aiot/blob/main/requirements.txt): runtime manifest · Python 3.13 Streamlit, AI, and database dependencies. · `requirements.txt`

### Recognition Domain

- [Firebase Manager](https://github.com/luunhattien2005/streamlit_aiot/blob/main/source/firebase_manager.py): persistence adapter · Firebase access with mock-mode fallback. · `source/firebase_manager.py`
- [Face Engine](https://github.com/luunhattien2005/streamlit_aiot/blob/main/source/face_engine.py): recognition service · Generates embeddings and turns failures into unknown/rejected outcomes. · `source/face_engine.py`
- **DeepFace VGG-Face**: inference model · Extracts face vectors from enrollment and request images.
- [Cosine Matcher](https://github.com/luunhattien2005/streamlit_aiot/blob/main/source/face_engine.py): matching rule · Compares enrolled vectors using the 0.68 threshold. · `source/face_engine.py`

### Data & External Services

- **ESP32-CAM**: request producer · Captures door-access images.
- **ImgBB**: image hosting · Hosts captured request images.
- **Firebase Realtime DB**: shared datastore · Production state store; credentials come from Streamlit secrets.
- **new_request**: request queue · Pending and completed door requests.
- **registered**: identity store · Names and facial embeddings.
- **history_log**: audit log · Immutable-style allow/deny recognition outcomes.
- [Local registered DB](https://github.com/luunhattien2005/streamlit_aiot/blob/main/source/Face_Database/registered_db.json): development artifact · Optional development registration data. · `source/Face_Database/registered_db.json`

## Connections

- ESP32-CAM → ImgBB: uploads capture
- ESP32-CAM → new_request: creates pending request
- ImgBB → new_request: image URL
- Dashboard Shell → Firebase Manager: polls, enrolls, shows history
- Dashboard Shell → Face Engine: submits enrollment and requests
- Dashboard Styles → Dashboard Shell: styles UI
- Python Dependencies → Dashboard Shell: provides runtime
- Firebase Manager → Firebase Realtime DB: reads and writes
- Firebase Manager → Local registered DB: mock/development fallback
- Firebase Realtime DB → new_request: stores branch
- Firebase Realtime DB → registered: stores branch
- Firebase Realtime DB → history_log: stores branch
- new_request → Face Engine: pending image URL
- Face Engine → DeepFace VGG-Face: extracts embedding
- Face Engine → Cosine Matcher: request embedding
- registered → Cosine Matcher: enrolled embeddings
- Cosine Matcher → Firebase Manager: allow, deny, or unknown result
- Firebase Manager → new_request: marks completed
- Firebase Manager → history_log: appends audit outcome
- Face Engine → registered: persists enrollment vector
