Skip to content

NihanthBhargav/Store-Intelligence

Repository files navigation

Store Intelligence System — Purplle Challenge 2026

A complete end-to-end system that transforms raw CCTV footage from physical retail stores into real-time business intelligence, specifically offline conversion rates.

Quick Start (5 Commands)

# 1. Clone and navigate
cd store-intelligence

# 2. Install dependencies
pip install -r requirements.txt

# 3. Download YOLOv8n weights
python -c "from ultralytics import YOLO; YOLO('yolov8n.pt')"

# 4. Start API (with Docker Compose)
docker compose up

# 5. Send events
curl -X POST http://localhost:8000/events/ingest \
  -H "Content-Type: application/json" \
  -d '{"events": []}'

Project Structure

store-intelligence/
├── data/                    ← CCTV clips, POS data, store layout
├── pipeline/                ← Detection pipeline (YOLO + ByteTrack)
├── app/                     ← FastAPI intelligence API
├── tests/                   ← Pytest test suite
├── docs/                    ← DESIGN.md, CHOICES.md
├── dashboard/               ← Live dashboard (bonus)
├── docker-compose.yml       ← One-command system startup
├── Dockerfile               ← Container definition
├── requirements.txt         ← Python dependencies
└── README.md               ← This file

Pipeline Stages

Stage 1: Detection (pipeline/detect.py)

  • YOLOv8n person detection on each CCTV frame
  • ByteTrack persistent person tracking (track_id per visitor)
  • Re-ID logic to detect re-entries
  • Emits structured JSON events

Stage 2: Event Schema (pipeline/emit.py)

  • 8 event types: ENTRY, EXIT, ZONE_ENTER, ZONE_EXIT, ZONE_DWELL, etc.
  • Strict Pydantic validation with UUID, timestamps, session_seq ordering

Stage 3: Intelligence API (app/main.py)

  • /events/ingest — Batch event ingestion with deduplication
  • /stores/{id}/metrics — Real-time conversion rate, visitor count, dwell time
  • /stores/{id}/funnel — Entry → Zone → Billing → Purchase funnel
  • /stores/{id}/heatmap — Zone frequency and dwell analysis
  • /stores/{id}/anomalies — Queue spikes, conversion drops, dead zones
  • /health — Service status and feed staleness monitoring

Stage 4: Production Readiness

  • Docker Compose with API + SQLite
  • Structured logging on every request (trace_id, latency_ms, status_code)
  • 70%+ pytest coverage with edge case handling
  • Graceful error responses (503 on DB unavailable)

Stage 5: AI Documentation

  • DESIGN.md — Architecture overview (2–3 AI-assisted decisions)
  • CHOICES.md — 3 key architectural decisions

Stage 6: Live Dashboard (Bonus)

  • Terminal UI (rich library) or Web UI showing real-time metrics

📚 Documentation

🚀 Quick Demo

# 1. Start API
python -m uvicorn app.main:app --port 8000

# 2. Run end-to-end demo
python scripts/demo.py

# 3. Start dashboard
streamlit run dashboard/streamlit_app.py
# → Open http://localhost:8501

API Examples

Ingest events

curl -X POST http://localhost:8000/events/ingest \
  -H "Content-Type: application/json" \
  -d '{
    "events": [
      {
        "event_id": "550e8400-e29b-41d4-a716-446655440000",
        "store_id": "STORE_BLR_002",
        "camera_id": "CAM_ENTRY_01",
        "visitor_id": "VIS_c8a2f1",
        "event_type": "ENTRY",
        "timestamp": "2026-03-03T14:22:10Z",
        "zone_id": null,
        "dwell_ms": 0,
        "is_staff": false,
        "confidence": 0.95,
        "metadata": {"queue_depth": null, "sku_zone": null, "session_seq": 1}
      }
    ]
  }'

Get metrics

curl http://localhost:8000/stores/STORE_BLR_002/metrics

Response:

{
  "unique_visitors": 145,
  "conversion_rate": 0.38,
  "avg_dwell_ms": 480,
  "queue_depth_current": 3,
  "abandonment_rate": 0.12
}

Get funnel

curl http://localhost:8000/stores/STORE_BLR_002/funnel

Response:

{
  "entry": 145,
  "zone_visit": 132,
  "billing_queue": 85,
  "purchased": 55
}

Get heatmap

curl http://localhost:8000/stores/STORE_BLR_002/heatmap

Response:

{
  "SKINCARE": {"frequency": 87, "avg_dwell_ms": 420, "normalized": 85},
  "MAKEUP": {"frequency": 62, "avg_dwell_ms": 310, "normalized": 58}
}

Check health

curl http://localhost:8000/health

Response:

{
  "status": "healthy",
  "stores": {
    "STORE_BLR_002": {"last_event_timestamp": "2026-03-03T14:22:10Z", "lag_minutes": 2}
  },
  "warnings": []
}

Conversion Rate Calculation

The North Star Metric this system tracks:

Conversion Rate = (Visitors who purchased) / (Total unique visitors)

Logic:

  1. Count unique visitor_id with ENTRY events
  2. Correlate with POS transactions: if visitor was in billing zone within 5 min before transaction → purchased
  3. Calculate: conversion_rate = purchased_count / total_visitors

Edge Cases Handled

  • Group Entry: Multiple people detected separately (3 ENTRY events for 3 people)
  • Staff: Uniform color heuristic → flag is_staff: true (excluded from conversion calculation)
  • Re-entry: Person exits then returns → REENTRY event with same visitor_id
  • Partial Occlusion: Emit event with low confidence, don't silently drop
  • Billing Queue: Track queue_depth in real-time as integer
  • Empty Store: Return zero counts gracefully, no crashes
  • Camera Overlap: Acknowledged; cross-camera Re-ID optional

Logging & Monitoring

Every API request includes structured logging:

{
  "event": "request_complete",
  "timestamp": "2026-03-03T14:22:10Z",
  "trace_id": "550e8400-e29b-41d4-a716-446655440000",
  "endpoint": "/events/ingest",
  "method": "POST",
  "status_code": 200,
  "latency_ms": 142,
  "inserted_events": 495,
  "duplicates": 3,
  "errors": 2
}

Testing

# Run all tests with coverage
pytest --cov=app --cov=pipeline tests/

# Run specific test file
pytest tests/test_metrics.py -v

# Run with print statements
pytest tests/ -s

Acceptance Gates

  1. System Execution: docker compose up starts without errors
  2. API Responsiveness: POST /events/ingest and GET /metrics return valid JSON
  3. Event Generation: Detection pipeline outputs JSON with correct schema
  4. Documentation: DESIGN.md and CHOICES.md both >250 words
  5. Stability: No crashes during basic execution

Key Tech Stack

  • Detection: YOLOv8n + ByteTrack (person tracking)
  • API: FastAPI + Uvicorn (async web server)
  • Database: SQLite + SQLAlchemy ORM
  • Testing: pytest + pytest-cov
  • Logging: structlog (structured JSON logs)
  • Dashboard: rich (terminal UI) or HTML+JS (web UI)
  • Container: Docker + Docker Compose

All tools are 100% open source and free.

Next Steps

  1. Place CCTV clips in data/clips/
  2. Place POS transactions CSV in data/pos_transactions.csv
  3. Convert Excel store layout to data/store_layout.json
  4. Run docker compose up to start the API
  5. Run detection pipeline to generate events
  6. Ingest events via POST /events/ingest
  7. Query metrics via GET endpoints

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors