# kiruthikkumar16/facial architecture

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

- Interactive diagram: https://gitdiagram.com/kiruthikkumar16/facial
- Repository: https://github.com/kiruthikkumar16/facial

## Overview

#### Purpose and deployment shape
Enterprise facial-recognition platform using an edge-to-cloud architecture: local video inference produces face events and embeddings; a FastAPI service persists/searches them in PostgreSQL/pgvector; a Next.js dashboard presents live operations and analytics. Intended runtimes are a local Python edge node, Render-hosted API, Supabase PostgreSQL, and Vercel-hosted frontend.

#### Primary entry points
- Edge runtime: `facial_recognition/run.py`, with alternate CPU-oriented launch path `facial_recognition/run_cpu.py`; core application modules include `facial_recognition/main.py` and `facial_recognition/main_cpu.py`.
- API server: `backend/main.py`, run through Uvicorn.
- Dashboard: Next.js App Router root at `facial-recognition-dashboard/app/page.tsx`, composed by `facial-recognition-dashboard/components/dashboard/dashboard.tsx`.

#### Edge capture and inference
The edge process acquires RTSP/webcam frames, detects faces, generates 512-dimensional InsightFace/ONNX embeddings, and recognizes/enrolls identities. Detection and embedding responsibilities are separated in `facial_recognition/detector.py` and `facial_recognition/recognizer.py`; source acquisition resides in `facial_recognition/capture.py`. The Haar cascade asset at `cascades/haarcascade_frontalface_default.xml` is a local CV dependency.

#### Durable edge-to-cloud event pipeline
Each detection is dual-written: locally to a CSV-backed transaction/archive path and remotely to the API, so network loss does not discard events. Relevant pipeline/recovery boundaries are `facial_recognition/pending.py`, `facial_recognition/review_pending.py`, and `backend/tasks/ingest_csv.py`. Cloud-side CSV ingestion is also represented by `backend/ingest_csv.py`, enabling deferred reconciliation rather than making recognition dependent on API availability.

#### Cloud API and persistence boundary
FastAPI exposes ingestion, dashboard data, forensic search, and identity-management capabilities through `backend/main.py`. SQLAlchemy connection/session setup is centralized in `backend/database.py`; relational entities plus vector-bearing records are modeled in `backend/models.py`, with request/response contracts in `backend/schemas.py`. Configuration and deployment environment handling are in `backend/config.py` and `backend/render.yaml`. PostgreSQL on Supabase, with `pgvector`, is the authoritative cloud store for metadata and similarity search.

#### Vector search and identity lifecycle
Embeddings flow from edge inference or backend manual-image processing into pgvector-backed records. The backend keeps ONNX-capable CV processing available for forensic uploads, while database vector distance queries support cosine similarity and deduplication/merge workflows. Edge enrollment and unknown-person handling are represented by `facial_recognition/enroll.py` and `facial_recognition/pending.py`; the backend is the trust boundary for durable identity/profile mutation.

#### Live dashboard synchronization
The API WebSocket layer in `backend/websocket.py` broadcasts alert, health, or KPI changes after backend events. The frontend API boundary is `facial-recognition-dashboard/lib/api.ts`; React providers and query-state wiring begin at `facial-recognition-dashboard/components/providers.tsx`. Dashboard tabs divide operational views into alerts, analytics, forensic search, profiles, and system status, e.g. `components/dashboard/tabs/alerts-tab.tsx`, `analytics-tab.tsx`, and `forensic-tab.tsx`. Clients use push-triggered refresh/invalidation rather than continual database polling.

#### Frontend resilience and presentation boundary
The Next.js dashboard is a client presentation layer, not the source of truth. Shared dashboard composition is in `facial-recognition-dashboard/components/dashboard/shared.tsx`, while global and component-level failure containment is provided by `facial-recognition-dashboard/components/error-boundary.tsx` and `components/ui/error-boundary.tsx`. Environment-specific frontend API configuration is expected through `facial-recognition-dashboard/.env.local.example`.

Main entry points: [`backend/main.py`](https://github.com/kiruthikkumar16/facial/blob/main/backend/main.py) (FastAPI service), [`facial_recognition/recognizer.py`](https://github.com/kiruthikkumar16/facial/blob/main/facial_recognition/recognizer.py) (Embedding and recognition), [`facial-recognition-dashboard/components/dashboard/dashboard.tsx`](https://github.com/kiruthikkumar16/facial/blob/main/facial-recognition-dashboard/components/dashboard/dashboard.tsx) (Dashboard composition), [`facial-recognition-dashboard/lib/api.ts`](https://github.com/kiruthikkumar16/facial/blob/main/facial-recognition-dashboard/lib/api.ts) (Frontend API client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_edge["Edge node"]
  node_edge_entry["Edge launchers<br/>Python entry points<br/>[run.py]"]
  node_capture["Frame capture<br/>RTSP/webcam input<br/>[capture.py]"]
  node_detection["Face detection<br/>CV detector<br/>[detector.py]"]
  node_embedding["Embedding and recognition<br/>InsightFace / ONNX<br/>[recognizer.py]"]
  node_enrollment["Enrollment and unknowns<br/>identity intake<br/>[enroll.py]"]
  node_edge_archive["Local event archive<br/>CSV transaction log<br/>[pending.py]"]
  node_recovery["Pending-event recovery<br/>reconciliation worker<br/>[review_pending.py]"]
end

subgraph group_cloud["Cloud backend"]
  node_api["FastAPI service<br/>API and ingestion server<br/>[main.py]"]
  node_api_contracts["API contracts<br/>Pydantic schemas<br/>[schemas.py]"]
  node_manual_cv["Forensic image processing<br/>ONNX CV processing<br/>[main.py]"]
  node_csv_ingestion["Deferred CSV ingestion<br/>reconciliation ingestion<br/>[ingest_csv.py]"]
  node_persistence["SQLAlchemy persistence<br/>database session layer<br/>[database.py]"]
  node_models["Identity and vector records<br/>SQLAlchemy models<br/>[models.py]"]
  node_supabase[("Supabase PostgreSQL + pgvector<br/>authoritative data store")]
  node_websocket["WebSocket broadcaster<br/>live event channel<br/>[websocket.py]"]
end

subgraph group_dashboard["Dashboard"]
  node_next_root["Next.js dashboard<br/>App Router client<br/>[page.tsx]"]
  node_dashboard_shell["Dashboard composition<br/>React dashboard shell<br/>[dashboard.tsx]"]
  node_frontend_api["Frontend API client<br/>HTTP and WebSocket boundary<br/>[api.ts]"]
  node_client_state["Providers and query state<br/>React state layer<br/>[providers.tsx]"]
  node_error_boundary["Failure containment<br/>React error boundary<br/>[error-boundary.tsx]"]
end

node_edge_entry -->|"starts"| node_capture
node_capture -->|"frames"| node_detection
node_detection -->|"face crops"| node_embedding
node_embedding -->|"recognized or unknown faces"| node_enrollment
node_embedding -->|"dual-writes events"| node_edge_archive
node_edge_archive -.->|"pending CSV events"| node_recovery
node_embedding -->|"remote event ingestion"| node_api
node_recovery -.->|"reconciles archived events"| node_csv_ingestion
node_csv_ingestion -->|"deferred import"| node_api
node_api -->|"validates requests"| node_api_contracts
node_api -->|"forensic uploads"| node_manual_cv
node_api -->|"reads and mutates"| node_persistence
node_persistence -->|"maps records"| node_models
node_models -->|"metadata and vectors"| node_supabase
node_api -->|"backend changes"| node_websocket
node_next_root -->|"renders"| node_dashboard_shell
node_dashboard_shell -->|"uses"| node_client_state
node_client_state -->|"queries and invalidates"| node_frontend_api
node_frontend_api -->|"HTTP operations"| node_api
node_websocket -->|"push updates"| node_frontend_api
node_error_boundary -.->|"contains UI failures"| node_dashboard_shell

click node_edge_entry "https://github.com/kiruthikkumar16/facial/blob/main/facial_recognition/run.py"
click node_capture "https://github.com/kiruthikkumar16/facial/blob/main/facial_recognition/capture.py"
click node_detection "https://github.com/kiruthikkumar16/facial/blob/main/facial_recognition/detector.py"
click node_embedding "https://github.com/kiruthikkumar16/facial/blob/main/facial_recognition/recognizer.py"
click node_enrollment "https://github.com/kiruthikkumar16/facial/blob/main/facial_recognition/enroll.py"
click node_edge_archive "https://github.com/kiruthikkumar16/facial/blob/main/facial_recognition/pending.py"
click node_recovery "https://github.com/kiruthikkumar16/facial/blob/main/facial_recognition/review_pending.py"
click node_api "https://github.com/kiruthikkumar16/facial/blob/main/backend/main.py"
click node_api_contracts "https://github.com/kiruthikkumar16/facial/blob/main/backend/schemas.py"
click node_manual_cv "https://github.com/kiruthikkumar16/facial/blob/main/backend/main.py"
click node_csv_ingestion "https://github.com/kiruthikkumar16/facial/blob/main/backend/ingest_csv.py"
click node_persistence "https://github.com/kiruthikkumar16/facial/blob/main/backend/database.py"
click node_models "https://github.com/kiruthikkumar16/facial/blob/main/backend/models.py"
click node_websocket "https://github.com/kiruthikkumar16/facial/blob/main/backend/websocket.py"
click node_next_root "https://github.com/kiruthikkumar16/facial/blob/main/facial-recognition-dashboard/app/page.tsx"
click node_dashboard_shell "https://github.com/kiruthikkumar16/facial/blob/main/facial-recognition-dashboard/components/dashboard/dashboard.tsx"
click node_frontend_api "https://github.com/kiruthikkumar16/facial/blob/main/facial-recognition-dashboard/lib/api.ts"
click node_client_state "https://github.com/kiruthikkumar16/facial/blob/main/facial-recognition-dashboard/components/providers.tsx"
click node_error_boundary "https://github.com/kiruthikkumar16/facial/blob/main/facial-recognition-dashboard/components/error-boundary.tsx"

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_edge_entry,node_capture,node_detection,node_embedding,node_enrollment,node_edge_archive,node_recovery toneBlue
class node_api,node_api_contracts,node_manual_cv,node_csv_ingestion,node_persistence,node_models,node_supabase,node_websocket toneAmber
class node_next_root,node_dashboard_shell,node_frontend_api,node_client_state,node_error_boundary toneMint
```

## Components

### Edge node

Local video capture and recognition runtime.

- [Edge launchers](https://github.com/kiruthikkumar16/facial/blob/main/facial_recognition/run.py): Python entry points · GPU and CPU launch paths start local recognition. · `facial_recognition/run.py`
- [Frame capture](https://github.com/kiruthikkumar16/facial/blob/main/facial_recognition/capture.py): RTSP/webcam input · `facial_recognition/capture.py`
- [Face detection](https://github.com/kiruthikkumar16/facial/blob/main/facial_recognition/detector.py): CV detector · Uses the local Haar cascade dependency. · `facial_recognition/detector.py`
- [Embedding and recognition](https://github.com/kiruthikkumar16/facial/blob/main/facial_recognition/recognizer.py): InsightFace / ONNX · Produces 512-dimensional embeddings and recognition results. · `facial_recognition/recognizer.py`
- [Enrollment and unknowns](https://github.com/kiruthikkumar16/facial/blob/main/facial_recognition/enroll.py): identity intake · `facial_recognition/enroll.py`
- [Local event archive](https://github.com/kiruthikkumar16/facial/blob/main/facial_recognition/pending.py): CSV transaction log · Retains events while cloud delivery is unavailable. · `facial_recognition/pending.py`
- [Pending-event recovery](https://github.com/kiruthikkumar16/facial/blob/main/facial_recognition/review_pending.py): reconciliation worker · `facial_recognition/review_pending.py`

### Cloud backend

Render-hosted API and Supabase persistence boundary.

- [FastAPI service](https://github.com/kiruthikkumar16/facial/blob/main/backend/main.py): API and ingestion server · Exposes ingestion, dashboard, forensic, and identity operations. · `backend/main.py`
- [API contracts](https://github.com/kiruthikkumar16/facial/blob/main/backend/schemas.py): Pydantic schemas · `backend/schemas.py`
- [Forensic image processing](https://github.com/kiruthikkumar16/facial/blob/main/backend/main.py): ONNX CV processing · Processes manual forensic uploads into embeddings. · `backend/main.py`
- [Deferred CSV ingestion](https://github.com/kiruthikkumar16/facial/blob/main/backend/ingest_csv.py): reconciliation ingestion · Imports archived edge events after deferred delivery. · `backend/ingest_csv.py`
- [SQLAlchemy persistence](https://github.com/kiruthikkumar16/facial/blob/main/backend/database.py): database session layer · `backend/database.py`
- [Identity and vector records](https://github.com/kiruthikkumar16/facial/blob/main/backend/models.py): SQLAlchemy models · Defines relational entities and vector-bearing records. · `backend/models.py`
- **Supabase PostgreSQL + pgvector**: authoritative data store · Stores metadata and performs similarity, deduplication, and merge queries.
- [WebSocket broadcaster](https://github.com/kiruthikkumar16/facial/blob/main/backend/websocket.py): live event channel · Publishes alerts, health, and KPI changes. · `backend/websocket.py`

### Dashboard

Vercel-hosted Next.js operations client.

- [Next.js dashboard](https://github.com/kiruthikkumar16/facial/blob/main/facial-recognition-dashboard/app/page.tsx): App Router client · Operational presentation layer, not a source of truth. · `facial-recognition-dashboard/app/page.tsx`
- [Dashboard composition](https://github.com/kiruthikkumar16/facial/blob/main/facial-recognition-dashboard/components/dashboard/dashboard.tsx): React dashboard shell · Composes alerts, analytics, forensic, profiles, and system status views. · `facial-recognition-dashboard/components/dashboard/dashboard.tsx`
- [Frontend API client](https://github.com/kiruthikkumar16/facial/blob/main/facial-recognition-dashboard/lib/api.ts): HTTP and WebSocket boundary · `facial-recognition-dashboard/lib/api.ts`
- [Providers and query state](https://github.com/kiruthikkumar16/facial/blob/main/facial-recognition-dashboard/components/providers.tsx): React state layer · Refreshes or invalidates views from push events. · `facial-recognition-dashboard/components/providers.tsx`
- [Failure containment](https://github.com/kiruthikkumar16/facial/blob/main/facial-recognition-dashboard/components/error-boundary.tsx): React error boundary · `facial-recognition-dashboard/components/error-boundary.tsx`

## Connections

- Edge launchers → Frame capture: starts
- Frame capture → Face detection: frames
- Face detection → Embedding and recognition: face crops
- Embedding and recognition → Enrollment and unknowns: recognized or unknown faces
- Embedding and recognition → Local event archive: dual-writes events
- Local event archive → Pending-event recovery: pending CSV events
- Embedding and recognition → FastAPI service: remote event ingestion
- Pending-event recovery → Deferred CSV ingestion: reconciles archived events
- Deferred CSV ingestion → FastAPI service: deferred import
- FastAPI service → API contracts: validates requests
- FastAPI service → Forensic image processing: forensic uploads
- FastAPI service → SQLAlchemy persistence: reads and mutates
- SQLAlchemy persistence → Identity and vector records: maps records
- Identity and vector records → Supabase PostgreSQL + pgvector: metadata and vectors
- FastAPI service → WebSocket broadcaster: backend changes
- Next.js dashboard → Dashboard composition: renders
- Dashboard composition → Providers and query state: uses
- Providers and query state → Frontend API client: queries and invalidates
- Frontend API client → FastAPI service: HTTP operations
- WebSocket broadcaster → Frontend API client: push updates
- Failure containment → Dashboard composition: contains UI failures
