Files
kinect/docs/native-tracker.md
T
kinect-bot 31178f1675
ci / Linux C++ core (cmake + ctest) (push) Canceled after 0s
ci / Python root tests (pytest, no tracker deps) (push) Canceled after 0s
ci / Python tracker (pytest, tracker/.venv) (push) Canceled after 0s
feat(tracker-native): native Windows app - MF camera + Win32 GUI + shm publish
win_camera: Media Foundation device + native-resolution enumeration and RGB32
capture. win_main: plain Win32 GUI (device/resolution dropdowns, Start/Stop,
StretchDIBits live preview with mask+landmark overlay) + CreateFileMappingW on
kinect_virtual_frames + a worker thread wiring capture -> PoseEstimator
(bootstrap+track) -> FramePipeline -> publish. build_win.sh cross-compiles a
static kinect-tracker.exe with MinGW-w64 x64 (ORT header needs
-D_stdcall=__stdcall; x64 ignores the calling convention so the win ORT DLL ABI
matches). PoseEstimator path made ORTCHAR_T/wchar-portable; M_PI -> local const.
Builds clean; exe is PE32+ GUI x86-64.
2026-11-25 19:00:38 -05:00

4.6 KiB
Raw Blame History

Native tracker (zero-Python) — architecture

Status: in progress. 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, and no Python anywhere.

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,
  • publishes the frozen IPC contract into the kinect_virtual_frames shared memory the Kinect10.dll shim already reads.

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 FrameSlots, then per-slot depth (16-bit Kinect packed word) + color (BGRA), published via the seqlock in core/contract/contract.h.