# amanchauhan2005/purplee_hackathon architecture

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

- Interactive diagram: https://gitdiagram.com/amanchauhan2005/purplee_hackathon
- Repository: https://github.com/amanchauhan2005/purplee_hackathon

## Overview

#### Purpose and Runtime Topology
RetailIQ is a CPU-only, multi-camera retail analytics pipeline. It converts five CCTV streams into deduplicated visitor journeys and store KPIs, then serves live business telemetry and operational health views. Docker Compose runs a long-lived API service, a detector process, Prometheus, and Grafana. Core deployment files: `docker-compose.yml`, `Dockerfile`, `run_all.py`.

#### Detector Orchestration and Video Input
`run_all.py` launches one processing thread per camera. Each pipeline consumes bundled sample clips or mounted production files, preserving per-camera processing while coordinating through shared cross-camera identity state. Primary inputs are `data/sample/CAM_1.mp4` through `data/sample/CAM_5.mp4`; the per-stream implementation is `tracker/video_processor.py`.

#### Vision, Tracking, and Identity Boundary
For each frame, the processor uses YOLOv8 person detection and ByteTrack-style local tracking, then passes track observations to cross-camera Re-ID. Re-ID performs spatial-temporal handoff plus HSV appearance comparison to assign a store-wide identity and prevent duplicated counts across overlapping feeds. Model assets and identity logic reside in `tracker/yolov8n.pt`, `tracker/video_processor.py`, and `tracker/reid.py`.

#### Spatial Semantics and Journey Events
Camera-local polygon zones classify tracked people into store locations. Zone transitions are converted into domain events such as entry, exit, dwell, staff, product engagement, and billing-related activity. This is the key boundary from CV observations to business semantics. Relevant paths: `tracker/zone_config.py`, `tracker/zones_CAM_1.json`, `tracker/state_machine.py`. Calibration/homography code exists in `tracker/calibrate_homography.py` and `tracker/homography.py`, but the operational design uses configured camera zones rather than a required shared floor-plane calibration.

#### Event Ingestion and In-Memory Store
The detector posts normalized events to the FastAPI service. `api/main.py` is the service entry point and owns event ingestion, the in-memory single source of truth, KPI derivation, WebSocket fan-out, and business endpoints including `/metrics`, `/funnel`, and `/state`. Supporting event and comparison concerns are separated in `api/events.py`, `api/event_log.py`, and `api/compare.py`. There is no persistence boundary: process memory is the authoritative current-store state.

#### Live Dashboard and Client Flow
The API serves a static browser dashboard that opens a WebSocket to receive live state/KPI updates. The dashboard is presentation-only; business metrics originate in the API’s state store rather than being recomputed client-side. Main client assets: `dashboard/index.html`, `dashboard/app.js`, `dashboard/compare.html`.

#### Operations and External Interfaces
FastAPI/Uvicorn exposes health, system/resource, Prometheus, REST, and WebSocket interfaces. Docker health checks gate detector startup on API readiness. Prometheus scrapes API metrics, while provisioned Grafana visualizes golden signals and alert rules detect latency, errors, CPU pressure, and stale ingestion. Infrastructure paths: `docker-compose.yml`, `monitoring/prometheus.yml`, `monitoring/alerts.yml`, `monitoring/grafana/dashboards/retailiq-golden-signals.json`.

Main entry points: [`api/main.py`](https://github.com/amanchauhan2005/purplee_hackathon/blob/main/api/main.py) (FastAPI service), [`docker-compose.yml`](https://github.com/amanchauhan2005/purplee_hackathon/blob/main/docker-compose.yml) (Docker Compose), [`monitoring/prometheus.yml`](https://github.com/amanchauhan2005/purplee_hackathon/blob/main/monitoring/prometheus.yml) (Prometheus), [`run_all.py`](https://github.com/amanchauhan2005/purplee_hackathon/blob/main/run_all.py) (Multi-camera runner)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_detector["Detector pipeline"]
  node_video_inputs["Camera video streams<br/>video input<br/>[CAM_1.mp4]"]
  node_orchestrator["Multi-camera runner<br/>orchestrator<br/>[run_all.py]"]
  node_video_processor["Per-stream processor<br/>CV pipeline<br/>[video_processor.py]"]
  node_person_detection{{"YOLO person detection<br/>vision model<br/>[yolov8n.pt]"}}
  node_local_tracking["Local track association<br/>tracker<br/>[video_processor.py]"]
  node_cross_camera_reid["Cross-camera Re-ID<br/>identity service<br/>[reid.py]"]
  node_zone_classifier["Camera zone classification<br/>spatial semantics<br/>[zone_config.py]"]
  node_zone_definitions["Camera zone definitions<br/>zone config<br/>[zones_CAM_1.json]"]
  node_journey_state_machine["Journey event state machine<br/>domain event engine<br/>[state_machine.py]"]
end

subgraph group_api["API and live state"]
  node_api_service["FastAPI service<br/>service entry point<br/>[main.py]"]
  node_event_ingestion["Event normalization<br/>ingestion layer<br/>[events.py]"]
  node_state_store[("In-memory store state<br/>authoritative state<br/>[event_log.py]")]
  node_kpi_service["KPI and comparison derivation<br/>business metrics<br/>[compare.py]"]
  node_dashboard["Live browser dashboard<br/>static client<br/>[app.js]"]
end

subgraph group_observability["Observability"]
  node_prometheus[("Prometheus<br/>metrics collector<br/>[prometheus.yml]")]
  node_grafana["Grafana dashboards<br/>visualization"]
  node_alert_rules["Alert rules<br/>monitoring rules<br/>[alerts.yml]"]
end

node_compose{{"Docker Compose<br/>deployment<br/>[docker-compose.yml]"}}

node_compose -->|"runs"| node_api_service
node_compose -->|"runs after API health"| node_orchestrator
node_compose -->|"runs"| node_prometheus
node_compose -->|"runs"| node_grafana
node_video_inputs -->|"camera feeds"| node_orchestrator
node_orchestrator -->|"one thread per camera"| node_video_processor
node_person_detection -->|"detects people"| node_video_processor
node_video_processor -->|"detections"| node_local_tracking
node_local_tracking -->|"track observations"| node_cross_camera_reid
node_cross_camera_reid -->|"store-wide identity"| node_zone_classifier
node_zone_definitions -->|"polygon zones"| node_zone_classifier
node_zone_classifier -->|"zone transitions"| node_journey_state_machine
node_journey_state_machine -->|"normalized events"| node_api_service
node_api_service -->|"ingests"| node_event_ingestion
node_event_ingestion -->|"updates"| node_state_store
node_state_store -->|"current state"| node_kpi_service
node_kpi_service -->|"REST and live state"| node_api_service
node_api_service -->|"serves static app and WebSocket updates"| node_dashboard
node_api_service -->|"exposes /metrics"| node_prometheus
node_prometheus -->|"metrics"| node_grafana
node_alert_rules -->|"evaluated by"| node_prometheus

click node_compose "https://github.com/amanchauhan2005/purplee_hackathon/blob/main/docker-compose.yml"
click node_video_inputs "https://github.com/amanchauhan2005/purplee_hackathon/blob/main/data/sample/CAM_1.mp4"
click node_orchestrator "https://github.com/amanchauhan2005/purplee_hackathon/blob/main/run_all.py"
click node_video_processor "https://github.com/amanchauhan2005/purplee_hackathon/blob/main/tracker/video_processor.py"
click node_person_detection "https://github.com/amanchauhan2005/purplee_hackathon/blob/main/tracker/yolov8n.pt"
click node_local_tracking "https://github.com/amanchauhan2005/purplee_hackathon/blob/main/tracker/video_processor.py"
click node_cross_camera_reid "https://github.com/amanchauhan2005/purplee_hackathon/blob/main/tracker/reid.py"
click node_zone_classifier "https://github.com/amanchauhan2005/purplee_hackathon/blob/main/tracker/zone_config.py"
click node_zone_definitions "https://github.com/amanchauhan2005/purplee_hackathon/blob/main/tracker/zones_CAM_1.json"
click node_journey_state_machine "https://github.com/amanchauhan2005/purplee_hackathon/blob/main/tracker/state_machine.py"
click node_api_service "https://github.com/amanchauhan2005/purplee_hackathon/blob/main/api/main.py"
click node_event_ingestion "https://github.com/amanchauhan2005/purplee_hackathon/blob/main/api/events.py"
click node_state_store "https://github.com/amanchauhan2005/purplee_hackathon/blob/main/api/event_log.py"
click node_kpi_service "https://github.com/amanchauhan2005/purplee_hackathon/blob/main/api/compare.py"
click node_dashboard "https://github.com/amanchauhan2005/purplee_hackathon/blob/main/dashboard/app.js"
click node_prometheus "https://github.com/amanchauhan2005/purplee_hackathon/blob/main/monitoring/prometheus.yml"
click node_grafana "https://github.com/amanchauhan2005/purplee_hackathon/blob/main/monitoring/grafana/dashboards/retailiq-golden-signals.json"
click node_alert_rules "https://github.com/amanchauhan2005/purplee_hackathon/blob/main/monitoring/alerts.yml"

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_video_inputs,node_orchestrator,node_video_processor,node_person_detection,node_local_tracking,node_cross_camera_reid,node_zone_classifier,node_zone_definitions,node_journey_state_machine toneBlue
class node_api_service,node_event_ingestion,node_state_store,node_kpi_service,node_dashboard toneAmber
class node_prometheus,node_grafana,node_alert_rules toneMint
class node_compose toneNeutral
```

## Components

### Detector pipeline

One processing thread runs per camera stream.

- [Camera video streams](https://github.com/amanchauhan2005/purplee_hackathon/blob/main/data/sample/CAM_1.mp4): video input · Five sample or mounted CCTV feeds. · `data/sample/CAM_1.mp4`
- [Multi-camera runner](https://github.com/amanchauhan2005/purplee_hackathon/blob/main/run_all.py): orchestrator · Launches one processing thread per camera. · `run_all.py`
- [Per-stream processor](https://github.com/amanchauhan2005/purplee_hackathon/blob/main/tracker/video_processor.py): CV pipeline · Processes frames and submits normalized events. · `tracker/video_processor.py`
- [YOLO person detection](https://github.com/amanchauhan2005/purplee_hackathon/blob/main/tracker/yolov8n.pt): vision model · `tracker/yolov8n.pt`
- [Local track association](https://github.com/amanchauhan2005/purplee_hackathon/blob/main/tracker/video_processor.py): tracker · ByteTrack-style tracking within each camera. · `tracker/video_processor.py`
- [Cross-camera Re-ID](https://github.com/amanchauhan2005/purplee_hackathon/blob/main/tracker/reid.py): identity service · Uses handoff timing and HSV appearance to assign store-wide identities. · `tracker/reid.py`
- [Camera zone classification](https://github.com/amanchauhan2005/purplee_hackathon/blob/main/tracker/zone_config.py): spatial semantics · Classifies tracks against configured polygon zones. · `tracker/zone_config.py`
- [Camera zone definitions](https://github.com/amanchauhan2005/purplee_hackathon/blob/main/tracker/zones_CAM_1.json): zone config · `tracker/zones_CAM_1.json`
- [Journey event state machine](https://github.com/amanchauhan2005/purplee_hackathon/blob/main/tracker/state_machine.py): domain event engine · Converts zone transitions into retail journey events. · `tracker/state_machine.py`

### API and live state

FastAPI owns the authoritative in-memory store state.

- [FastAPI service](https://github.com/amanchauhan2005/purplee_hackathon/blob/main/api/main.py): service entry point · Exposes ingestion, REST, health, metrics, and WebSocket interfaces. · `api/main.py`
- [Event normalization](https://github.com/amanchauhan2005/purplee_hackathon/blob/main/api/events.py): ingestion layer · `api/events.py`
- [In-memory store state](https://github.com/amanchauhan2005/purplee_hackathon/blob/main/api/event_log.py): authoritative state · Current-store state has no persistence boundary. · `api/event_log.py`
- [KPI and comparison derivation](https://github.com/amanchauhan2005/purplee_hackathon/blob/main/api/compare.py): business metrics · Derives live KPIs, funnels, state, and comparisons. · `api/compare.py`
- [Live browser dashboard](https://github.com/amanchauhan2005/purplee_hackathon/blob/main/dashboard/app.js): static client · Presentation-only client receiving API updates. · `dashboard/app.js`

### Observability

- [Prometheus](https://github.com/amanchauhan2005/purplee_hackathon/blob/main/monitoring/prometheus.yml): metrics collector · Scrapes API metrics. · `monitoring/prometheus.yml`
- [Grafana dashboards](https://github.com/amanchauhan2005/purplee_hackathon/blob/main/monitoring/grafana/dashboards/retailiq-golden-signals.json): visualization · Displays golden signals and alerts. · `monitoring/grafana/dashboards/retailiq-golden-signals.json`
- [Alert rules](https://github.com/amanchauhan2005/purplee_hackathon/blob/main/monitoring/alerts.yml): monitoring rules · Detect latency, errors, CPU pressure, and stale ingestion. · `monitoring/alerts.yml`

### Other components

- [Docker Compose](https://github.com/amanchauhan2005/purplee_hackathon/blob/main/docker-compose.yml): deployment · Starts API, detector, Prometheus, and Grafana services. · `docker-compose.yml`

## Connections

- Docker Compose → FastAPI service: runs
- Docker Compose → Multi-camera runner: runs after API health
- Docker Compose → Prometheus: runs
- Docker Compose → Grafana dashboards: runs
- Camera video streams → Multi-camera runner: camera feeds
- Multi-camera runner → Per-stream processor: one thread per camera
- YOLO person detection → Per-stream processor: detects people
- Per-stream processor → Local track association: detections
- Local track association → Cross-camera Re-ID: track observations
- Cross-camera Re-ID → Camera zone classification: store-wide identity
- Camera zone definitions → Camera zone classification: polygon zones
- Camera zone classification → Journey event state machine: zone transitions
- Journey event state machine → FastAPI service: normalized events
- FastAPI service → Event normalization: ingests
- Event normalization → In-memory store state: updates
- In-memory store state → KPI and comparison derivation: current state
- KPI and comparison derivation → FastAPI service: REST and live state
- FastAPI service → Live browser dashboard: serves static app and WebSocket updates
- FastAPI service → Prometheus: exposes /metrics
- Prometheus → Grafana dashboards: metrics
- Alert rules → Prometheus: evaluated by
