Files
ANSCORE/modules/ANSODEngine/ANSRTRTMPOSE.h
T
2026-05-14 17:48:09 +10:00

126 lines
5.9 KiB
C++

#ifndef ANSRTRTMPOSE_H
#define ANSRTRTMPOSE_H
#pragma once
#include "ANSEngineCommon.h"
#include "engine.h"
#include "ANSGpuFrameRegistry.h"
#include "engine/EnginePoolManager.h"
namespace ANSCENTER {
// ====================================================================
// ANSRTRTMPOSE — TensorRT-based RTMPose (SimCC) pose estimator
//
// Ported from C:\Projects\AIVISION\RTM_POSE\TensorRT\rtmpose_tensorrt.cpp
// but uses Engine<float> from TensorRTAPI (shared pool / pinned upload /
// multi-GPU dispatch / engine cache) instead of raw nvinfer1.
//
// 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)
//
// RunInference(image) treats the whole input as one person crop. To run
// on multiple people, call ANSODBase::RunInference(image, bboxes, cam) —
// the base class crops each bbox and dispatches to RunInference per crop.
// ====================================================================
class ANSENGINE_API ANSRTRTMPOSE : public ANSODBase {
public:
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);
std::vector<Object> RunInference(const cv::Mat& input,
const std::string& camera_id);
bool Destroy();
~ANSRTRTMPOSE();
private:
std::string _modelFilePath;
bool _modelLoadValid{ false };
bool _fp16{ false };
int m_maxSlotsPerGpu{ 1 };
void SetMaxSlotsPerGpu(int n) override { m_maxSlotsPerGpu = n; }
// Per-call image geometry — captured before inference, consumed at postprocess.
// affine_rev maps SimCC output coords (in 192x256 model space) back to
// the original image. Carrying this per-call keeps the class re-entrant.
struct ImageMetadata {
cv::Mat affine_rev;
float imgWidth = 0.f;
float imgHeight = 0.f;
cv::Rect personBox; // bbox the affine was built from
};
// Single-bbox pipeline: affine-crop -> upload -> Engine.runInference -> SimCC decode
std::vector<Object> InferenceOnBox(const cv::Mat& input,
const cv::Rect& box,
const std::string& camera_id);
// Build the 192x256 RGB crop around `box` and upload to GPU. outMeta
// captures the inverse-affine and original image size for postprocess.
std::vector<std::vector<cv::cuda::GpuMat>> Preprocess(const cv::Mat& inputImage,
const cv::Rect& box,
ImageMetadata& outMeta);
// Decode SimCC outputs into one Object with kps in original-image coords.
Object PostprocessSimCC(const std::vector<float>& simcc_x,
const std::vector<float>& simcc_y,
int numKeypoints, int extW, int extH,
const ImageMetadata& meta,
const std::string& camera_id);
// Affine helpers — direct port of utils.h::GetAffineTransform and
// rtmpose_tensorrt.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);
// ── TensorRT engine (shared pool, same pattern as ANSRTYOLO) ─────
std::shared_ptr<Engine<float>> m_trtEngine = nullptr;
EnginePoolManager<float>::PoolKey m_poolKey;
bool m_usingSharedPool = false;
ANSCENTER::Options m_options;
// Fixed RTMPose model geometry (overridden from the engine's input dims
// at LoadModel time once the network is built).
int RTM_INPUT_W{ 192 };
int RTM_INPUT_H{ 256 };
// ImageNet normalization, expressed for Engine<T>::blobFromGpuMats:
// blob = (pixel/255 - subVals) / divVals
// subVals = mean / 255 ; divVals = std / 255
const std::array<float, 3> SUB_VALS{ 0.485f, 0.456f, 0.406f };
const std::array<float, 3> DIV_VALS{ 0.229f, 0.224f, 0.225f };
const bool NORMALIZE = true;
// Keypoint confidence floor for output. Mirrors KPS_THRESHOLD from
// ANSTENSORRTPOSE — keypoints below threshold are still emitted (the
// SimCC max is taken regardless), but caller can read kps[2] (score)
// to decide whether to draw them.
float KPS_THRESHOLD{ 0.3f };
};
} // namespace ANSCENTER
#endif