SmartRetail_AI

SmartRetail AI — Architecture Overview

Living architectural record. Every phase appends to this file as services, patterns, and decisions land.

Vision

SmartRetail AI turns existing CCTV infrastructure into an explainable, event-aware retail intelligence platform. It builds a unified understanding of people, products, shelves, carts, staff, checkout events, and camera relationships, delivering store analytics and loss-prevention decision support.

It is a decision-support and human-review platform, never an autonomous system that declares a person guilty: every alert is explained with evidence and reviewed by a human.

Observe → Track → Associate → Understand → Score → Explain → Alert → Human Review → Learn

Chosen Stack (Phase 1)

Layer Choice Rationale
Backend Python + FastAPI Async-native, automatic OpenAPI docs, first-class fit with the Python-heavy CV/ML ecosystem (OpenCV, PyTorch, Ultralytics) used by later phases.
Primary store PostgreSQL Holds graph-like relationships between people, products, tracks, and events; native JSONB for flexible evidence/metadata fields; mature, battle-tested.
Cache / bus / short-lived state Redis Caching, pub/sub between services, and short-lived state such as active track buffers.
Migration Alembic (SQLAlchemy async) Versioned schema evolution on the async engine; migrations live in database/migrations/.
ORM SQLAlchemy 2.0 (async) Async engine/session foundation; ORM models attach from Phase 2.
Dev orchestration Docker Compose One command for the whole local stack (backend + PostgreSQL + Redis).
Server uvicorn (ASGI) Production-appropriate ASGI server in every environment; hot reload gated behind a RELOAD flag for dev.
Dependency management pip + requirements.txt Simple, consistent across the monorepo (requirements.txt runtime, requirements-dev.txt tooling).
Lint/format/test ruff + pytest Single-tool lint+format; pytest configured at the repo root in pyproject.toml.

System Topology

  CCTV Cameras (RTSP / ONVIF / Files)
            │
            ▼
  ┌──────────────────────┐        ┌──────────────────────┐
  │   Video Ingestion    │        │   Detection &        │
  │   (services/         │───────▶│   Tracking           │
  │    ingestion)        │        │   (detection/tracking│
  └──────────────────────┘        └──────────┬───────────┘
                                             │
                                             ▼
                              ┌──────────────────────────┐
                              │  Multi-Camera Tracking / │
                              │  Re-ID / Camera Handoff  │
                              └──────────┬───────────────┘
                                         │
                                         ▼
                              ┌──────────────────────────┐
                              │  Event Understanding      │
                              │  (events, product,       │
                              │   interaction)           │
                              └──────────┬───────────────┘
                                         │
                                         ▼
                              ┌──────────────────────────┐
                              │  Risk Engine             │
                              │  (risk-engine)           │
                              └──────────┬───────────────┘
                                         │
                                         ▼
                              ┌──────────────────────────┐
                              │  Dashboard / Alerting    │
                              │  (frontend, notifications)│
                              └──────────────────────────┘

  ┌───────────────┬──────────────┬──────────────┬──────────────┐
  │ PostgreSQL    │    Redis     │   Worker     │  Analytics   │
  │ (persistence) │ (cache/bus)  │  (async jobs)│ (aggregation)│
  └───────────────┴──────────────┴──────────────┴──────────────┘

Event flow: detection event → tracking event → interaction event → behavior event → risk event → alert event.

Repository Layout

Path Responsibility
apps/backend FastAPI API service (system endpoints live; domain API from Phase 2).
apps/frontend Human-review dashboard (React/TS).
apps/worker Background job worker (Redis-backed queues).
apps/ai-services CV/ML inference containers.
services/* Domain service packages (see each README): ingestion, detection, tracking, reid, product, interaction, events, risk-engine, analytics, notifications.
models/* ML model artifacts (detection, tracking, reid, pose, action).
datasets/* Raw, annotations, processed, splits.
database/ migrations/ (Alembic) and seeds/.
infrastructure/ docker/, kubernetes/ (stubbed), monitoring/.
tests/ unit/, integration/, scenarios/, performance/.
docs/ architecture/, api/, models/, deployment/.

Service Responsibilities

Conventions

Deployment Notes

Decision Log

2026 — Phase 1: Repo Scaffolding

2026 — Phase 2: Relational Schema

Phase 5 - Person Detection & Single-Camera Tracking

Phase 6 - Product Detection & Identification

Phase 7 - Person-Product Interaction Detection

Phase 8 - Multi-Camera Tracking & Re-ID

Phase 9 - Store Spatial Model & Zone Management

Phase 10 - Event Detection & Product State Machine

Phases 11-20 - Journeys through Frontend

Phase 21 - End-to-End Completeness & Section 101 Reference Demonstration