#include "pose_estimator.h" #include #include #include #include #include #include namespace kinect_tracker { namespace { constexpr float kPi = 3.14159265358979323846f; float Sigmoid(float v) { return 1.0f / (1.0f + std::exp(-v)); } struct Affine2x3 { float a, b, tx; float c, d, ty; void apply(float u, float v, float* x, float* y) const { *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 // cv2.getAffineTransform of the rotated ROI square (see blazepose_ref.crop_roi). Affine2x3 crop_to_image(const Roi& roi) { const float cos = std::cos(roi.angle); const float sin = std::sin(roi.angle); const float half = roi.size * 0.5f; const float s = roi.size / static_cast(kLandmarkInput); Affine2x3 m; m.a = s * cos; m.b = -s * sin; m.c = s * sin; m.d = s * cos; m.tx = roi.cx - half * cos + half * sin; m.ty = roi.cy - half * sin - half * cos; return m; } } PoseEstimator::PoseEstimator(const char* landmark_model_path) { env_ = std::make_unique(ORT_LOGGING_LEVEL_WARNING, "pose"); Ort::SessionOptions so; so.SetIntraOpNumThreads(1); so.SetGraphOptimizationLevel(ORT_ENABLE_ALL); #ifdef _WIN32 const std::string p(landmark_model_path); const std::wstring wp(p.begin(), p.end()); session_ = std::make_unique(*env_, wp.c_str(), so); #else session_ = std::make_unique(*env_, landmark_model_path, so); #endif input_.resize(static_cast(kLandmarkInput) * kLandmarkInput * 3); } PoseEstimator::~PoseEstimator() = default; PoseResult PoseEstimator::acquire(const uint8_t* rgb, int width, int height) { const float longest = static_cast(std::max(width, height)); const float scales[] = {0.8f, 1.1f, 1.5f}; const float cy_fracs[] = {0.4f, 0.55f}; PoseResult best; float best_presence = -1.0f; for (float scale : scales) { for (float cy : cy_fracs) { Roi roi{width * 0.5f, height * cy, longest * scale, 0.0f}; PoseResult r = run(rgb, width, height, roi); if (r.presence > best_presence) { best_presence = r.presence; best = std::move(r); } } } return best; } Roi PoseEstimator::bootstrap_roi(int width, int height) { Roi r; r.cx = width * 0.5f; r.cy = height * 0.5f; r.size = static_cast(std::max(width, height)); r.angle = 0.0f; return r; } Roi PoseEstimator::roi_from_points(float cx, float cy, float ex, float ey) { const float dx = ex - cx; const float dy = ey - cy; Roi r; r.cx = cx; r.cy = cy; r.size = 2.0f * std::hypot(dx, dy) * 1.25f; r.angle = kPi * 0.5f - std::atan2(-dy, dx); return r; } PoseResult PoseEstimator::run(const uint8_t* rgb, int width, int height, const Roi& roi) { const Affine2x3 m = crop_to_image(roi); for (int v = 0; v < kLandmarkInput; ++v) { for (int u = 0; u < kLandmarkInput; ++u) { float sx, sy; m.apply(static_cast(u), static_cast(v), &sx, &sy); float r = 0, g = 0, b = 0; const int x0 = static_cast(std::floor(sx)); const int y0 = static_cast(std::floor(sy)); if (x0 >= 0 && y0 >= 0 && x0 + 1 < width && y0 + 1 < height) { const float fx = sx - x0; const float fy = sy - y0; auto px = [&](int x, int y, int ch) -> float { return rgb[(static_cast(y) * width + x) * 3 + ch]; }; for (int ch = 0; ch < 3; ++ch) { const float top = px(x0, y0, ch) * (1 - fx) + px(x0 + 1, y0, ch) * fx; const float bot = px(x0, y0 + 1, ch) * (1 - fx) + px(x0 + 1, y0 + 1, ch) * fx; const float val = top * (1 - fy) + bot * fy; if (ch == 0) r = val; else if (ch == 1) g = val; else b = val; } } const size_t base = (static_cast(v) * kLandmarkInput + u) * 3; input_[base + 0] = r / 255.0f; input_[base + 1] = g / 255.0f; input_[base + 2] = b / 255.0f; } } const std::array shape{1, kLandmarkInput, kLandmarkInput, 3}; Ort::MemoryInfo mi = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault); 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", "Identity_4"}; auto outs = session_->Run(Ort::RunOptions{nullptr}, in_names, &input, 1, 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; out.presence = Sigmoid(pres[0]); for (int i = 0; i < kBlazeposeLandmarks; ++i) { const float lx = lm[i * 5 + 0]; const float ly = lm[i * 5 + 1]; float ix, iy; m.apply(lx, ly, &ix, &iy); 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; } }