# tryolabs/norfair architecture

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

- Interactive diagram: https://gitdiagram.com/tryolabs/norfair
- Repository: https://github.com/tryolabs/norfair

## Overview

1. **What this repository is**
   - Norfair is a **Python library** for real-time multi-object tracking, not a standalone application.
   - The architecture centers on a small tracking core that can be embedded into many different computer-vision pipelines.

2. **Core package boundary**
   - The main runtime code lives under `norfair/`.
   - This package is the product boundary: external code provides detections and optional video frames, and Norfair returns tracked objects with stable IDs over time.
   - The public API is exposed from `norfair/__init__.py`.

3. **Tracking pipeline**
   - The primary flow is: **detector output → Detection objects → Tracker update → tracked objects**.
   - Detections are not limited to boxes; they are represented as one or more points, which is the key abstraction that makes Norfair detector-agnostic.
   - The tracker performs frame-to-frame association and state prediction, then assigns or preserves track identities.

4. **Association and motion model**
   - `tracker.py`, `distances.py`, and `filter.py` are the core tracking subsystems.
   - `distances.py` defines how detections and tracked objects are compared; this is intentionally pluggable and is a major architectural seam.
   - `filter.py` provides the prediction/smoothing layer used by the tracker, so association is based on estimated positions rather than raw last-frame positions.

5. **Advanced tracking features**
   - `camera_motion.py` handles compensation for moving cameras, which changes the coordinate frame before matching.
   - The library also supports higher-dimensional tracking and appearance-based re-identification, which are showcased in demos rather than being a separate top-level service.
   - This means the tracker can operate in multiple modes depending on whether the input scene is static, camera-stabilized, or requires ReID.

6. **Video and frame I/O utilities**
   - `video.py` contains helpers for reading and writing frame streams, likely built around OpenCV.
   - This is a utility boundary: it helps create end-to-end inference loops, but it is not required for using the tracker itself.
   - In architecture diagrams, this sits alongside the core tracker as an optional adapter to video sources and sinks.

7. **Visualization layer**
   - `norfair/drawing/` contains rendering utilities for tracked objects, boxes, points, paths, and fixed-camera / grid overlays.
   - Drawing is separate from tracking logic, which keeps the tracker headless and reusable in non-visual pipelines.
   - The demos use these helpers heavily to visualize track IDs and trajectories on video frames.

8. **Metrics and evaluation**
   - `metrics.py` is a separate subsystem for evaluation, used for MOT-style benchmarking.
   - The repo includes a `demos/motmetrics4norfair` area and tests around MOT outputs, indicating that evaluation is treated as an auxiliary workflow rather than core runtime logic.
   - This supports architecture diagrams that distinguish production tracking from offline assessment.

9. **Demos as integration examples**
   - The large `demos/` tree is a collection of **integration adapters** for different detectors and scenarios: YOLO variants, Detectron2, AlphaPose, OpenPose, SAHI, YOLO-NAS, camera motion, 3D tracking, profiling, and ReID.
   - Each demo is mostly a thin pipeline glue layer: detector-specific preprocessing/inference on one side, Norfair tracking in the middle, and optional drawing/output on the other.
   - These demos show that Norfair is designed to slot into heterogeneous ML stacks rather than impose its own detector.

10. **Documentation and developer tooling**
   - `docs/` is a MkDocs-based documentation site, with generated reference pages for each major module.
   - `tests/` covers distances, drawing, tracker behavior, and metrics-related behavior, which maps closely to the core package boundaries.
   - CI, linting, packaging, and release automation live in `.github/workflows`, `pyproject.toml`, `tox.ini`, and `.pylintrc`, indicating a conventional Python library lifecycle.

11. **External dependencies and runtime shape**
   - OpenCV is a material dependency for video handling and drawing; GPU-centric detector frameworks appear only in demos.
   - The core library itself is designed to be lightweight and detector-agnostic, so most heavy ML dependencies are intentionally pushed to demo-specific subprojects.
   - This separation is important: the library stays small, while integrations carry the cost of any specific detector or pose model.

12. **How to draw the architecture**
   - A good diagram would place **Norfair core tracking** in the center, with arrows in from “detector outputs” and “video/frame stream,” and arrows out to “tracked objects,” “visualization,” and “metrics/evaluation.”
   - Around that core, show optional modules for **camera motion**, **ReID**, **drawing**, and **video I/O**.
   - Outside the package boundary, show the many detector/demo integrations as interchangeable upstream producers of detections.

Main entry points: [`norfair/tracker.py`](https://github.com/tryolabs/norfair/blob/master/norfair/tracker.py) (Tracker), [`norfair/__init__.py`](https://github.com/tryolabs/norfair/blob/master/norfair/__init__.py) (Public API), [`norfair/camera_motion.py`](https://github.com/tryolabs/norfair/blob/master/norfair/camera_motion.py) (Camera motion), [`norfair/distances.py`](https://github.com/tryolabs/norfair/blob/master/norfair/distances.py) (Distances)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Core library"]
  node_public_api["Public API<br/>package surface<br/>[__init__.py]"]
  node_tracker["Tracker<br/>tracking core<br/>[tracker.py]"]
  node_distances["Distances<br/>matching metric<br/>[distances.py]"]
  node_filter["Filter<br/>motion model<br/>[filter.py]"]
  node_camera_motion["Camera motion<br/>stabilization<br/>[camera_motion.py]"]
  node_video["Video I/O<br/>frame stream<br/>[video.py]"]
  node_metrics["Metrics<br/>evaluation<br/>[metrics.py]"]
  node_drawing["Drawing<br/>visualization<br/>[__init__.py]"]
end

subgraph group_demos["Demo integrations"]
  node_detector_demos["Detector demos<br/>integration examples<br/>[demo.py]"]
  node_pose_demos["Pose / 3D demos<br/>integration examples<br/>[demo.py]"]
  node_reid_demo["ReID demo<br/>integration example<br/>[demo.py]"]
  node_camera_demo["Camera demo<br/>integration example<br/>[demo.py]"]
  node_motmetrics_demo["MOT metrics demo<br/>evaluation example"]
end

subgraph group_docsdev["Docs and tooling"]
  node_docs_site["Docs site<br/>documentation<br/>[index.md]"]
  node_ci_workflows["CI workflows<br/>automation<br/>[ci.yml]"]
end

node_detector_demos -->|"detections"| node_tracker
node_pose_demos -->|"points"| node_tracker
node_reid_demo -->|"tracked IDs"| node_tracker
node_camera_demo -->|"stabilize"| node_camera_motion
node_camera_motion -->|"compensated frame"| node_tracker
node_video -->|"frames"| node_tracker
node_distances -->|"match cost"| node_tracker
node_filter -->|"prediction"| node_tracker
node_tracker -->|"visualize"| node_drawing
node_tracker -->|"outputs"| node_metrics
node_motmetrics_demo -->|"benchmark"| node_metrics
node_public_api -->|"exports"| node_tracker
node_public_api -->|"exports"| node_distances
node_public_api -->|"exports"| node_filter
node_public_api -->|"exports"| node_camera_motion
node_public_api -->|"exports"| node_video
node_public_api -->|"exports"| node_drawing
node_docs_site -->|"documents"| node_public_api
node_ci_workflows -->|"builds"| node_docs_site

click node_public_api "https://github.com/tryolabs/norfair/blob/master/norfair/__init__.py"
click node_tracker "https://github.com/tryolabs/norfair/blob/master/norfair/tracker.py"
click node_distances "https://github.com/tryolabs/norfair/blob/master/norfair/distances.py"
click node_filter "https://github.com/tryolabs/norfair/blob/master/norfair/filter.py"
click node_camera_motion "https://github.com/tryolabs/norfair/blob/master/norfair/camera_motion.py"
click node_video "https://github.com/tryolabs/norfair/blob/master/norfair/video.py"
click node_metrics "https://github.com/tryolabs/norfair/blob/master/norfair/metrics.py"
click node_drawing "https://github.com/tryolabs/norfair/blob/master/norfair/drawing/__init__.py"
click node_detector_demos "https://github.com/tryolabs/norfair/blob/master/demos/yolov5/src/demo.py"
click node_pose_demos "https://github.com/tryolabs/norfair/blob/master/demos/openpose/src/demo.py"
click node_reid_demo "https://github.com/tryolabs/norfair/blob/master/demos/reid/src/demo.py"
click node_camera_demo "https://github.com/tryolabs/norfair/blob/master/demos/camera_motion/src/demo.py"
click node_motmetrics_demo "https://github.com/tryolabs/norfair/blob/master/demos/motmetrics4norfair/src/motmetrics4norfair.py"
click node_docs_site "https://github.com/tryolabs/norfair/blob/master/docs/reference/index.md"
click node_ci_workflows "https://github.com/tryolabs/norfair/blob/master/.github/workflows/ci.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_public_api,node_tracker,node_distances,node_filter,node_camera_motion,node_video,node_metrics,node_drawing toneBlue
class node_detector_demos,node_pose_demos,node_reid_demo,node_camera_demo,node_motmetrics_demo toneAmber
class node_docs_site,node_ci_workflows toneMint
```

## Components

### Core library

Detector-agnostic tracking runtime and its main subsystems.

- [Public API](https://github.com/tryolabs/norfair/blob/master/norfair/__init__.py): package surface · Exports the library entry points for embedding Norfair in external pipelines. · `norfair/__init__.py`
- [Tracker](https://github.com/tryolabs/norfair/blob/master/norfair/tracker.py): tracking core · Maintains track state, performs frame-to-frame association, and assigns stable IDs. · `norfair/tracker.py`
- [Distances](https://github.com/tryolabs/norfair/blob/master/norfair/distances.py): matching metric · Pluggable comparison logic between detections and tracked objects. · `norfair/distances.py`
- [Filter](https://github.com/tryolabs/norfair/blob/master/norfair/filter.py): motion model · Prediction and smoothing layer used before matching and identity assignment. · `norfair/filter.py`
- [Camera motion](https://github.com/tryolabs/norfair/blob/master/norfair/camera_motion.py): stabilization · Compensates for moving cameras by transforming the matching coordinate frame. · `norfair/camera_motion.py`
- [Video I/O](https://github.com/tryolabs/norfair/blob/master/norfair/video.py): frame stream · Helpers for reading and writing video frame streams around the tracker. · `norfair/video.py`
- [Metrics](https://github.com/tryolabs/norfair/blob/master/norfair/metrics.py): evaluation · MOT-style evaluation helpers for benchmarking tracker outputs. · `norfair/metrics.py`
- [Drawing](https://github.com/tryolabs/norfair/blob/master/norfair/drawing/__init__.py): visualization · Rendering utilities for boxes, points, paths, grids, and fixed-camera overlays. · `norfair/drawing/__init__.py`

### Demo integrations

Example pipelines that plug detectors, tracking, drawing, and evaluation together.

- [Detector demos](https://github.com/tryolabs/norfair/blob/master/demos/yolov5/src/demo.py): integration examples · YOLO, Detectron2, SAHI, MMDetection, YOLO-NAS, and related detector adapters. · `demos/yolov5/src/demo.py`
- [Pose / 3D demos](https://github.com/tryolabs/norfair/blob/master/demos/openpose/src/demo.py): integration examples · Keypoint, pose, and 3D tracking pipelines that feed point-based detections into Norfair. · `demos/openpose/src/demo.py`
- [ReID demo](https://github.com/tryolabs/norfair/blob/master/demos/reid/src/demo.py): integration example · Appearance-based re-identification workflow layered on top of tracking. · `demos/reid/src/demo.py`
- [Camera demo](https://github.com/tryolabs/norfair/blob/master/demos/camera_motion/src/demo.py): integration example · End-to-end example for moving-camera scenes and stabilization. · `demos/camera_motion/src/demo.py`
- [MOT metrics demo](https://github.com/tryolabs/norfair/blob/master/demos/motmetrics4norfair/src/motmetrics4norfair.py): evaluation example · Offline benchmarking and comparison workflows for tracking outputs. · `demos/motmetrics4norfair/src/motmetrics4norfair.py`

### Docs and tooling

Documentation site and repository maintenance workflow.

- [Docs site](https://github.com/tryolabs/norfair/blob/master/docs/reference/index.md): documentation · MkDocs-based project documentation and generated module references. · `docs/reference/index.md`
- [CI workflows](https://github.com/tryolabs/norfair/blob/master/.github/workflows/ci.yml): automation · GitHub Actions, packaging, linting, and release automation. · `.github/workflows/ci.yml`

## Connections

- Detector demos → Tracker: detections · Detector pipelines produce Norfair detections for association.
- Pose / 3D demos → Tracker: points · Pose and 3D examples feed point-based detections into the core tracker.
- ReID demo → Tracker: tracked IDs · ReID examples augment tracking with appearance-based identity recovery.
- Camera demo → Camera motion: stabilize · Camera-motion examples use motion compensation before matching.
- Camera motion → Tracker: compensated frame · Transforms coordinates before association runs.
- Video I/O → Tracker: frames · Optional frame streams provide image context for end-to-end loops.
- Distances → Tracker: match cost · Distance functions drive object-to-detection association.
- Filter → Tracker: prediction · State estimates are used to predict and smooth track positions.
- Tracker → Drawing: visualize · Tracked objects can be rendered as boxes, points, or paths.
- Tracker → Metrics: outputs · Tracker results are evaluated against MOT-style metrics.
- MOT metrics demo → Metrics: benchmark · Evaluation demo wraps the metrics subsystem.
- Public API → Tracker: exports · Public package surface exposes the core tracker entry points.
- Public API → Distances: exports · Distance abstractions are part of the public API.
- Public API → Filter: exports · Filter abstractions are available to consumers.
- Public API → Camera motion: exports · Camera motion helpers are exposed for advanced use cases.
- Public API → Video I/O: exports · Video helpers are exposed as optional utilities.
- Public API → Drawing: exports · Drawing helpers are available for visualization pipelines.
- Docs site → Public API: documents · Reference docs cover the main library modules and API.
- CI workflows → Docs site: builds · Automation validates, builds, and publishes documentation and releases.
