feat(tracker-native): ONNX Runtime C++ inference foundation + setup
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

Scaffold the zero-Python native tracker. Convert the real MediaPipe BlazePose
landmark weights (from pose_landmarker_full.task) to ONNX and prove the ONNX
Runtime C++ API runs them bit-identically to Python (ort_smoke vs
ort_smoke_ref.py; landmarks match to 6dp, mask <=1.2% prob vs TFLite).
Reproducible setup scripts (setup_models.sh, fetch_ort.sh) and a Linux build.
No binaries committed (ORT + onnx/tflite are fetched/generated).
This commit is contained in:
kinect-bot committed 2026-11-25 18:29:48 -05:00
1 parent 290283d8c0
commit 5879422ab9
6 files changed
+138

No files matched your search

+26
View File
@@ -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.
+12
View File
@@ -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"
+22
View File
@@ -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)"
+20
View File
@@ -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)
+17
View File
@@ -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"
+41
View File
@@ -0,0 +1,41 @@
// Headless ORT C++ integration check; mirrored by scripts/ort_smoke_ref.py.
#include <onnxruntime_cxx_api.h>
#include <array>
#include <cmath>
#include <cstdio>
#include <vector>
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<float> in(static_cast<size_t>(1) * 256 * 256 * 3);
for (size_t i = 0; i < in.size(); ++i) {
in[i] = static_cast<float>(i % 1000) / 1000.0f;
}
std::array<int64_t, 4> shape{1, 256, 256, 3};
Ort::MemoryInfo mi = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
Ort::Value input = Ort::Value::CreateTensor<float>(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<float>(); // [1,195]
const float* pres = outs[1].GetTensorData<float>(); // [1,1]
const float* mask = outs[2].GetTensorData<float>(); // [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;
}