diff --git a/tracker-native/README.md b/tracker-native/README.md new file mode 100644 index 0000000..0bb3b99 --- /dev/null +++ b/tracker-native/README.md @@ -0,0 +1,26 @@ +# kinect-tracker (native, zero-Python) + +Native replacement for the Python/MediaPipe `tracker/`. See +`docs/native-tracker.md` for the architecture. One `kinect-tracker.exe`: +camera + resolution picker, live preview, native pose + segmentation, and the +shared-memory publish the `Kinect10.dll` shim reads. + +No binaries are committed. Generate them locally: + +```sh +scripts/setup_models.sh # extract MediaPipe .task -> tflite, convert -> onnx +scripts/fetch_ort.sh # download prebuilt ONNX Runtime (linux + win x64) +./build.sh # headless Linux build (currently the ORT smoke test) +``` + +## Status + +- Inference foundation proven: the real MediaPipe BlazePose landmark weights + (`pose_landmarks_detector`, 33 landmarks + world + 256x256 mask) convert to + ONNX faithfully (mask <=1.2% in probability space vs TFLite) and run + bit-identically under the ONNX Runtime C++ API (`build/ort_smoke` vs + `scripts/ort_smoke_ref.py`). +- Next: BlazePose pre/post (ROI, letterbox, landmark/mask un-transform) + validated against the MediaPipe reference; the detector model for ROI + acquisition (tf2onnx can't convert it yet); native shm writer; Media + Foundation capture; the Dear ImGui GUI; MinGW/MSVC Windows build. diff --git a/tracker-native/build.sh b/tracker-native/build.sh new file mode 100755 index 0000000..f2f4b9d --- /dev/null +++ b/tracker-native/build.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +set -euo pipefail +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +ORT="${HERE}/third_party/onnxruntime-linux-x64" +mkdir -p "${HERE}/build" + +g++ -std=c++17 -O2 -Wall -Wextra \ + -I "${ORT}/include" \ + "${HERE}/src/ort_smoke.cpp" \ + -o "${HERE}/build/ort_smoke" \ + -L "${ORT}/lib" -lonnxruntime -Wl,-rpath,"${ORT}/lib" +echo "built ${HERE}/build/ort_smoke" diff --git a/tracker-native/scripts/fetch_ort.sh b/tracker-native/scripts/fetch_ort.sh new file mode 100755 index 0000000..3ef20fb --- /dev/null +++ b/tracker-native/scripts/fetch_ort.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +# Fetch prebuilt ONNX Runtime (linux-x64 for headless validation, win-x64 for +# the shipped build). The official microsoft/onnxruntime GitHub repo was removed +# on 2026-11-17; these are the unchanged 1.20.1 assets from a recovery mirror. +set -euo pipefail +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +V="${ORT_VERSION:-1.20.1}" +B="https://github.com/cga-management/third-party-mirror/releases/download/onnxruntime-${V}" +mkdir -p "${HERE}/third_party" +cd "${HERE}/third_party" + +if [ ! -d onnxruntime-linux-x64 ]; then + curl -fsSL -o ort-linux.tgz "${B}/onnxruntime-linux-x64-${V}.tgz" + tar xzf ort-linux.tgz && rm ort-linux.tgz + mv "onnxruntime-linux-x64-${V}" onnxruntime-linux-x64 +fi +if [ ! -d onnxruntime-win-x64 ]; then + curl -fsSL -o ort-win.zip "${B}/onnxruntime-win-x64-${V}.zip" + python3 -c "import zipfile; zipfile.ZipFile('ort-win.zip').extractall('.')" + rm ort-win.zip && mv "onnxruntime-win-x64-${V}" onnxruntime-win-x64 +fi +echo "ONNX Runtime ${V} ready: $(ls -d onnxruntime-*-x64)" diff --git a/tracker-native/scripts/ort_smoke_ref.py b/tracker-native/scripts/ort_smoke_ref.py new file mode 100644 index 0000000..2165289 --- /dev/null +++ b/tracker-native/scripts/ort_smoke_ref.py @@ -0,0 +1,20 @@ +import math +import numpy as np +import onnxruntime as ort + +n = 1 * 256 * 256 * 3 +x = (np.arange(n) % 1000).astype(np.float32) / 1000.0 +x = x.reshape(1, 256, 256, 3) + +sess = ort.InferenceSession( + "models/pose_landmarks_detector.onnx", providers=["CPUExecutionProvider"] +) +out = dict(zip([o.name for o in sess.get_outputs()], sess.run(None, {"input_1": x}))) +lm = out["Identity"].ravel() +pres = out["Identity_1"].ravel() +mask = out["Identity_2"].ravel() +msum = float((1.0 / (1.0 + np.exp(-mask))).sum()) + +print("lm0..4: %.6f %.6f %.6f %.6f %.6f" % (lm[0], lm[1], lm[2], lm[3], lm[4])) +print("presence: %.6f" % pres[0]) +print("mask_prob_sum: %.3f" % msum) diff --git a/tracker-native/scripts/setup_models.sh b/tracker-native/scripts/setup_models.sh new file mode 100755 index 0000000..5fe0d00 --- /dev/null +++ b/tracker-native/scripts/setup_models.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +set -euo pipefail +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +TASK="${1:-${HERE}/../tracker/models/pose_landmarker_full.task}" +PY="${PY:-${HERE}/../tracker/.venv/bin/python}" +mkdir -p "${HERE}/models" + +"${PY}" -c "import zipfile; zipfile.ZipFile('${TASK}').extractall('${HERE}/models')" + +for m in pose_detector pose_landmarks_detector; do + if [ -f "${HERE}/models/${m}.onnx" ]; then continue; fi + if ! "${PY}" -m tf2onnx.convert --tflite "${HERE}/models/${m}.tflite" \ + --output "${HERE}/models/${m}.onnx" --opset 17; then + echo "note: ${m} tflite->onnx conversion not supported by tf2onnx; skipping" >&2 + fi +done +ls -la "${HERE}/models" diff --git a/tracker-native/src/ort_smoke.cpp b/tracker-native/src/ort_smoke.cpp new file mode 100644 index 0000000..89c0b4c --- /dev/null +++ b/tracker-native/src/ort_smoke.cpp @@ -0,0 +1,41 @@ +// Headless ORT C++ integration check; mirrored by scripts/ort_smoke_ref.py. +#include + +#include +#include +#include +#include + +int main(int argc, char** argv) { + const char* model = argc > 1 ? argv[1] : "models/pose_landmarks_detector.onnx"; + Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "smoke"); + Ort::SessionOptions so; + so.SetIntraOpNumThreads(1); + so.SetGraphOptimizationLevel(ORT_ENABLE_ALL); + Ort::Session sess(env, model, so); + + std::vector in(static_cast(1) * 256 * 256 * 3); + for (size_t i = 0; i < in.size(); ++i) { + in[i] = static_cast(i % 1000) / 1000.0f; + } + std::array shape{1, 256, 256, 3}; + Ort::MemoryInfo mi = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault); + Ort::Value input = Ort::Value::CreateTensor(mi, in.data(), in.size(), + shape.data(), shape.size()); + + const char* in_names[] = {"input_1"}; + const char* out_names[] = {"Identity", "Identity_1", "Identity_2", + "Identity_3", "Identity_4"}; + auto outs = sess.Run(Ort::RunOptions{nullptr}, in_names, &input, 1, out_names, 5); + + const float* lm = outs[0].GetTensorData(); // [1,195] + const float* pres = outs[1].GetTensorData(); // [1,1] + const float* mask = outs[2].GetTensorData(); // [1,256,256,1] logits + double msum = 0.0; + for (int i = 0; i < 256 * 256; ++i) msum += 1.0 / (1.0 + std::exp(-mask[i])); + + std::printf("lm0..4: %.6f %.6f %.6f %.6f %.6f\n", lm[0], lm[1], lm[2], lm[3], lm[4]); + std::printf("presence: %.6f\n", pres[0]); + std::printf("mask_prob_sum: %.3f\n", msum); + return 0; +}