# vladmandic/human architecture

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

- Interactive diagram: https://gitdiagram.com/vladmandic/human
- Repository: https://github.com/vladmandic/human

## Overview

##### Project Type and Purpose
- This is a TypeScript/JavaScript AI SDK (“Human Library”) that provides a unified API for running multiple ML models (face detection, body pose, hand tracking, emotion, segmentation, etc.) on both browser (WebGL/WebGPU/WASM/CPU) and NodeJS (tfjs-node CPU/GPU) environments.
- The project ships as:
- Prebuilt bundles in dist/ (ESM, IIFE, node variants)
- Model definitions and binaries in models/
- Demos for browser & NodeJS in demo/
- Typedoc docs in typedoc/
- Tests in test/

##### High-Level Architecture Components
a. Core Library (src/)
- Human.ts – Main class, orchestrates pipeline, config, eventing.
- Config & Models (src/config.ts, src/models.ts) – Defines which models to load, their URLs/files.
- Pipeline modules – Each domain has its own folder (face/, body/, hand/, gesture/, segmentation/, object/, etc.) with loaders, detectors, post-processors.
- Backend Adapters (src/tfjs/) – Abstraction over tfjs backends (webgl, wasm, cpu, node-gpu).
- Utility modules (src/util/, src/image/, src/sample.ts, src/warmup.ts)

b. Model Store (models/)
- JSON metadata and .bin files for each ML model.
- models/models.json – master index used by loader.

c. Bundles (dist/)
- Multiple build targets: human.esm.js, human.js (IIFE), human.node.js, variants with WASM or GPU.
- tfjs.esm.js – extracted tfjs-core for advanced users.

d. Demos (demo/)
- Browser demos: facedetect, facematch, faceid, multithread, segmentation, tracker, typescript, video, plus helpers (webrtc, jsonview).
- Node demos: nodejs folder with scripts showing file‐based, video, webcam, events, matching, multi‐process usage.

e. Tests (test/)
- Automated browser and NodeJS tests covering backends and domains.

f. Documentation (typedoc/, README.md, wiki/)
- Full API reference generated by TypeDoc.

##### Relationships & Data Flow
- Input Layer: HTMLMediaElement / Canvas / ImageBitmap / Buffer (Node)
↓
↓
- Pipeline Dispatcher (Human.ts):
- Based on config, schedule modules in attention pipeline (e.g., run face detector → face mesh only if face found).
- Might run modules in parallel or multithread (WebWorkers / child processes).
↓
- Inference:
- Each module loads its own tfjs GraphModel via loader (src/tfjs/load.ts) using the appropriate backend.
- Raw tensors processed by module-specific logic (src/face/*.ts, src/body/*.ts, etc.).
↓
- Postprocessing:
- Convert outputs into structured results (e.g., FaceResult, BodyResult, HandResult).
- Apply smoothing/interpolation (temporal util).
↓
↓
↓
- Events: Human.events emits stages (“warmup”, “detect”, etc.) for client code to hook into.

##### Architectural Patterns & Design Principles
- Modular pipeline: each domain (face, body, hand) encapsulated, configurable ON/OFF.
- Attention pipeline: conditional execution to optimize performance.
- Backend abstraction: single API over multiple compute backends (WebGL, WASM, CPU, Node GPUs).
- Extensibility: custom modules can be plugged in via wiki instructions.
- Separation of concerns: input handling, inference, postprocessing, drawing, and I/O are distinct layers.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Input Layer
    InputBrowser["Browser Input<br/>MediaElement/WebCam/File Drop"]:::io
    InputNode["Node Input<br/>File System/ffmpeg/fswebcam"]:::io

    %% Preprocessing
    Preprocessor["Preprocessor<br/>src/image/image.ts"]:::core

    %% Core Pipeline
    CoreHuman["Human Core<br/>src/human.ts"]:::core
    Config["Config & Models<br/>src/config.ts, src/models.ts"]:::core

    %% Model Store
    subgraph "Model Store"
      ModelStoreNode["Master Index & Binaries<br/>models/models.json + .bin/.json"]:::store
    end

    %% Backend Adapters
    BackendAdapters["Backend Adapters<br/>src/tfjs/"]:::backend

    %% Pipeline Modules (Inference)
    subgraph "Pipeline Modules"
      Face["Face Module<br/>src/face/"]:::ml
      Body["Body Module<br/>src/body/"]:::ml
      Hand["Hand Module<br/>src/hand/"]:::ml
      Gesture["Gesture Module<br/>src/gesture/gesture.ts"]:::ml
      Segmentation["Segmentation Module<br/>src/segmentation/"]:::ml
      ObjectMod["Object Module<br/>src/object/"]:::ml
    end

    %% Postprocessing & Draw
    Postprocessor["Postprocessor & Temporal Interpolator"]:::core
    Draw["Draw Engine<br/>src/draw/"]:::core

    %% Output
    Output["Output<br/>Canvas/Images/JSON"]:::io

    %% Bundles & Demos
    subgraph "Bundles & Demos"
      Bundles["Prebuilt Bundles (dist/)"]:::io
      BrowserDemos["Browser Demos<br/>demo/"]:::io
      NodeDemos["Node Demos<br/>demo/nodejs/"]:::io
    end

    %% Data Flow
    InputBrowser --> Preprocessor
    InputNode --> Preprocessor
    Preprocessor --> CoreHuman
    Config --> CoreHuman
    ModelStoreNode --> CoreHuman
    CoreHuman --> BackendAdapters
    BackendAdapters --> Face
    BackendAdapters --> Body
    BackendAdapters --> Hand
    BackendAdapters --> Gesture
    BackendAdapters --> Segmentation
    BackendAdapters --> ObjectMod
    CoreHuman --> Face
    CoreHuman --> Body
    CoreHuman --> Hand
    CoreHuman --> Gesture
    CoreHuman --> Segmentation
    CoreHuman --> ObjectMod
    Face --> Postprocessor
    Body --> Postprocessor
    Hand --> Postprocessor
    Gesture --> Postprocessor
    Segmentation --> Postprocessor
    ObjectMod --> Postprocessor
    Postprocessor --> Draw
    Draw --> Output

    %% Demo Integration
    BrowserDemos --> CoreHuman
    NodeDemos --> CoreHuman

    %% Click Events
    click Preprocessor "https://github.com/vladmandic/human/blob/main/src/image/image.ts"
    click CoreHuman "https://github.com/vladmandic/human/blob/main/src/human.ts"
    click Config "https://github.com/vladmandic/human/blob/main/src/config.ts"
    click Config "https://github.com/vladmandic/human/blob/main/src/models.ts"
    click Face "https://github.com/vladmandic/human/tree/main/src/face/"
    click Body "https://github.com/vladmandic/human/tree/main/src/body/"
    click Hand "https://github.com/vladmandic/human/tree/main/src/hand/"
    click Gesture "https://github.com/vladmandic/human/blob/main/src/gesture/gesture.ts"
    click Segmentation "https://github.com/vladmandic/human/tree/main/src/segmentation/"
    click ObjectMod "https://github.com/vladmandic/human/tree/main/src/object/"
    click BackendAdapters "https://github.com/vladmandic/human/tree/main/src/tfjs/"
    click Draw "https://github.com/vladmandic/human/tree/main/src/draw/"
    click ModelStoreNode "https://github.com/vladmandic/human/blob/main/models/models.json"
    click Bundles "https://github.com/vladmandic/human/tree/main/dist/"
    click BrowserDemos "https://github.com/vladmandic/human/tree/main/demo/"
    click NodeDemos "https://github.com/vladmandic/human/tree/main/demo/nodejs/"

    %% Styles
    classDef core fill:#add8e6,stroke:#000,color:#000;
    classDef ml fill:#90ee90,stroke:#000,color:#000;
    classDef io fill:#ffdead,stroke:#000,color:#000;
    classDef backend fill:#dda0dd,stroke:#000,color:#000;
    classDef store fill:#d3d3d3,stroke:#000,color:#000;
```
