# jnr-10/visor-video-instance-segmentation-and-object-retrieval architecture

> Architecture diagram of the GitHub repository jnr-10/visor-video-instance-segmentation-and-object-retrieval, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval
- Repository: https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval

## Overview

1. **Repo shape**
   - VISOR is a two-tier application: a **FastAPI backend** that handles video ingestion and ML processing, and a **Next.js 14 frontend** that presents the video, overlays detections, and product matches.
   - The system is designed to work in a mostly **stateless local-file mode**, with an optional database layer present but not the primary runtime dependency.

2. **User-facing flow**
   - The main path is: **upload video → backend stores it → processing pipeline runs → frontend polls status and renders interactive results**.
   - The frontend is not doing ML itself; it is a thin presentation layer that consumes backend APIs and draws overlays on top of video playback.

3. **Backend API boundary**
   - The backend is organized around small route modules in `backend/app/api/`: `videos`, `process`, `events`, and `health`.
   - These separate concerns cleanly:
     - `videos` for upload/storage
     - `process` for starting and monitoring analysis
     - `events` for analytics/logging
     - `health` for readiness checks

4. **Processing pipeline boundary**
   - The core domain logic lives in `backend/app/services/`.
   - `video_processor.py` appears to orchestrate the end-to-end pipeline, while:
     - `frame_extractor.py` handles frame sampling
     - `grounding_dino_service.py` performs zero-shot object detection
     - `segmentation_service.py` performs pixel-level masking
     - `product_search.py` maps detected objects to shopping results
   - This is the main architectural boundary: the API layer triggers processing, but services own the ML/video workflow.

5. **ML/model subsystem**
   - The repo is centered on a detection stack using **OWL-ViT / Grounding-style zero-shot detection**, **SAM 2 segmentation**, and optional **CLIP-based product embeddings**.
   - These are not embedded in the frontend; they are backend-only model integrations, likely loaded from `data/models/` and chosen based on the configured device (`cuda`, `mps`, or `cpu`).

6. **State and storage**
   - Runtime artifacts are stored on the local filesystem under `data/`, with the README indicating folders for videos, frames, crops, masks, and tracking outputs.
   - This makes the backend portable and supports the stated “stateless mode,” where processing can happen without a mandatory database.
   - `backend/app/core/cache.py` likely provides transient progress/status tracking, which fits the real-time processing UX.

7. **Database boundary**
   - There is a database layer in `backend/app/core/database.py` plus ORM-style models in `backend/app/models/db_models.py`, and SQL scripts in `scripts/init_db.sql` and `scripts/add_category_column.sql`.
   - However, the README explicitly says the database is **optional / currently disabled for portability**, so in the current architecture it should be treated as a secondary capability rather than a required subsystem.

8. **Frontend boundary**
   - The frontend is a **Next.js App Router** application in `frontend/src/app/`.
   - Key UI components are:
     - `VideoPlayer.tsx` for playback and overlay interaction
     - `ProductDrawer.tsx` for showing matched shopping results
   - `frontend/src/lib/api.ts` is the API boundary between the UI and the backend, and `types/` defines the shared response shapes expected from the backend.

9. **Interaction model**
   - The frontend likely polls the backend for processing progress and retrieved tracking data, rather than receiving a live stream.
   - The backend exposes a progress/status endpoint and logs, so the UX is driven by **incremental status updates** and then **interactive object selection** once processing completes.

10. **External service integration**
   - The only clearly material external service is **SerpAPI**, used by `product_search.py` to return real shopping results.
   - This means product matching is a hybrid of local detection and external search, with the search call sitting behind the backend so the frontend never talks directly to SerpAPI.

11. **Observability and health**
   - The `events` endpoint provides application-level analytics, such as object clicks, which can be used for usage tracking or product interaction metrics.
   - `health` supports operational checks for deployment and startup verification.

12. **Operational/tooling layer**
   - The repo includes setup and validation scripts (`verify_env.py`, `test_processing.py`, SQL init scripts), which suggests the project is meant to be run both locally and in reproducible dev environments.
   - The stack materially depends on **Python 3.10+**, **FastAPI**, **Next.js 14**, **OpenCV/FFmpeg**, and optional GPU acceleration for acceptable processing speed.

13. **How to draw the architecture**
   - A clear diagram should show:
     - **Browser/UI**
     - **Next.js frontend**
     - **FastAPI backend**
     - **Local storage under `data/`**
     - **ML processing pipeline/services**
     - **Optional PostgreSQL**
     - **External SerpAPI**
   - The main arrows are:
     - upload from frontend to backend
     - processing from API into services/models
     - artifact writes to filesystem
     - status/result reads back to frontend
     - product queries out to SerpAPI

Main entry points: [`backend/app/services/video_processor.py`](https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/backend/app/services/video_processor.py) (Video Processor), [`backend/app/main.py`](https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/backend/app/main.py) (FastAPI App), [`frontend/src/lib/api.ts`](https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/frontend/src/lib/api.ts) (API Client), [`frontend/src/app`](https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/tree/main/frontend/src/app) (Next.js App)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["Browser & UI"]
  node_browser(("Browser<br/>ui client"))
end

subgraph group_frontend["Frontend"]
  node_next_app["Next.js App<br/>frontend app"]
  node_video_player["Video Player<br/>ui component<br/>[VideoPlayer.tsx]"]
  node_product_drawer["Product Drawer<br/>ui component<br/>[ProductDrawer.tsx]"]
  node_api_client["API Client<br/>frontend boundary<br/>[api.ts]"]
end

subgraph group_backend_api["Backend API"]
  node_api_main["FastAPI App<br/>backend app<br/>[main.py]"]
  node_videos_api["Videos API<br/>route<br/>[videos.py]"]
  node_process_api["Process API<br/>route<br/>[process.py]"]
  node_events_api["Events API<br/>route<br/>[events.py]"]
  node_health_api["Health API<br/>route<br/>[health.py]"]
end

subgraph group_backend_core["Backend Core"]
  node_config["Config<br/>core settings<br/>[config.py]"]
  node_cache["Progress Cache<br/>core state<br/>[cache.py]"]
end

subgraph group_pipeline["Processing Pipeline"]
  node_video_processor["Video Processor<br/>orchestrator<br/>[video_processor.py]"]
  node_frame_extractor["Frame Extractor<br/>pipeline stage<br/>[frame_extractor.py]"]
  node_dino_service["Grounding DINO<br/>detection service"]
  node_segmentation_service["Segmentation<br/>mask service"]
  node_product_search["Product Search<br/>retrieval service<br/>[product_search.py]"]
end

subgraph group_storage["Storage"]
  node_data_store[("Data Files<br/>local storage")]
  node_db_layer[("Database Layer<br/>optional database<br/>[database.py]")]
  node_serpapi(("SerpAPI<br/>external search"))
end

subgraph group_ops["Operations"]
  node_scripts["Ops Scripts<br/>tooling"]
end

node_browser -->|"use"| node_next_app
node_next_app -->|"calls"| node_api_client
node_api_client -->|"upload"| node_videos_api
node_api_client -->|"start/status"| node_process_api
node_api_client -->|"log"| node_events_api
node_api_client -->|"check"| node_health_api
node_next_app -->|"renders"| node_video_player
node_next_app -->|"renders"| node_product_drawer
node_api_main -->|"mounts"| node_videos_api
node_api_main -->|"mounts"| node_process_api
node_api_main -->|"mounts"| node_events_api
node_api_main -->|"mounts"| node_health_api
node_videos_api -->|"writes"| node_data_store
node_process_api -->|"updates"| node_cache
node_process_api -->|"triggers"| node_video_processor
node_video_processor -->|"uses"| node_frame_extractor
node_video_processor -->|"uses"| node_dino_service
node_video_processor -->|"uses"| node_segmentation_service
node_video_processor -->|"uses"| node_product_search
node_video_processor -->|"writes"| node_data_store
node_product_search -->|"queries"| node_serpapi
node_video_processor -.->|"optional write"| node_db_layer
node_events_api -.->|"optional write"| node_db_layer
node_health_api -->|"reads"| node_config
node_api_main -->|"reads"| node_config
node_scripts -->|"initializes"| node_db_layer

click node_next_app "https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/tree/main/frontend/src/app"
click node_video_player "https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/frontend/src/components/VideoPlayer.tsx"
click node_product_drawer "https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/frontend/src/components/ProductDrawer.tsx"
click node_api_client "https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/frontend/src/lib/api.ts"
click node_api_main "https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/backend/app/main.py"
click node_videos_api "https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/backend/app/api/videos.py"
click node_process_api "https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/backend/app/api/process.py"
click node_events_api "https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/backend/app/api/events.py"
click node_health_api "https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/backend/app/api/health.py"
click node_config "https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/backend/app/core/config.py"
click node_cache "https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/backend/app/core/cache.py"
click node_video_processor "https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/backend/app/services/video_processor.py"
click node_frame_extractor "https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/backend/app/services/frame_extractor.py"
click node_dino_service "https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/backend/app/services/grounding_dino_service.py"
click node_segmentation_service "https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/backend/app/services/segmentation_service.py"
click node_product_search "https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/backend/app/services/product_search.py"
click node_data_store "https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/tree/main/data"
click node_db_layer "https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/backend/app/core/database.py"
click node_scripts "https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/tree/main/scripts"

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_browser,node_scripts toneBlue
class node_next_app,node_video_player,node_product_drawer,node_api_client toneAmber
class node_api_main,node_videos_api,node_process_api,node_events_api,node_health_api toneMint
class node_config,node_cache toneRose
class node_video_processor,node_frame_extractor,node_dino_service,node_segmentation_service,node_product_search toneIndigo
class node_data_store,node_db_layer,node_serpapi toneTeal
```

## Components

### Browser & UI

User-facing playback and results presentation

- **Browser**: ui client · User interface for uploading videos, viewing overlays, and exploring matches

### Frontend

Next.js application and client API layer

- [Next.js App](https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/tree/main/frontend/src/app): frontend app · App Router frontend that renders the experience and polls backend results · `frontend/src/app`
- [Video Player](https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/frontend/src/components/VideoPlayer.tsx): ui component · Plays video and renders interactive overlays · `frontend/src/components/VideoPlayer.tsx`
- [Product Drawer](https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/frontend/src/components/ProductDrawer.tsx): ui component · Shows matched shopping results and details · `frontend/src/components/ProductDrawer.tsx`
- [API Client](https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/frontend/src/lib/api.ts): frontend boundary · Frontend-to-backend request layer and response handling · `frontend/src/lib/api.ts`

### Backend API

FastAPI route boundary

- [FastAPI App](https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/backend/app/main.py): backend app · Backend application entrypoint and route assembly · `backend/app/main.py`
- [Videos API](https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/backend/app/api/videos.py): route · Video upload and storage endpoints · `backend/app/api/videos.py`
- [Process API](https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/backend/app/api/process.py): route · Start and monitor analysis processing · `backend/app/api/process.py`
- [Events API](https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/backend/app/api/events.py): route · Analytics and interaction logging endpoints · `backend/app/api/events.py`
- [Health API](https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/backend/app/api/health.py): route · Readiness and operational checks · `backend/app/api/health.py`

### Backend Core

Runtime state and configuration

- [Config](https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/backend/app/core/config.py): core settings · Environment and runtime configuration · `backend/app/core/config.py`
- [Progress Cache](https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/backend/app/core/cache.py): core state · Transient progress and status tracking · `backend/app/core/cache.py`

### Processing Pipeline

Video, detection, segmentation, and product matching

- [Video Processor](https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/backend/app/services/video_processor.py): orchestrator · Coordinates the end-to-end analysis workflow · `backend/app/services/video_processor.py`
- [Frame Extractor](https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/backend/app/services/frame_extractor.py): pipeline stage · Samples frames from uploaded video · `backend/app/services/frame_extractor.py`
- [Grounding DINO](https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/backend/app/services/grounding_dino_service.py): detection service · Zero-shot object detection integration · `backend/app/services/grounding_dino_service.py`
- [Segmentation](https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/backend/app/services/segmentation_service.py): mask service · Pixel-level masking and instance refinement · `backend/app/services/segmentation_service.py`
- [Product Search](https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/backend/app/services/product_search.py): retrieval service · Maps detections to shopping results via external search · `backend/app/services/product_search.py`

### Storage

Local artifacts and optional database

- [Data Files](https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/tree/main/data): local storage · Videos, frames, crops, masks, and tracking artifacts · `data`
- [Database Layer](https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/blob/main/backend/app/core/database.py): optional database · Optional persistence layer for enabled deployments · `backend/app/core/database.py`
- **SerpAPI**: external search · External shopping search provider used for product matching

### Operations

Setup, validation, and maintenance scripts

- [Ops Scripts](https://github.com/jnr-10/visor-video-instance-segmentation-and-object-retrieval/tree/main/scripts): tooling · Environment verification, processing tests, and SQL setup · `scripts`

## Connections

- Browser → Next.js App: use · Users interact with the web app
- Next.js App → API Client: calls · UI requests backend data and actions
- API Client → Videos API: upload · Uploads video files
- API Client → Process API: start/status · Starts processing and polls progress
- API Client → Events API: log · Sends interaction analytics
- API Client → Health API: check · Checks backend readiness
- Next.js App → Video Player: renders · Displays playback and overlays
- Next.js App → Product Drawer: renders · Displays product matches
- FastAPI App → Videos API: mounts · Registers video routes
- FastAPI App → Process API: mounts · Registers process routes
- FastAPI App → Events API: mounts · Registers event routes
- FastAPI App → Health API: mounts · Registers health routes
- Videos API → Data Files: writes · Stores uploaded media locally
- Process API → Progress Cache: updates · Exposes progress and status
- Process API → Video Processor: triggers · Starts the analysis pipeline
- Video Processor → Frame Extractor: uses · Extracts frames for analysis
- Video Processor → Grounding DINO: uses · Detects objects with zero-shot models
- Video Processor → Segmentation: uses · Builds masks for detected instances
- Video Processor → Product Search: uses · Finds candidate products
- Video Processor → Data Files: writes · Persists frames, masks, and tracking outputs
- Product Search → SerpAPI: queries · Fetches external shopping results
- Video Processor → Database Layer: optional write · Persists metadata when database is enabled
- Events API → Database Layer: optional write · Stores analytics when persistence is enabled
- Health API → Config: reads · Uses runtime configuration for readiness checks
- FastAPI App → Config: reads · Loads application settings
- Ops Scripts → Database Layer: initializes · Prepares optional database schema
