111 lines
5.2 KiB
Markdown
111 lines
5.2 KiB
Markdown
# Native tracker (zero-Python) — architecture
|
||
|
||
Status: **working** (verified on the user's Windows box; DEA renders the skeleton
|
||
and the menu hand-cursor covers the screen). Replaces the Python/MediaPipe
|
||
tracker (`tracker/`) with a single native Windows application. Goal from the
|
||
user: pick the camera, pick the resolution, see a live preview, no Python.
|
||
|
||
## Why / scope
|
||
|
||
The Python tracker works but is a blind CLI (`--source 0 --depth-size 640x480`)
|
||
and drags a MediaPipe/OpenCV venv behind it. The new tracker is one native
|
||
`kinect-tracker.exe` that:
|
||
|
||
- enumerates webcams and their native resolutions, lets you pick both in a GUI,
|
||
- shows a live preview with a mask/skeleton overlay so you can frame yourself,
|
||
- runs pose + person segmentation natively (ONNX BlazePose; inference is
|
||
multi-threaded across ~half the cores for lower latency),
|
||
- **remembers the last camera + resolution** (`kinect_tracker_last.txt` next to
|
||
the exe) and defaults the dropdowns to it on the next launch,
|
||
- mirrors the camera at the source (selfie view, matching a real Kinect's
|
||
mirrored depth image),
|
||
- publishes the frozen IPC contract into the `kinect_virtual_frames` shared
|
||
memory the `Kinect10.dll` shim already reads.
|
||
|
||
The skeleton-selection/validity gate and the menu hand-cursor mapping (L/R swap +
|
||
reach amplification, applied shim-side) are documented in
|
||
[game-internals.md](game-internals.md).
|
||
|
||
## What is reused (already C++)
|
||
|
||
`core/` is the whole non-ML spine and already builds in the shim:
|
||
|
||
- `core/contract` — the exact shared-memory layout (seqlock triple buffer,
|
||
`#pragma pack(1)`, golden offsets). The native writer mirrors the Python
|
||
`FrameContractPublisher` using these structs directly.
|
||
- `core/mapping` — MediaPipe-33 landmarks → Kinect-20 joints.
|
||
- `core/synth` — synthetic depth from the segmentation mask + placed joints,
|
||
plus the mask stabilizer.
|
||
|
||
So the native tracker only adds: camera capture, inference, GUI, and the shm
|
||
*writer* (the shim already has the *reader*).
|
||
|
||
## Inference: faithful MediaPipe weights via ONNX Runtime
|
||
|
||
The `pose_landmarker_full.task` bundle is a zip of the real BlazePose GHUM
|
||
TFLite models:
|
||
|
||
- `pose_detector.tflite` — SSD-style person/pose detector (→ ROI).
|
||
- `pose_landmarks_detector.tflite` — 256×256 ROI → 33 landmarks (image + world)
|
||
+ visibility/presence + a 256×256 segmentation mask.
|
||
|
||
We convert both TFLite models to ONNX (tf2onnx) and run them with ONNX Runtime
|
||
(prebuilt Windows DLL, C++ API). This keeps the *exact* MediaPipe weights —
|
||
maximal fidelity — and only the pre/post-processing is reimplemented in C++:
|
||
|
||
1. letterbox the frame to the detector input, decode SSD anchors + NMS → pose ROI,
|
||
2. rotate/crop the ROI to 256×256 for the landmark model,
|
||
3. decode 33 landmarks back to image space; keep world landmarks + visibility,
|
||
4. take the segmentation mask head, un-rotate/un-crop back to frame space.
|
||
|
||
Each stage is validated headless on Linux against the Python MediaPipe output on
|
||
the same image (numpy A/B) before and after the C++ port, so correctness is
|
||
proven without the Windows GUI in the loop. The landmark output feeds
|
||
`core/mapping`; the mask feeds `core/synth` — unchanged.
|
||
|
||
## Capture: Media Foundation
|
||
|
||
Windows Media Foundation (MinGW-w64 headers available):
|
||
|
||
- `MFEnumDeviceSources(VIDCAP)` → device friendly names for the picker,
|
||
- `IMFSourceReader` → capture; `GetNativeMediaType` enumerates native
|
||
resolutions for the resolution picker,
|
||
- convert NV12/YUY2/RGB32 samples to BGR for inference + preview.
|
||
|
||
## GUI: plain Win32 + GDI
|
||
|
||
A plain Win32 window (user32/gdi32 only — no ImGui, no Direct3D, no
|
||
`d3dcompiler` dependency, so it cross-compiles with MinGW first try): two
|
||
drop-downs (camera device, native resolution), a Start/Stop button, a status
|
||
line, and a live preview drawn with `StretchDIBits`. The preview shows the
|
||
captured frame with the segmentation mask tinted and the pose landmarks
|
||
overlaid. A worker thread runs capture → pose → publish; the UI thread repaints
|
||
the latest frame on a 33 ms timer.
|
||
|
||
## Process / threading
|
||
|
||
Mirrors the Python design that already paces well:
|
||
|
||
- capture + inference on a worker thread caching the latest `(frame, pose, mask)`,
|
||
- a publish loop paced to a 30 fps monotonic deadline writing the shm contract,
|
||
- the GUI thread renders the preview from the latest cached frame.
|
||
|
||
## Build / target
|
||
|
||
- C++17, **64-bit** (`x86_64`). The shm layout is `pack(1)` fixed-offset, so a
|
||
64-bit writer and the 32-bit shim reader interoperate cleanly; 64-bit eases
|
||
ONNX Runtime + Media Foundation.
|
||
- CMake. Two toolchains: a Linux build of the inference core for headless
|
||
validation, and MinGW-w64 (or MSVC on the user's machine — Visual Studio is
|
||
installed) for the shipped `kinect-tracker.exe`.
|
||
- No committed binaries (standing agreement): models and the ONNX Runtime DLL
|
||
are fetched/generated by a setup step, not checked in.
|
||
|
||
## Shared-memory contract (must match the shim)
|
||
|
||
- Mapping name: `kinect_virtual_frames` (shim default; `KINECT_SHIM_SHM`
|
||
overrides on the shim side, `CreateFileMappingW` on ours).
|
||
- Layout: `nui::contract::FrameContract` + 3 `FrameSlot`s, then per-slot
|
||
depth (16-bit Kinect packed word) + color (BGRA), published via the seqlock in
|
||
`core/contract/contract.h`.
|