From 41534b07d1093e192d7e88e870018d644478a1fc Mon Sep 17 00:00:00 2001 From: kinect-bot Date: Wed, 25 Nov 2026 18:52:07 -0500 Subject: [PATCH] feat(tracker-native): full pose->mapping->synth->shm pipeline (headless-validated) FramePipeline ties PoseEstimator to the existing C++ core: 33 landmarks -> core/mapping SkeletonMapper (20 Kinect joints), the image-space segmentation mask + skeleton -> core/synth DepthRasterizer (articulated packed depth), color -> passthrough_color, all published via ShmPublisher. PoseEstimator now also emits world-landmark z (Identity_4) and an image-space mask (inverse-affine warp). pipeline_probe on grace_hopper: tracking_id=1, 9/20 joints tracked, 56% body depth pixels, hip at (0,-1.05,2.5)m - the whole tracker brain verified without a camera. Remaining: Media Foundation capture + Dear ImGui GUI. --- tracker-native/build.sh | 10 +++ tracker-native/src/frame_pipeline.cpp | 98 +++++++++++++++++++++++++++ tracker-native/src/frame_pipeline.h | 40 +++++++++++ tracker-native/src/pipeline_probe.cpp | 76 +++++++++++++++++++++ tracker-native/src/pose_estimator.cpp | 30 +++++++- tracker-native/src/pose_estimator.h | 4 ++ 6 files changed, 256 insertions(+), 2 deletions(-) create mode 100644 tracker-native/src/frame_pipeline.cpp create mode 100644 tracker-native/src/frame_pipeline.h create mode 100644 tracker-native/src/pipeline_probe.cpp diff --git a/tracker-native/build.sh b/tracker-native/build.sh index e9f0108..fa83d78 100755 --- a/tracker-native/build.sh +++ b/tracker-native/build.sh @@ -25,3 +25,13 @@ g++ -std=c++17 -O2 -Wall -Wextra \ -o "${HERE}/build/pose_probe" \ -L "${ORT}/lib" -lonnxruntime -Wl,-rpath,"${ORT}/lib" echo "built ${HERE}/build/pose_probe" + +g++ -std=c++17 -O2 -Wall -Wextra \ + -I "${ORT}/include" -I "${HERE}/src" -I "${CORE}" \ + "${HERE}/src/pose_estimator.cpp" "${HERE}/src/shm_publisher.cpp" \ + "${HERE}/src/frame_pipeline.cpp" "${HERE}/src/pipeline_probe.cpp" \ + "${CORE}/mapping/mapping.cpp" "${CORE}/synth/synth.cpp" \ + "${CORE}/synth/mask_stabilizer.cpp" "${CORE}/contract/contract.cpp" \ + -o "${HERE}/build/pipeline_probe" \ + -L "${ORT}/lib" -lonnxruntime -Wl,-rpath,"${ORT}/lib" +echo "built ${HERE}/build/pipeline_probe" diff --git a/tracker-native/src/frame_pipeline.cpp b/tracker-native/src/frame_pipeline.cpp new file mode 100644 index 0000000..ae8f862 --- /dev/null +++ b/tracker-native/src/frame_pipeline.cpp @@ -0,0 +1,98 @@ +#include "frame_pipeline.h" + +#include +#include + +namespace kinect_tracker { + +using nui::contract::JointTrackingState; +using nui::contract::SkeletonFrame; +using nui::mapping::kMediaPipeLandmarkCount; +using nui::mapping::MappingConfig; +using nui::mapping::MediaPipeLandmark; +using nui::synth::BgraFrame; +using nui::synth::DepthFrame; +using nui::synth::ImageSize; +using nui::synth::SegmentationMask; + +namespace { + +MappingConfig make_mapping_config(const PipelineConfig& c) { + MappingConfig m; + m.depth_width = static_cast(c.depth_width); + m.depth_height = static_cast(c.depth_height); + m.z0_meters = 2.5f; + m.depth_gain = 1.0f; + m.mirror = true; + return m; +} + +} + +FramePipeline::FramePipeline(uint8_t* shm_base, const PipelineConfig& config) + : config_(config), + mapper_(make_mapping_config(config)), + publisher_(shm_base, config.depth_width, config.depth_height, + config.color_width, config.color_height), + color_scratch_(static_cast(config.color_width) * + config.color_height * 4u) { + publisher_.init(); +} + +uint64_t FramePipeline::publish(uint32_t frame_number, int64_t timestamp_micros, + const PoseResult* pose, const uint8_t* camera_bgra, + int camera_width, int camera_height) { + const bool tracked = pose != nullptr && pose->valid && + pose->presence >= config_.presence_threshold; + + SkeletonFrame skeleton{}; + DepthFrame depth{{config_.depth_width, config_.depth_height}, + config_.depth_width * 2u, + std::vector(static_cast(config_.depth_width) * + config_.depth_height, + 0)}; + + if (tracked) { + std::array landmarks{}; + for (size_t i = 0; i < kMediaPipeLandmarkCount; ++i) { + landmarks[i].u = pose->image_xy[i * 2] / static_cast(camera_width); + landmarks[i].v = pose->image_xy[i * 2 + 1] / static_cast(camera_height); + landmarks[i].visibility = pose->visibility[i]; + landmarks[i].world_z = pose->world_z[i]; + } + skeleton = mapper_.map(landmarks); + + SegmentationMask mask{ + {static_cast(pose->image_mask_width), + static_cast(pose->image_mask_height)}, + pose->image_mask}; + depth = rasterizer_.rasterize( + mask, skeleton, + {{config_.depth_width, config_.depth_height}, 0}); + } else { + skeleton.skeleton_tracking_id = 0; + for (auto& j : skeleton.joints) { + j.tracking_state = JointTrackingState::NOT_TRACKED; + j.position = {0.0f, 0.0f, 0.0f, 1.0f}; + } + } + + const ImageSize color_size{config_.color_width, config_.color_height}; + if (camera_bgra != nullptr) { + BgraFrame src{{static_cast(camera_width), + static_cast(camera_height)}, + static_cast(camera_width) * 4u, + std::vector(camera_bgra, + camera_bgra + static_cast(camera_width) * + camera_height * 4u)}; + BgraFrame scaled = nui::synth::passthrough_color(src, color_size); + std::memcpy(color_scratch_.data(), scaled.pixels.data(), color_scratch_.size()); + } else { + std::memset(color_scratch_.data(), 0, color_scratch_.size()); + } + + return publisher_.publish(frame_number, timestamp_micros, skeleton, + depth.pixels.data(), color_scratch_.data()); +} + +} diff --git a/tracker-native/src/frame_pipeline.h b/tracker-native/src/frame_pipeline.h new file mode 100644 index 0000000..4af48e8 --- /dev/null +++ b/tracker-native/src/frame_pipeline.h @@ -0,0 +1,40 @@ +#ifndef TRACKER_NATIVE_FRAME_PIPELINE_H +#define TRACKER_NATIVE_FRAME_PIPELINE_H + +#include +#include + +#include "mapping/mapping.h" +#include "pose_estimator.h" +#include "shm_publisher.h" +#include "synth/synth.h" + +namespace kinect_tracker { + +struct PipelineConfig { + uint32_t depth_width = 640; + uint32_t depth_height = 480; + uint32_t color_width = 640; + uint32_t color_height = 480; + float presence_threshold = 0.5f; +}; + +class FramePipeline { + public: + FramePipeline(uint8_t* shm_base, const PipelineConfig& config); + + uint64_t publish(uint32_t frame_number, int64_t timestamp_micros, + const PoseResult* pose, const uint8_t* camera_bgra, + int camera_width, int camera_height); + + private: + PipelineConfig config_; + nui::mapping::SkeletonMapper mapper_; + nui::synth::DepthRasterizer rasterizer_; + ShmPublisher publisher_; + std::vector color_scratch_; +}; + +} + +#endif diff --git a/tracker-native/src/pipeline_probe.cpp b/tracker-native/src/pipeline_probe.cpp new file mode 100644 index 0000000..5237dc3 --- /dev/null +++ b/tracker-native/src/pipeline_probe.cpp @@ -0,0 +1,76 @@ +#include +#include +#include +#include + +#include "contract/contract.h" +#include "frame_pipeline.h" +#include "pose_estimator.h" + +using kinect_tracker::FramePipeline; +using kinect_tracker::PipelineConfig; +using kinect_tracker::PoseEstimator; +using kinect_tracker::PoseResult; +using kinect_tracker::Roi; +using kinect_tracker::ShmPublisher; +using nui::contract::FrameContract; +using nui::contract::FrameData; +using nui::contract::JointTrackingState; +using nui::contract::kFrameSlotCount; + +int main(int argc, char** argv) { + const char* model = argc > 1 ? argv[1] : "models/pose_landmarks_detector.onnx"; + std::ifstream rgbf("testdata/sample_rgb.bin", std::ios::binary); + int32_t w = 0, h = 0; + rgbf.read(reinterpret_cast(&w), 4); + rgbf.read(reinterpret_cast(&h), 4); + std::vector rgb(static_cast(w) * h * 3); + rgbf.read(reinterpret_cast(rgb.data()), rgb.size()); + + std::vector bgra(static_cast(w) * h * 4); + for (size_t i = 0; i < static_cast(w) * h; ++i) { + bgra[i * 4 + 0] = rgb[i * 3 + 2]; + bgra[i * 4 + 1] = rgb[i * 3 + 1]; + bgra[i * 4 + 2] = rgb[i * 3 + 0]; + bgra[i * 4 + 3] = 255; + } + + Roi roi; + std::ifstream roif("testdata/roi.txt"); + roif >> roi.cx >> roi.cy >> roi.size >> roi.angle; + + PoseEstimator est(model); + PoseResult pose = est.run(rgb.data(), w, h, roi); + + PipelineConfig cfg; + const size_t region = + ShmPublisher::region_size(cfg.depth_width, cfg.depth_height, + cfg.color_width, cfg.color_height); + std::vector shm(region, 0); + FramePipeline pipe(shm.data(), cfg); + + uint64_t seq = pipe.publish(1, 1000, &pose, bgra.data(), w, h); + + auto* fc = reinterpret_cast(shm.data()); + const FrameData& d = fc->slots[fc->published_sequence % kFrameSlotCount].data; + const auto* depth = reinterpret_cast(shm.data() + d.depth.byte_offset); + + size_t body = 0; + for (size_t i = 0; i < static_cast(cfg.depth_width) * cfg.depth_height; ++i) { + if (depth[i] != 0) ++body; + } + int tracked = 0; + for (const auto& j : d.skeleton.joints) { + if (j.tracking_state == JointTrackingState::TRACKED) ++tracked; + } + + std::printf("pipeline OK: seq=%llu presence=%.3f tracking_id=%u tracked_joints=%d/20 " + "body_depth_px=%zu (%.1f%%)\n", + static_cast(seq), pose.presence, + d.skeleton.skeleton_tracking_id, tracked, + body, 100.0 * body / (static_cast(cfg.depth_width) * cfg.depth_height)); + const auto& hip = d.skeleton.joints[0].position; + std::printf("hip-center joint: (%.3f, %.3f, %.3f) m\n", hip.x, hip.y, hip.z); + + return (body > 0 && tracked > 0) ? 0 : 1; +} diff --git a/tracker-native/src/pose_estimator.cpp b/tracker-native/src/pose_estimator.cpp index 0939531..a01b7c8 100644 --- a/tracker-native/src/pose_estimator.cpp +++ b/tracker-native/src/pose_estimator.cpp @@ -18,6 +18,11 @@ struct Affine2x3 { *x = a * u + b * v + tx; *y = c * u + d * v + ty; } + Affine2x3 inverse() const { + const float det = a * d - b * c; + const float ia = d / det, ib = -b / det, ic = -c / det, id = a / det; + return {ia, ib, -(ia * tx + ib * ty), ic, id, -(ic * tx + id * ty)}; + } }; // crop(u,v in [0,kLandmarkInput]) -> source-image pixel, matching @@ -110,13 +115,14 @@ PoseResult PoseEstimator::run(const uint8_t* rgb, int width, int height, Ort::Value input = Ort::Value::CreateTensor( mi, input_.data(), input_.size(), shape.data(), shape.size()); const char* in_names[] = {"input_1"}; - const char* out_names[] = {"Identity", "Identity_1", "Identity_2"}; + const char* out_names[] = {"Identity", "Identity_1", "Identity_2", "Identity_4"}; auto outs = session_->Run(Ort::RunOptions{nullptr}, in_names, &input, 1, - out_names, 3); + out_names, 4); const float* lm = outs[0].GetTensorData(); const float* pres = outs[1].GetTensorData(); const float* mask = outs[2].GetTensorData(); + const float* world = outs[3].GetTensorData(); PoseResult out; out.valid = true; @@ -129,10 +135,30 @@ PoseResult PoseEstimator::run(const uint8_t* rgb, int width, int height, out.image_xy[i * 2 + 0] = ix; out.image_xy[i * 2 + 1] = iy; out.z[i] = lm[i * 5 + 2]; + out.world_z[i] = world[i * 3 + 2]; out.visibility[i] = Sigmoid(lm[i * 5 + 3]); } out.mask.assign(mask, mask + static_cast(kMaskDim) * kMaskDim); + const Affine2x3 inv = m.inverse(); + out.image_mask_width = width; + out.image_mask_height = height; + out.image_mask.assign(static_cast(width) * height, 0.0f); + for (int iy = 0; iy < height; ++iy) { + for (int ix = 0; ix < width; ++ix) { + float cu, cv; + inv.apply(static_cast(ix), static_cast(iy), &cu, &cv); + const int u0 = static_cast(std::floor(cu)); + const int v0 = static_cast(std::floor(cv)); + if (u0 < 0 || v0 < 0 || u0 + 1 >= kMaskDim || v0 + 1 >= kMaskDim) continue; + const float fu = cu - u0, fv = cv - v0; + auto mv = [&](int u, int v) { return mask[static_cast(v) * kMaskDim + u]; }; + const float top = mv(u0, v0) * (1 - fu) + mv(u0 + 1, v0) * fu; + const float bot = mv(u0, v0 + 1) * (1 - fu) + mv(u0 + 1, v0 + 1) * fu; + out.image_mask[static_cast(iy) * width + ix] = Sigmoid(top * (1 - fv) + bot * fv); + } + } + out.next_roi = roi_from_points(out.image_xy[33 * 2], out.image_xy[33 * 2 + 1], out.image_xy[34 * 2], out.image_xy[34 * 2 + 1]); return out; diff --git a/tracker-native/src/pose_estimator.h b/tracker-native/src/pose_estimator.h index b040469..4a546b2 100644 --- a/tracker-native/src/pose_estimator.h +++ b/tracker-native/src/pose_estimator.h @@ -27,8 +27,12 @@ struct PoseResult { float presence = 0.0f; std::array image_xy{}; std::array z{}; + std::array world_z{}; std::array visibility{}; std::vector mask; + int image_mask_width = 0; + int image_mask_height = 0; + std::vector image_mask; Roi next_roi{}; };