Files
ANSCORE/modules/ANSODEngine/ANSONNXRTMPOSE.h
T
2026-06-18 05:55:31 +10:00

183 lines
8.7 KiB
C++

#ifndef ANSONNXRTMPOSE_H
#define ANSONNXRTMPOSE_H
#pragma once
#include "ANSEngineCommon.h"
#include "ONNXEngine.h"
#include "ANSONNXYOLO.h" // internal person-detector front-end (top-down pipeline)
namespace ANSCENTER {
// ====================================================================
// ONNXRtmPose — BasicOrtHandler subclass for RTMPose (SimCC) ONNX
//
// Ported from C:\Projects\AIVISION\RTM_POSE\OnnxRuntime-CPU\
// OnnxRunTime-CPU\rtmpose_onnxruntime.cpp but using BasicOrtHandler
// so the session benefits from the shared EP loader (CUDA / TensorRT
// / DirectML / OpenVINO / CPU auto-detect), per-session high-perf
// options, EP fallback recovery, and the wstring path handling that
// BasicOrtHandler centralises.
//
// RTMPose is a top-down single-person pose estimator:
// Input : person crop, 192x256 RGB, ImageNet-normalized
// Output : two SimCC heatmaps
// simcc_x [1, K, ext_w]
// simcc_y [1, K, ext_h]
// K = number of keypoints (e.g. 17 / 26 / 133)
// ext_w / ext_h = SimCC oversampled dims (typ. 2 * input)
//
// Class name is mixed-case (not ONNXRTMPOSE) to avoid a collision with
// the unscoped enum value ModelType::ONNXRTMPOSE in ANSLicense.h —
// identical spelling would shadow the type.
// ====================================================================
class ONNXENGINE_API ONNXRtmPose : public BasicOrtHandler
{
public:
explicit ONNXRtmPose(const std::string& _onnx_path,
unsigned int _num_threads = 1);
explicit ONNXRtmPose(const std::string& _onnx_path,
EngineType engineType,
unsigned int _num_threads = 1);
~ONNXRtmPose() override = default;
// Full per-bbox pipeline. Returns one Object with kps in original-
// image coords (kps layout: [x, y, score] * K). Empty kps means
// the call short-circuited (invalid box, runtime error, etc.).
Object estimate(const cv::Mat& image, const cv::Rect& box);
// Read-back accessors so the wrapper can introspect once the
// session is alive (used to populate _modelConfig.numKPS, log a
// sanity line, etc.).
int inputWidth() const { return m_input_w; }
int inputHeight() const { return m_input_h; }
int numKeypoints() const { return m_num_points; }
private:
// Override the BasicOrtHandler pure virtuals. transform() takes a
// pre-cropped 192x256 BGR person image and produces the normalized
// CHW tensor. transformBatch() throws — RTMPose is single-person.
Ort::Value transform(const cv::Mat& mat) override;
Ort::Value transformBatch(const std::vector<cv::Mat>& images) override;
// Per-call preprocess that writes into a caller-provided buffer +
// dims vector instead of the BasicOrtHandler members (mirrors
// ONNXYOLO::transformInto). Lets concurrent threads sharing one
// ONNXRtmPose instance preprocess and infer in parallel without
// racing on input_values_handler.
Ort::Value transformInto(const cv::Mat& cropBGR,
std::vector<float>& outBuffer,
std::vector<int64_t>& outDims);
// Affine helpers — direct port of utils.h::GetAffineTransform
// and rtmpose_onnxruntime.cpp::CropImageByDetectBox.
static cv::Mat GetAffineTransform(float center_x, float center_y,
float scale_w, float scale_h,
int output_w, int output_h,
bool inverse = false);
void CropImageByDetectBox(const cv::Mat& image, const cv::Rect& box,
cv::Mat& crop, cv::Mat& affine_rev);
// Read input geometry off the model (1x3xHxW). Falls back to
// 192x256 (the canonical RTMPose body-2d shape) if the ONNX
// export uses dynamic dims.
void resolveInputShape();
// Fixed RTMPose model geometry (read off the model at construction;
// defaults match the reference impl).
int m_input_w{ 192 };
int m_input_h{ 256 };
int m_num_points{ 26 };
// ImageNet normalization. Matches the IMAGE_MEAN / IMAGE_STD
// constants in C:\Projects\AIVISION\RTM_POSE\OnnxRuntime-CPU\
// OnnxRunTime-CPU\rtmpose_utils.h.
const float MEAN_R{ 123.675f };
const float MEAN_G{ 116.28f };
const float MEAN_B{ 103.53f };
const float STD_R { 58.395f };
const float STD_G { 57.12f };
const float STD_B { 57.375f };
};
// ====================================================================
// ANSONNXRTMPOSE — ANSODBase wrapper around ONNXRtmPose
//
// Mirrors the structure of ANSONNXYOLO. Owns a shared_ptr<ONNXRtmPose>
// so a concurrent inference thread can copy the handle under a brief
// lock and dereference it lock-free, which lets multiple AI tasks
// share one ANSONNXRTMPOSE without serialising on the engine mutex.
// ====================================================================
class ANSENGINE_API ANSONNXRTMPOSE : public ANSODBase {
public:
// Explicit non-copy/non-move (see ANSONNXYOLO for dllexport rationale).
ANSONNXRTMPOSE(const ANSONNXRTMPOSE&) = delete;
ANSONNXRTMPOSE& operator=(const ANSONNXRTMPOSE&) = delete;
ANSONNXRTMPOSE(ANSONNXRTMPOSE&&) = delete;
ANSONNXRTMPOSE& operator=(ANSONNXRTMPOSE&&) = delete;
ANSONNXRTMPOSE() = default;
bool Initialize(std::string licenseKey, ModelConfig modelConfig,
const std::string& modelZipFilePath,
const std::string& modelZipPassword,
std::string& labelMap) override;
bool LoadModel(const std::string& modelZipFilePath,
const std::string& modelZipPassword) override;
bool LoadModelFromFolder(std::string licenseKey, ModelConfig modelConfig,
std::string modelName, std::string className,
const std::string& modelFolder,
std::string& labelMap) override;
bool OptimizeModel(bool fp16, std::string& optimizedModelFolder) override;
std::vector<Object> RunInference(const cv::Mat& input) override;
std::vector<Object> RunInference(const cv::Mat& input,
const std::string& camera_id) override;
bool Destroy() override;
~ANSONNXRTMPOSE();
private:
std::string _modelFilePath;
bool _modelLoadValid{ false };
// Keypoint confidence floor. The SimCC max score is emitted as
// kps[2] regardless; this is just the threshold used to compute
// the per-Object confidence average.
float KPS_THRESHOLD{ 0.3f };
// ORT engine. shared_ptr (mirrors ANSONNXYOLO) so an inference
// thread holding a local copy keeps the underlying session alive
// even if a future CPU-fallback / EP-rebuild path resets the
// member here on another thread.
std::shared_ptr<ONNXRtmPose> m_ortEngine;
// ── Optional internal person detector (top-down pipeline) ───────
// RTMPose is a top-down single-person estimator. If the model
// folder bundles person.onnx + person.names alongside the pose
// model, we auto-load an ANSONNXYOLO person detector and run pose
// ONCE PER DETECTED PERSON. When those files are missing the
// wrapper falls back to whole-image pose so single-person test
// inputs (selfies, fitness clips) still work.
std::unique_ptr<ANSONNXYOLO> m_personDetector;
// Look for person.onnx + person.names in `modelFolder`. Loads
// m_personDetector and returns true on success; returns false
// (without logging an error) if either file is missing — caller
// should treat that as "no detector, fall back to whole-image".
bool TryLoadPersonDetector(const std::string& modelFolder);
bool InitOrtEngine();
bool InitOrtEngine(ANSCENTER::EngineType engineType);
void WarmUpEngine();
// Per-bbox pipeline used by RunInference. Pulled out as its own
// method so the future bbox-overload (ANSODBase::RunInference with
// a vector<cv::Rect>) can route here directly.
std::vector<Object> DetectObjects(const cv::Mat& inputImage,
const std::string& camera_id);
};
} // namespace ANSCENTER
#endif