Free Motion Capture for Everyone πβ¨
TypeScript
---
sidebar_position: 8
title: Pipeline Architecture
---
import { AiGeneratedBanner, Tip } from '@freemocap/skellydocs';
<AiGeneratedBanner />
# Pipeline Architecture
Pipelines are the central abstraction of the FreeMoCap backend. A pipeline takes data from sources (cameras or video files), processes it through a chain of nodes, and produces output (3D keypoints, calibration parameters, Blender scenes).
## Two Pipeline Types
| | Realtime | Posthoc |
|---|---|---|
| **Source** | Camera shared memory ring buffers | Video files (OpenCV `VideoCapture`) |
| **Cadence** | Latest frame, drops stale frames | Sequential, processes every frame |
| **Accuracy** | Best-effort (frame dropping is acceptable) | Highest possible (no drops, full dataset) |
| **Lifetime** | Long-lived, bound to camera group | Fire-and-forget, ends when video ends |
| **Dataset access** | Current frame only | Full video (forward/backward, global context) |
| **GPU inference** | Batched centralized node (default) | Inline per video node |
| **Output** | WebSocket β frontend (streaming) | Files on disk β playback |
```mermaid
graph LR
subgraph Realtime["Realtime Pipeline"]
CG["CameraGroup<br/>(shared memory)"] --> CN1["CameraNode 1"]
CG --> CN2["CameraNode 2"]
CG --> CN3["CameraNode N"]
CN1 -->|"observations"| AGG["AggregatorNode<br/>(triangulation + filtering)"]
CN2 -->|"observations"| AGG
CN3 -->|"observations"| AGG
AGG -->|"3D keypoints"| WS["WebSocket β Frontend"]
end
subgraph Posthoc["Posthoc Pipeline"]
VG["VideoGroup<br/>(video files)"] --> VN1["VideoNode 1"]
VG --> VN2["VideoNode 2"]
VG --> VN3["VideoNode N"]
VN1 -->|"observations"| PAGG["AggregatorNode<br/>(task_fn)"]
VN2 -->|"observations"| PAGG
VN3 -->|"observations"| PAGG
PAGG -->|"processed data"| DISK["Files on Disk"]
end
```
## Abstract Base Classes
All pipeline components inherit from a shared set of ABCs in `core/pipeline/abcs/`.
### BaseNode
Every node in a pipeline (source or aggregator) extends `BaseNode`. It provides:
- **Lifecycle**: `start()` spawns a child process via `ManagedWorker`; `shutdown()` signals it to stop; `is_alive` checks if the process is still running
- **Shutdown signal**: Each node holds a `shutdown_self_flag` (`multiprocessing.Value('b', False)`, a synchronized bool) checked in its main loop
- **Global kill**: Nodes also check `global_kill_flag` β the nuclear option that stops everything
- **Windows spawn staggering**: 0.25s delay between child process starts to avoid `spawn` mode race conditions
### SourceNode and AggregatorNode
Marker ABCs that distinguish the two node roles. `SourceNode` produces observations (camera frames, video frames). `AggregatorNode` consumes them and produces final output.
### PipelineABC and PipelineManagerABC
Marker ABCs for pipelines and their managers. These exist for type clarity rather than shared behavior β the realtime and posthoc implementations diverge significantly.
### PipelineIPC
Shared IPC primitives owned by each pipeline:
| Field | Purpose |
|---|---|
| `pipeline_id` | Unique identifier |
| `pipeline_shutdown_flag` | Stops this pipeline only |
| `global_kill_flag` | Shared reference to the global kill switch |
| `heartbeat_timestamp` | Last heartbeat from each child node |
| `ws_queue` | Queue for messages destined for the WebSocket |
The `should_continue` property checks all three stop conditions: `not global_kill_flag.value and not pipeline_shutdown_flag.value and check_main_process_heartbeat(...)`. This ensures nodes stop if either the global kill switch is triggered, their pipeline is shut down, or the main process has died.
Two shutdown authorities:
- `shutdown_pipeline()` β stops this pipeline, leaves everything else running
- `kill_everything()` β sets both pipeline shutdown AND global kill flags (nuclear option, used when a CameraNode crashes)
## Realtime Pipeline
The realtime pipeline streams live camera data, processes it frame-by-frame, and sends results to the frontend over WebSocket. It's designed for **low latency** β the latest frame is always processed, and stale frames are dropped.
### Topology
```
CameraGroup (shared memory ring buffers, one per camera)
β
βΌ
CameraNodes (one child process per camera)
β Reads from ring buffer, blocks on ProcessFrameNumberTopic
β Runs ChArUco detection inline (CPU); skeleton only in per-camera mode
β Publishes CameraNodeOutputMessage per frame
β
βΌ (default: use_centralized_inference=True)
RealtimeSkeletonInferenceNode (centralized GPU β the default skeleton path)
β One OnnxSession; tracker.process_batch() batches all cameras in one call
β Drains to latest frame (drops stale frames)
β Publishes SkeletonInferenceResultMessage
β
βΌ
RealtimeAggregatorNode (child process)
β Collects per-camera observations for current frame
β If calibration valid: triangulates (DLT) β filters β outputs
β Publishes AggregationNodeOutputMessage
β
βΌ
RealtimePipeline.get_latest_frontend_payload() (main process)
β Called by WebSocket relay
β
βΌ
WebSocket β Frontend
```
### CameraNode (`camera_node.py`, ~258 lines)
Each camera gets its own child process running a `CameraNode`:
1. **Reads frames** from the camera's `CameraSharedMemoryRingBuffer` (shared memory, no copy)
2. **Blocks on `ProcessFrameNumberTopic`** β the aggregator tells it which frame to process (up to 5ms timeout)
3. **Runs trackers inline** via `tracker_factory` (`core/tracking/`): a ChArUco `Tracker` (CPU) for calibration markers, and β only in per-camera mode β a skeleton `Tracker` (RTMPose or MediaPipe). Each is a SkellyTracker `Tracker`; the node calls `tracker.process_image(image, frame_number, state)`.
4. **Applies image rotation** from camera metadata (if the camera is physically rotated)
5. **Verifies frame_number** matches the requested frame β detects ring buffer overwrites (camera writing faster than pipeline reads)
6. **Publishes `CameraNodeOutputMessage`** per frame (observations + metadata)
7. **On exception**: calls `ipc.kill_everything()` β the nuclear option. A camera node crash means the pipeline can't produce valid output, so everything stops.
**By default** (`use_centralized_inference=True`) CameraNodes run **ChArUco only** and skip skeleton detection β the centralized `RealtimeSkeletonInferenceNode` does batched skeleton inference for all cameras. Inline per-camera skeleton detection is the `use_centralized_inference=False` fallback (more GPU memory, no batching; useful for CPU-bound setups). See [Tracking Integration](/docs/architecture/tracking-integration).
### RealtimeSkeletonInferenceNode (`realtime_skeleton_inference_node.py`, ~435 lines)
The **default** skeleton path (`use_centralized_inference=True`): a dedicated GPU worker doing batched inference for all cameras. See [Tracking Integration](/docs/architecture/tracking-integration) for how the tracker is built.
1. **One CUDA context, one `OnnxSession` + `Tracker`** (built via `tracker_factory`) β avoids per-node GPU context switching overhead
2. **Subscribes to `ProcessFrameNumberTopic`**, drains to the latest frame (drops stale frames when GPU falls behind)
3. **Reads images directly** from per-camera ring buffers (read-only, no copy)
4. **Calls `tracker.process_batch(images_dict)`** β one batched SkellyTracker call (one ONNX `session.run` per model) for all cameras at once
5. **Publishes `SkeletonInferenceResultMessage`** β per-camera skeleton observations keyed by frame_number
6. **GPU OOM recovery**: catches `MemoryError`, rebuilds session (up to 3 retries), skips the affected frame
7. **TensorRT engine compilation** available when configured with execution provider `"trt"` (1β3 minutes on first run, cached on subsequent runs). The default execution provider is `"cuda"`.
### RealtimeAggregatorNode (`realtime_aggregator_node.py`, ~621 lines)
The aggregator is where the real work happens. It runs as a child process:
1. **Owns a `CalibrationStateTracker`** for hot-reloadable calibration (loads on creation, polls for changes when called)
2. **Collects `CameraNodeOutputMessage`** from all cameras (blocks up to 5ms per camera)
3. **In GPU mode**: waits for `SkeletonInferenceResultMessage`, splices per-camera skeletons into camera outputs
4. **Optimistically requests next frame** before processing the current one (parallelism: cameras work on frame N+1 while aggregator processes frame N)
5. **Triangulation**: DLT via `calibration.try_angulate()` (which uses the `Triangulator` class internally), reprojection error gating
6. **Filtering pipeline**: `RealtimeKeypointFilter` (OneEuro on raw) β `RealtimePointGate` (velocity check) β `RealtimeSkeletonFilter` (OneEuro + FABRIK bone constraint)
7. **All processing in `dict[str, ndarray]`** until final `Point3d` conversion (avoids per-point object overhead)
8. **Backpressure**: `result_ready_event` / `result_consumed_event` pair β the aggregator waits for the main process to consume the previous result before producing the next one
### RealtimePipelineManager (`realtime_pipeline_manager.py`, ~156 lines)
- Maintains `pipelines: dict[PipelineIdString, RealtimePipeline]`
- **One pipeline per camera group** (singleton by camera ID set)
- `wait_for_any_result_ready()`: concurrent `asyncio.to_thread(event.wait)` across all alive pipelines
- `pause_unpause_all()`: toggles all camera groups
- Thread-safe via `multiprocessing.Lock`
## Posthoc Pipeline
The posthoc pipeline processes recorded videos offline. It's designed for **accuracy** β every frame is processed sequentially, the full dataset is available, and the output is written to disk.
### Topology
```
VideoGroup (set of synchronized video files on disk)
β
βΌ
VideoNodes (one child process per video)
β Reads frames sequentially via OpenCV VideoCapture
β Runs detector from config (CharUco, MediaPipe, RTMPose)
β Optionally produces annotated video output
β Reports progress per frame
β Publishes VideoNodeOutputMessage per frame
β
βΌ
PosthocAggregatorNode (child process)
β Collects observations from all video nodes
β Runs task_fn (calibration or mocap logic)
β Writes output artifacts to disk
β Reports progress
β
βΌ
Output files on disk β Playback via REST API
```
### VideoNode (`video_node.py`, ~340 lines)
Each video gets a child process:
1. **Reads frames** via OpenCV `cv2.VideoCapture` (sequential, no frame dropping)
2. **Builds a SkellyTracker `Tracker`** from its `TrackerConfig` / `DetectionStageConfig` (ChArUco, MediaPipe, or RTMPose) and calls `tracker.process_image()` per frame
3. **Publishes `VideoNodeOutputMessage`** per frame (detection observations)
4. **Annotated video output**: layers detection annotations on frames
- Tries H.264 ("avc1") encoder first, falls back to mp4v
- If existing annotated video exists, layers on top of it (preserves previous annotations)
5. **Progress per frame**: `VideoNodeProgressMessage` at each phase transition
6. **On error**: logs exception, sends FAILED progress, calls `ipc.shutdown_pipeline()` (pipeline-local β does NOT trigger global kill)
### VideoGroupHelper (`video_group_helper.py`, ~581 lines)
Manages the set of video files for a posthoc pipeline:
- **`VideoHelper`**: LRU-cached video reader with frame cache (configurable MB limit), sequential read optimization
- **`VideoMetadata`**: Pydantic model (width, height, fps, frame_count, fourcc, duration)
- **Source resolution**: prefers manifest's `videos` map (authoritative camera_id β filename); falls back to filename parsing with collision detection
- **Validates** all videos have the same frame count
### PosthocPipelineManager (`posthoc_pipeline_manager.py`, ~216 lines)
- `pipelines: dict[PipelineIdString, PosthocPipeline]`
- **Fire-and-forget**: creates pipelines that run to completion independently
- **Lazy dead-pipeline eviction**: drains final progress messages before removing completed/failed pipelines
- `create_calibration_pipeline()`: binds `run_posthoc_calibration_task` via `functools.partial`
- `create_mocap_pipeline()`: binds `run_posthoc_mocap_aggregator_task`
- Each pipeline emits a "queued" progress message before starting workers
## Pipeline Phases & Progress Reporting
Pipeline progress flows from backend to frontend through WebSocket β rAF loop β Redux dispatch. The phase enums must stay in sync with `ServerContextProvider.tsx`.
### VideoNode Phase
```
SETTING_UP β PROCESSING_IMAGES β COMPLETE
β FAILED
```
### Aggregator Phase
```
COLLECTING_CAMERA_OUTPUT β COMPLETE
β FAILED
```
### Calibration Stage
```
VALIDATING_OBSERVATIONS β RUNNING_SOLVER β SAVING_CALIBRATION
```
### Mocap Stage
```
BUILDING_RECORDERS β TRIANGULATING β EXPORTING_BLENDER
```
### Progress Message Structure
```python
@dataclass
class PipelineProgressMessage:
message_type: str = "posthoc_progress"
pipeline_id: str = ""
pipeline_type: str = "" # "calibration" or "mocap"
phase: str = "" # Current phase enum value
progress_fraction: float = 0.0 # 0.0 to 1.0
detail: str = "" # Human-readable status
recording_name: str = ""
recording_path: str = ""
```
Video node progress adds `camera_id`. Aggregator progress uses the base message. The frontend groups progress by `pipeline_id` and displays per-stage completion in the pipeline progress panel.
<Tip shortInfo="Progress messages are JSON over WebSocket, not REST. This means the frontend gets live progress without polling. The frontend's rAF loop dispatches them to Redux, which triggers UI updates in the PipelineProgressPanel and PipelineProgressSnackbar components." />
## Additional Pipeline Components
Several pipeline components exist in the codebase without dedicated documentation sections yet:
| Component | File | Lines | Purpose |
|---|---|---|---|
| `RealtimePipeline` | `realtime_pipeline.py` | ~371 | Pipeline dataclass: create, start, shutdown, config update for realtime pipelines |
| `PosthocPipeline` | `posthoc_pipeline.py` | ~221 | Pipeline dataclass: create, start, shutdown for posthoc pipelines |
| `PosthocAggregationNode` | `posthoc_aggregation_node.py` | ~286 | Collects all VideoNode outputs by frame, delegates to task function |
| `CameraNodeConfig` | `camera_node_config.py` | ~25 | Pydantic model for CameraNode configuration |
| `RealtimeAggregatorNodeConfig` | `realtime_aggregator_node_config.py` | ~24 | Pydantic model for AggregatorNode configuration |
| `RealtimePipelineConfig` | `realtime_pipeline_config.py` | ~28 | Pydantic model for RealtimePipeline configuration |
| `RealtimeSkeletonInferenceNodeConfig` | `realtime_skeleton_inference_node_config.py` | ~36 | Pydantic model for SkeletonInferenceNode configuration |
| `PipelineStageTimer` | `pipeline_stage_timer.py` | ~69 | Per-node accumulator for stage timing measurements |
| `PipelineTimingReporter` | `pipeline_timing_reporter.py` | ~291 | Subscribes to timing data, prints consolidated report tables |
| `TaskProgressReporter` | `task_progress_reporter.py` | ~50 | Typed progress-reporting interface for task functions |
| `SimpleRealtimeKeypointFilter` | `realtime_keypoint_filter.py` | ~123 | One Euro filter with gap-filling for realtime keypoints |