Add person model into RTMPOSE

This commit is contained in:
2026-05-14 19:05:20 +10:00
parent ee8f0d1163
commit 8e4d9a9e5d
5 changed files with 478 additions and 36 deletions
+150 -5
View File
@@ -46,6 +46,12 @@ namespace ANSCENTER {
return false;
}
ANS_DBG("ANSRTRTMPOSE", "OptimizeModel: build OK -> '%s'", optimizedModelFolder.c_str());
// Also build the bundled person-detector engine if present.
// TryLoadPersonDetector triggers ANSRTYOLO::LoadModelFromFolder
// which performs the engine build (= optimization) and caches
// it to disk; the next Initialize/LoadModel will hit the cache.
(void)TryLoadPersonDetector(_modelFolder);
return true;
}
catch (std::exception& e) {
@@ -138,6 +144,11 @@ namespace ANSCENTER {
ANS_DBG("ANSRTRTMPOSE", "LoadModel: engine ready %dx%d kps=%d sharedPool=%d",
RTM_INPUT_W, RTM_INPUT_H, _modelConfig.numKPS, m_usingSharedPool ? 1 : 0);
}
// Top-down pipeline: try to load the bundled person detector.
// Falls back gracefully (whole-image pose) when the files are missing.
(void)TryLoadPersonDetector(_modelFolder);
_modelLoadValid = true;
_isInitialized = true;
return true;
@@ -242,6 +253,9 @@ namespace ANSCENTER {
ANS_DBG("ANSRTRTMPOSE", "LoadModelFromFolder: engine ready %dx%d kps=%d sharedPool=%d",
RTM_INPUT_W, RTM_INPUT_H, _modelConfig.numKPS, m_usingSharedPool ? 1 : 0);
}
(void)TryLoadPersonDetector(_modelFolder);
_modelLoadValid = true;
_isInitialized = true;
return true;
@@ -344,6 +358,9 @@ namespace ANSCENTER {
ANS_DBG("ANSRTRTMPOSE", "Initialize: engine ready %dx%d kps=%d sharedPool=%d",
RTM_INPUT_W, RTM_INPUT_H, _modelConfig.numKPS, m_usingSharedPool ? 1 : 0);
}
(void)TryLoadPersonDetector(_modelFolder);
_modelLoadValid = true;
_isInitialized = true;
return true;
@@ -370,11 +387,56 @@ namespace ANSCENTER {
return {};
}
try {
// Top-down pose estimator: with no external bbox, treat the whole
// image as a single person crop. Higher-level orchestration that
// wants per-person pose should call ANSODBase::RunInference(input,
// bboxes, cam) instead — that overload crops each bbox and routes
// back through us per crop.
// Two pipelines:
//
// (a) Top-down (preferred) — m_personDetector loaded from
// the bundled person.onnx + person.names. Detect people
// first, then run RTMPose ONCE per person bbox. This is
// what RTMPose is designed for; works on multi-person
// scenes with small / distant subjects.
//
// (b) Standalone fallback — no detector files in the model
// folder. Treat the whole image as a single person crop.
// Only sensible for inputs that are already a person
// (selfies, single-subject fitness clips, unit-test
// crops). Higher-level callers can also use the
// ANSODBase::RunInference(input, vector<Rect>, cam)
// overload which crops each bbox and routes per-crop
// through our Preprocess.
if (m_personDetector) {
std::vector<Object> persons = m_personDetector->RunInference(inputImgBGR, camera_id);
if (persons.empty()) {
ANS_DBG("ANSRTRTMPOSE", "RunInference: detector found 0 persons cam='%s'",
camera_id.c_str());
return {};
}
std::vector<Object> results;
results.reserve(persons.size());
for (const auto& person : persons) {
// person.onnx is a single-class model — accept anything
// the detector returned, but defend against degenerate
// boxes (zero/negative area, off-image).
if (person.box.width <= 0 || person.box.height <= 0) continue;
auto poseResults = InferenceOnBox(inputImgBGR, person.box, camera_id);
for (auto& obj : poseResults) {
// Carry the detector's track-id and confidence so
// downstream tracking/visualization stays coherent
// across frames. RTMPose itself has no class /
// tracking concept of its own.
obj.trackId = person.trackId;
obj.confidence = person.confidence;
if (!person.className.empty()) obj.className = person.className;
results.push_back(std::move(obj));
}
}
ANS_DBG("ANSRTRTMPOSE", "RunInference: %zu persons -> %zu poses cam='%s'",
persons.size(), results.size(), camera_id.c_str());
return results;
}
// Fallback — single-person whole-image pose
const cv::Rect wholeImage(0, 0, inputImgBGR.cols, inputImgBGR.rows);
return InferenceOnBox(inputImgBGR, wholeImage, camera_id);
}
@@ -394,6 +456,10 @@ namespace ANSCENTER {
bool ANSRTRTMPOSE::Destroy() {
try {
if (m_personDetector) {
m_personDetector->Destroy();
m_personDetector.reset();
}
if (m_usingSharedPool) {
EnginePoolManager<float>::instance().release(m_poolKey);
m_trtEngine.reset();
@@ -699,4 +765,83 @@ namespace ANSCENTER {
cv::Size(RTM_INPUT_W, RTM_INPUT_H), cv::INTER_LINEAR);
}
// ====================================================================
// TryLoadPersonDetector
//
// Look for person.onnx + person.names alongside the pose model. If
// both exist, instantiate an ANSRTYOLO front-end and load it via
// LoadModelFromFolder (which builds / loads the TRT engine from
// cache). On success m_personDetector is non-null and RunInference
// switches to the proper top-down pipeline.
//
// Missing files are NOT an error — the wrapper is designed to work
// standalone for single-person test inputs. We log a debug line so
// it's clear which path is active and return false.
// ====================================================================
bool ANSRTRTMPOSE::TryLoadPersonDetector(const std::string& modelFolder) {
const std::string personOnnx = CreateFilePath(modelFolder, "person.onnx");
const std::string personNames = CreateFilePath(modelFolder, "person.names");
if (!FileExist(personOnnx) || !FileExist(personNames)) {
ANS_DBG("ANSRTRTMPOSE",
"TryLoadPersonDetector: person.onnx/person.names not in '%s' "
"-- standalone mode (whole-image pose)", modelFolder.c_str());
return false;
}
if (m_personDetector) {
ANS_DBG("ANSRTRTMPOSE", "TryLoadPersonDetector: already loaded, skipping");
return true;
}
try {
auto detector = std::make_unique<ANSRTYOLO>();
detector->SetLoadEngineOnCreation(this->_loadEngineOnCreation);
// SetMaxSlotsPerGpu is declared private in ANSRTYOLO (it's a
// private override of ANSODBase's public virtual). Call it via
// the base-class interface where access is public.
static_cast<ANSODBase*>(detector.get())->SetMaxSlotsPerGpu(m_maxSlotsPerGpu);
ModelConfig personCfg{};
personCfg.detectionType = DetectionType::DETECTION;
personCfg.modelType = ModelType::RTYOLO;
personCfg.precisionType = PrecisionType::FP16;
personCfg.inpHeight = 640;
personCfg.inpWidth = 640;
personCfg.detectionScoreThreshold = 0.5f;
personCfg.modelConfThreshold = 0.5f;
personCfg.modelMNSThreshold = 0.45f;
personCfg.numKPS = 0;
personCfg.gpuOptBatchSize = 1;
personCfg.gpuMaxBatchSize = 1;
personCfg.gpuDeviceIndex = m_options.deviceIndex;
std::string personLabelMap;
const bool ok = detector->LoadModelFromFolder(
/*licenseKey*/ _licenseKey,
personCfg,
/*modelName*/ "person",
/*className*/ "person.names",
/*modelFolder*/ modelFolder,
personLabelMap);
if (!ok) {
_logger.LogWarn("ANSRTRTMPOSE::TryLoadPersonDetector",
"Person detector LoadModelFromFolder FAILED -- falling back to whole-image pose",
__FILE__, __LINE__);
ANS_DBG("ANSRTRTMPOSE", "TryLoadPersonDetector: detector load FAILED");
return false;
}
m_personDetector = std::move(detector);
ANS_DBG("ANSRTRTMPOSE",
"TryLoadPersonDetector: detector loaded OK from '%s' (labels='%s')",
modelFolder.c_str(), personLabelMap.c_str());
return true;
}
catch (const std::exception& e) {
_logger.LogWarn("ANSRTRTMPOSE::TryLoadPersonDetector", e.what(), __FILE__, __LINE__);
ANS_DBG("ANSRTRTMPOSE", "TryLoadPersonDetector: EXCEPTION '%s'", e.what());
m_personDetector.reset();
return false;
}
}
} // namespace ANSCENTER