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.
This commit is contained in:
1 parent
2715a706a0
commit
41534b07d1
6 files changed
+256
-2
No files matched your search
@@ -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"
|
||||
@@ -0,0 +1,98 @@
|
||||
#include "frame_pipeline.h"
|
||||
|
||||
#include <array>
|
||||
#include <cstring>
|
||||
|
||||
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<int>(c.depth_width);
|
||||
m.depth_height = static_cast<int>(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<size_t>(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<uint16_t>(static_cast<size_t>(config_.depth_width) *
|
||||
config_.depth_height,
|
||||
0)};
|
||||
|
||||
if (tracked) {
|
||||
std::array<MediaPipeLandmark, kMediaPipeLandmarkCount> landmarks{};
|
||||
for (size_t i = 0; i < kMediaPipeLandmarkCount; ++i) {
|
||||
landmarks[i].u = pose->image_xy[i * 2] / static_cast<float>(camera_width);
|
||||
landmarks[i].v = pose->image_xy[i * 2 + 1] / static_cast<float>(camera_height);
|
||||
landmarks[i].visibility = pose->visibility[i];
|
||||
landmarks[i].world_z = pose->world_z[i];
|
||||
}
|
||||
skeleton = mapper_.map(landmarks);
|
||||
|
||||
SegmentationMask mask{
|
||||
{static_cast<uint32_t>(pose->image_mask_width),
|
||||
static_cast<uint32_t>(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<uint32_t>(camera_width),
|
||||
static_cast<uint32_t>(camera_height)},
|
||||
static_cast<uint32_t>(camera_width) * 4u,
|
||||
std::vector<uint8_t>(camera_bgra,
|
||||
camera_bgra + static_cast<size_t>(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());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#ifndef TRACKER_NATIVE_FRAME_PIPELINE_H
|
||||
#define TRACKER_NATIVE_FRAME_PIPELINE_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
#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<uint8_t> color_scratch_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,76 @@
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
|
||||
#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<char*>(&w), 4);
|
||||
rgbf.read(reinterpret_cast<char*>(&h), 4);
|
||||
std::vector<uint8_t> rgb(static_cast<size_t>(w) * h * 3);
|
||||
rgbf.read(reinterpret_cast<char*>(rgb.data()), rgb.size());
|
||||
|
||||
std::vector<uint8_t> bgra(static_cast<size_t>(w) * h * 4);
|
||||
for (size_t i = 0; i < static_cast<size_t>(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<uint8_t> shm(region, 0);
|
||||
FramePipeline pipe(shm.data(), cfg);
|
||||
|
||||
uint64_t seq = pipe.publish(1, 1000, &pose, bgra.data(), w, h);
|
||||
|
||||
auto* fc = reinterpret_cast<FrameContract*>(shm.data());
|
||||
const FrameData& d = fc->slots[fc->published_sequence % kFrameSlotCount].data;
|
||||
const auto* depth = reinterpret_cast<const uint16_t*>(shm.data() + d.depth.byte_offset);
|
||||
|
||||
size_t body = 0;
|
||||
for (size_t i = 0; i < static_cast<size_t>(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<unsigned long long>(seq), pose.presence,
|
||||
d.skeleton.skeleton_tracking_id, tracked,
|
||||
body, 100.0 * body / (static_cast<double>(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;
|
||||
}
|
||||
@@ -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<float>(
|
||||
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<float>();
|
||||
const float* pres = outs[1].GetTensorData<float>();
|
||||
const float* mask = outs[2].GetTensorData<float>();
|
||||
const float* world = outs[3].GetTensorData<float>();
|
||||
|
||||
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<size_t>(kMaskDim) * kMaskDim);
|
||||
|
||||
const Affine2x3 inv = m.inverse();
|
||||
out.image_mask_width = width;
|
||||
out.image_mask_height = height;
|
||||
out.image_mask.assign(static_cast<size_t>(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<float>(ix), static_cast<float>(iy), &cu, &cv);
|
||||
const int u0 = static_cast<int>(std::floor(cu));
|
||||
const int v0 = static_cast<int>(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<size_t>(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<size_t>(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;
|
||||
|
||||
@@ -27,8 +27,12 @@ struct PoseResult {
|
||||
float presence = 0.0f;
|
||||
std::array<float, kBlazeposeLandmarks * 2> image_xy{};
|
||||
std::array<float, kBlazeposeLandmarks> z{};
|
||||
std::array<float, kBlazeposeLandmarks> world_z{};
|
||||
std::array<float, kBlazeposeLandmarks> visibility{};
|
||||
std::vector<float> mask;
|
||||
int image_mask_width = 0;
|
||||
int image_mask_height = 0;
|
||||
std::vector<float> image_mask;
|
||||
Roi next_roi{};
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user