# buvi07/deeplivecam architecture

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

- Interactive diagram: https://gitdiagram.com/buvi07/deeplivecam
- Repository: https://github.com/buvi07/deeplivecam

## Overview

##### Project type & purpose (inferred from file tree)
This is a **local, GPU-accelerated real-time video processing application** (likely “DeepLiveCam” style) that:
- **Captures video frames** (webcam / video input),
- **Analyzes faces** (detection/landmarks/embedding),
- Runs one or more **frame processors** (e.g., **face swap**, **face enhancement**),
- **Renders / outputs** processed frames (preview window and/or video file via ffmpeg),
- Supports multiple compute backends (**CUDA**, **DirectML**) and model export (**GFPGAN → ONNX**).

##### What the file structure says about architecture
Top-level indicators:
- **Entry points / scripts**
- `run.py` (main launcher)
- `run-cuda.bat`, `run-directml.bat` (backend selection / environment bootstrapping)
- `dmll.py`, `torchdml.py` (DirectML-related runtime glue)
- `export_gfpgan_to_onnx.py` (model conversion pipeline)
- **Core application package**
- `modules/core.py` (likely orchestration / main loop)
- `modules/globals.py` (global config/state)
- `modules/metadata.py` (build/run metadata)
- `modules/ui.py`, `modules/ui.json` (UI layer + UI schema)
- `modules/gettext.py`, `locales/*` (i18n)
- **Video ingestion**
- `modules/capturer.py`
- `modules/video_capture.py`
- **Face intelligence**
- `modules/face_analyser.py`
- `modules/predicter.py` (model inference wrapper)
- **Processing pipeline**
- `modules/processors/frame/*`
- `face_swapper.py`
- `face_enhancer.py`
- `core.py` (processor interface / chaining)
- **Utilities / external tool integration**
- `modules/utilities/ffmpeg_utils.py` (ffmpeg encode/decode)
- `modules/utilities/status.py` (status/progress reporting)
- **Artifacts / I/O**
- `frames_raw/`, `frames_processed/` (debug/export directories)
- `media/` (sample inputs/outputs)
- `models/` (model weights, instructions)
- **Dependencies**
- `requirements.txt`

Architecturally, this looks like a **single-process application** with a **streaming pipeline** (capture → analyze → process → render/encode), with optional compute backends.

##### Frame Processor Chain
- `face_swapper` (swap face region)
- `face_enhancer` (GFPGAN-style enhancement)
- any shared processor `core` for ordering/enable flags
##### (Optional) Encode/Save via FFmpeg

Runtime choices:
- **CUDA path** (`run-cuda.bat`) → PyTorch/TensorRT/ONNXRuntime-GPU (whatever is actually used; if unknown, label generically “CUDA Inference Backend”)
- **DirectML path** (`run-directml.bat`, `torchdml.py`, `dmll.py`) → “DirectML backend”
- **CPU fallback** (if applicable; if not sure, mark as “optional/unknown”)

##### Entry / Lifecycle
- **Launcher**: `run.py`
- **Backend launch scripts**: `run-cuda.bat`, `run-directml.bat`
- **Global config/state**: `modules/globals.py`, `mypi.ini`

##### UI & Interaction
- **UI**: `modules/ui.py`
- **UI schema/config**: `modules/ui.json` (and `modules/utilities/ui.json` if used)
- **Localization**: `modules/gettext.py`, `locales/zh.json`

##### / Input
- **Capture abstraction**: `modules/capturer.py`
- **Low-level capture**: `modules/video_capture.py`

##### Analysis & Inference
- **Face analysis**: `modules/face_analyser.py`
- **Model inference wrapper**: `modules/predicter.py`
- **Compute backend adapters**: `torchdml.py`, `dmll.py` (DirectML path)

##### Processing
- **Frame processor framework**: `modules/processors/frame/core.py`
- **Face swap**: `modules/processors/frame/face_swapper.py`
- **Face enhancement**: `modules/processors/frame/face_enhancer.py`

##### External tool integration & outputs
- **FFmpeg integration**: `modules/utilities/ffmpeg_utils.py`
- **Status/progress**: `modules/utilities/status.py`
- **Outputs**: `frames_raw/`, `frames_processed/`, `media/`
- **Models**: `models/` (+ `models/instructions.txt`)
- **Model export utility**: `export_gfpgan_to_onnx.py`

##### Architectural patterns worth making explicit
- **Pipeline / streaming processing**: capture → analyze → process → output.
- **Pluggable processors**: `modules/processors/frame/*` suggests a processor interface and a chain (turn processors on/off).
- **Backend abstraction**: multiple inference backends (CUDA vs DirectML) behind `predicter.py` (and DirectML glue).
- **Separation of concerns**:
- Isolated from processing,
- analysis isolated from transformation,
- UI isolated from core loop.

##### Diagramming conventions to use (so it’s readable at principal level)
Include:
- **Clear labels**: use module names + responsibility, e.g. “FaceAnalyser (modules/face_analyser.py)”
- Rounded rectangles: in-process modules (Python)

Also separate **control flow** vs **data flow**:

##### “As many components as possible” checklist (what to split out)
To be very detailed (as requested), split at least into:
- UI
- Core loop
- Device abstraction
- Face analyzer
- Processor framework
- Face swapper
- Face enhancer
- Predictor/inference wrapper
- Backend (CUDA vs DirectML)
- Model store
- FFmpeg tool
- Output directories
- i18n/locales
- Status/progress utility

That level of decomposition will capture the essential architecture without overthinking internal functions.

## Diagram (Mermaid)

```mermaid
flowchart TD

  %% =========================
  %% Legend (keep vertical)
  %% =========================
  subgraph "Legend"
    direction TB
    L1["Python module (in-process)"]:::py
    L2[("Storage / Directories")]:::store
    L3{{"External tool / binary"}}:::external
    L4["GPU / Backend adapter"]:::backend
    L5["Solid arrow = data flow"]:::note
    L6["Dashed arrow = control/config"]:::note
  end

  %% =========================
  %% Entry & Runtime Selection
  %% =========================
  subgraph "Entry & Backend Selection"
    direction TB
    RunBatCUDA["run-cuda.bat (CUDA launcher)"]:::entry
    RunBatDML["run-directml.bat (DirectML launcher)"]:::entry
    RunPy["run.py (Launcher)"]:::entry
    Meta["Metadata (build/run)"]:::support
  end

  RunBatCUDA -.->|"starts"| RunPy
  RunBatDML -.->|"starts"| RunPy
  RunPy -.->|"reads"| Meta

  %% =========================
  %% Application (single process)
  %% =========================
  subgraph "Application Process (Python)"
    direction TB

    Globals["Globals / Config State"]:::support
    Ini[("mypi.ini")]:::store

    subgraph "UI & Localization"
      direction TB
      UI["UI Layer"]:::ui
      UISchema["UI Schema/Config (ui.json)"]:::support
      UIUtilsSchema["UI Utilities Schema (utilities/ui.json)"]:::support
      I18N["gettext / i18n"]:::support
      Locales[("locales/")]:::store
      LocaleZH["zh.json"]:::support
      Status["Status / Progress"]:::support
    end

    subgraph "Core Orchestration"
      direction TB
      Core["Core Orchestrator / Main Loop"]:::core
      Types["Shared Typing Definitions"]:::support
    end

    subgraph "Capture Subsystem"
      direction TB
      Capturer["Capture Abstraction"]:::capture
      VideoCap["Low-level Video Capture"]:::capture
      VideoSrc["Video Source (webcam/file/device)"]:::external
    end

    subgraph "Analysis Subsystem"
      direction TB
      FaceAnalyser["Face Analyser"]:::analysis
      Cluster["Cluster Analysis (supporting analytics)"]:::analysis
    end

    subgraph "Frame Processing Subsystem (pluggable chain)"
      direction TB
      FPChain["Frame Processor Framework / Chain Core"]:::processor
      Swapper["Face Swapper (frame processor)"]:::processor
      Enhancer["Face Enhancer (GFPGAN-style)"]:::processor
      NoteProc["Note: processors are enabled/ordered via settings"]:::note
    end

    subgraph "Inference & Backends"
      direction TB
      Predicter["Predicter (model inference wrapper)"]:::inference

      subgraph "Backend Adapters"
        direction TB
        CUDA["CUDA Inference Backend (generic)"]:::backend
        DMLTorch["torchdml (DirectML adapter)"]:::backend
        DMLGlue["dmll (DirectML runtime glue)"]:::backend
      end

      Models[("models/ (weights)")]:::store
      ModelsInstr["instructions.txt"]:::support
    end

    subgraph "Output / Export"
      direction TB
      Preview["Preview / Render (UI)"]:::ui
      FFmpeg{{"FFmpeg (external encoder/decoder)"}}:::external
      RawFrames[("frames_raw/")]:::store
      ProcFrames[("frames_processed/")]:::store
      Media[("media/")]:::store
      ExportONNX["Export GFPGAN to ONNX Utility"]:::tool
    end
  end

  %% =========================
  %% Control flow (dashed)
  %% =========================
  UI -.->|"start/stop,settings,toggles"| Core
  Globals -.->|"global_state"| Core
  Ini -.->|"config"| Globals
  I18N -.->|"localized_strings"| UI
  Locales -.->|"resource_lookup"| I18N
  LocaleZH -.->|"language_pack"| Locales
  UISchema -.->|"ui_definition"| UI
  UIUtilsSchema -.->|"shared_widgets"| UI
  Status -.->|"progress_updates"| UI
  Types -.->|"type_contracts"| Core

  %% =========================
  %% Dataflow pipeline (solid)
  %% =========================
  VideoSrc -->|"video_frames"| VideoCap
  VideoCap -->|"frames"| Capturer
  Capturer -->|"frame (ndarray/tensor)"| Core

  Core -->|"frame"| FaceAnalyser
  FaceAnalyser -->|"faces:bboxes/landmarks/embeddings"| Core

  Core -->|"frame+face_metadata+settings"| FPChain
  FPChain -->|"dispatch(frame)"| Swapper
  FPChain -->|"dispatch(frame)"| Enhancer
  Swapper -->|"inference_calls"| Predicter
  Enhancer -->|"inference_calls"| Predicter
  Predicter -->|"generated_face/enhanced_patch/masks"| FPChain

  Core -->|"preview_frame,FPS,status"| Preview
  Preview -->|"display"| UI

  %% Optional persistence/debug
  Core -->|"write_raw_frames(optional)"| RawFrames
  Core -->|"write_processed_frames(optional)"| ProcFrames

  %% Encode/Save
  Core -->|"frames_stream/encode_cmd(optional)"| FFmpeg
  FFmpeg -->|"encoded_video/output"| Media

  %% =========================
  %% Dependencies (models/backends)
  %% =========================
  Predicter -->|"load_weights"| Models
  ModelsInstr -.->|"model_setup_guidance"| Models

  Predicter -->|"use_backend"| CUDA
  Predicter -->|"use_backend"| DMLTorch
  DMLTorch -->|"calls_into"| DMLGlue

  %% Analytics support
  FaceAnalyser -->|"optional_face_features"| Cluster
  Cluster -.->|"insights/grouping"| Core

  %% Model export utility
  ExportONNX -->|"read_models"| Models
  ExportONNX -->|"write_onnx(optional)"| Models

  %% =========================
  %% Click Events (paths only)
  %% =========================
  click RunPy "https://github.com/buvi07/deeplivecam/blob/main/run.py"
  click RunBatCUDA "https://github.com/buvi07/deeplivecam/blob/main/run-cuda.bat"
  click RunBatDML "https://github.com/buvi07/deeplivecam/blob/main/run-directml.bat"
  click Core "https://github.com/buvi07/deeplivecam/blob/main/modules/core.py"
  click Globals "https://github.com/buvi07/deeplivecam/blob/main/modules/globals.py"
  click Ini "https://github.com/buvi07/deeplivecam/blob/main/mypi.ini"
  click Meta "https://github.com/buvi07/deeplivecam/blob/main/modules/metadata.py"
  click UI "https://github.com/buvi07/deeplivecam/blob/main/modules/ui.py"
  click UISchema "https://github.com/buvi07/deeplivecam/blob/main/modules/ui.json"
  click UIUtilsSchema "https://github.com/buvi07/deeplivecam/blob/main/modules/utilities/ui.json"
  click I18N "https://github.com/buvi07/deeplivecam/blob/main/modules/gettext.py"
  click Locales "https://github.com/buvi07/deeplivecam/tree/main/locales/"
  click LocaleZH "https://github.com/buvi07/deeplivecam/blob/main/locales/zh.json"
  click Capturer "https://github.com/buvi07/deeplivecam/blob/main/modules/capturer.py"
  click VideoCap "https://github.com/buvi07/deeplivecam/blob/main/modules/video_capture.py"
  click FaceAnalyser "https://github.com/buvi07/deeplivecam/blob/main/modules/face_analyser.py"
  click Predicter "https://github.com/buvi07/deeplivecam/blob/main/modules/predicter.py"
  click DMLTorch "https://github.com/buvi07/deeplivecam/blob/main/torchdml.py"
  click DMLGlue "https://github.com/buvi07/deeplivecam/blob/main/dmll.py"
  click FPChain "https://github.com/buvi07/deeplivecam/blob/main/modules/processors/frame/core.py"
  click Swapper "https://github.com/buvi07/deeplivecam/blob/main/modules/processors/frame/face_swapper.py"
  click Enhancer "https://github.com/buvi07/deeplivecam/blob/main/modules/processors/frame/face_enhancer.py"
  click Status "https://github.com/buvi07/deeplivecam/blob/main/modules/utilities/status.py"
  click Models "https://github.com/buvi07/deeplivecam/tree/main/models/"
  click ModelsInstr "https://github.com/buvi07/deeplivecam/blob/main/models/instructions.txt"
  click ExportONNX "https://github.com/buvi07/deeplivecam/blob/main/export_gfpgan_to_onnx.py"
  click RawFrames "https://github.com/buvi07/deeplivecam/tree/main/frames_raw/"
  click ProcFrames "https://github.com/buvi07/deeplivecam/tree/main/frames_processed/"
  click Media "https://github.com/buvi07/deeplivecam/tree/main/media/"
  click Cluster "https://github.com/buvi07/deeplivecam/blob/main/modules/cluster_analysis.py"
  click Types "https://github.com/buvi07/deeplivecam/blob/main/modules/typing.py"

  %% =========================
  %% Styles (color is required)
  %% =========================
  classDef entry fill:#1f2937,stroke:#111827,color:#ffffff,stroke-width:1px
  classDef core fill:#2563eb,stroke:#1e40af,color:#ffffff,stroke-width:1px
  classDef ui fill:#7c3aed,stroke:#5b21b6,color:#ffffff,stroke-width:1px
  classDef capture fill:#0ea5e9,stroke:#075985,color:#ffffff,stroke-width:1px
  classDef analysis fill:#10b981,stroke:#047857,color:#ffffff,stroke-width:1px
  classDef processor fill:#f59e0b,stroke:#b45309,color:#111827,stroke-width:1px
  classDef inference fill:#ef4444,stroke:#991b1b,color:#ffffff,stroke-width:1px
  classDef backend fill:#fb7185,stroke:#9f1239,color:#111827,stroke-width:1px
  classDef store fill:#94a3b8,stroke:#475569,color:#111827,stroke-width:1px
  classDef external fill:#111827,stroke:#000000,color:#ffffff,stroke-width:1px
  classDef support fill:#22c55e,stroke:#15803d,color:#052e16,stroke-width:1px
  classDef tool fill:#6366f1,stroke:#3730a3,color:#ffffff,stroke-width:1px
  classDef note fill:#f1f5f9,stroke:#cbd5e1,color:#0f172a,stroke-dasharray: 4 3
```
