From f34817c62023b2ac92f1cb2ea26c565edf93be2e Mon Sep 17 00:00:00 2001 From: Tuan Nghia Nguyen Date: Thu, 27 Aug 2026 01:57:54 +1000 Subject: [PATCH] ANSFR: configurable slow-attribute refresh interval New API SetANSRFAttributeInterval(Handle, frames) / UpdateAttributeInterval (1..60, default 5) controls how often the slow attributes (age/gender + emotions) refresh per camera; head pose and recognition are unaffected. GetANSRFModelInfo now reports "attribute_interval". The adaptive logic's stable-scene branch previously capped at a hardcoded 10 - it now doubles the configured interval instead (default behaviour 10/5/2 unchanged). Motivation: the v1.4 ViT age-gender model is heavier per attribute frame than the old OpenVINO IR. Interval sweep on RTX 3070 (classroom.mp4, ~3.1 faces/frame) vs the v1.1 baseline at 16.0 ms / 62 fps: interval 5 (default): 19.9 ms / 50 fps (+3.9 ms) interval 10: 17.1 ms / 58 fps (+1.1 ms) interval 20: 16.5 ms / 61 fps (parity) Recommendation: default on 8 GB GPUs; interval 10 on small-VRAM machines. ANSFR-UnitTest: --bench old|new [interval] runs the sweep. Co-Authored-By: Claude Fable 5 --- modules/ANSFR/ANSFR.cpp | 22 ++++++++++++++++++++-- modules/ANSFR/ANSFR.h | 13 +++++++++++++ modules/ANSFR/dllmain.cpp | 12 ++++++++++++ tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp | 24 ++++++++++++++++++------ 4 files changed, 63 insertions(+), 8 deletions(-) diff --git a/modules/ANSFR/ANSFR.cpp b/modules/ANSFR/ANSFR.cpp index 2f730d1..63ec3fd 100644 --- a/modules/ANSFR/ANSFR.cpp +++ b/modules/ANSFR/ANSFR.cpp @@ -1348,6 +1348,22 @@ namespace ANSCENTER { } } + bool ANSFacialRecognition::UpdateAttributeInterval(int frames) { + if (frames < 1 || frames > 60) { + LogThreadSafe("ANSFacialRecognition::UpdateAttributeInterval", + "Invalid interval " + std::to_string(frames) + " (expected 1..60)"); + return false; + } + { + std::lock_guard lock(_configMutex); + _attributeInterval = frames; + } + LogThreadSafe("ANSFacialRecognition::UpdateAttributeInterval", + "Attribute refresh interval set to " + std::to_string(frames) + " frames", + LogLevel::Info); + return true; + } + int ANSFacialRecognition::GetRecognizerModelType() const { return static_cast(_recognizer ? _activeRecModel : _requestedRecModel); } @@ -1373,6 +1389,7 @@ namespace ANSCENTER { info["recognizer_loaded"] = (_recognizer != nullptr); info["agegender_backend"] = agegenderBackend; info["emotion_backend"] = emotionBackend; + info["attribute_interval"] = _attributeInterval; return info.dump(); } catch (const std::exception& e) { @@ -3072,9 +3089,10 @@ namespace ANSCENTER { } camDataPtr->previousTrackIdCount = currentTrackIdCount; - // Adapt interval: stable scene → increase (max 10), changes → decrease (min 2) + // Adapt interval: stable scene → double the configured + // interval, changes → decrease (min 2) if (camDataPtr->stableFrameCount > 30) { - camDataPtr->currentAttributeInterval = std::min(10, _attributeInterval * 2); + camDataPtr->currentAttributeInterval = _attributeInterval * 2; } else if (camDataPtr->stableFrameCount > 10) { camDataPtr->currentAttributeInterval = _attributeInterval; } else { diff --git a/modules/ANSFR/ANSFR.h b/modules/ANSFR/ANSFR.h index f91a42e..c94522e 100644 --- a/modules/ANSFR/ANSFR.h +++ b/modules/ANSFR/ANSFR.h @@ -98,6 +98,13 @@ namespace ANSCENTER bool UpdateFaceQueue(int queueSize, int faceThresholdSize, bool enableFaceQueue); bool GetFaceQueue(int& queueSize, int& faceThresholdSize, bool& enableFaceQueue); + // How often the slow attributes (age/gender + emotions) refresh: + // every N frames per camera (1 = every frame). Head pose still runs + // every frame. The adaptive logic doubles N on stable scenes and + // halves it (min 2) when faces appear/disappear. Default 5. + bool UpdateAttributeInterval(int frames); + int GetAttributeInterval() const { return _attributeInterval; } + // Recognizer model switching (0 = AdaFace [default], 1 = ArcFace). // Before LoadEngine: just records the preference. After LoadEngine: // re-initializes the recognizer, regenerates every stored embedding @@ -391,6 +398,12 @@ extern "C" ANSFR_API int GetANSRFRecognizerModel(ANSCENTER::ANSFacialRe // JSON describing the loaded models, e.g. // {"recognizer_model":"adaface","agegender_backend":"onnx","emotion_backend":"onnx",...} extern "C" ANSFR_API int GetANSRFModelInfo(ANSCENTER::ANSFacialRecognition** Handle, LStrHandle modelInfo); +// Slow-attribute (age/gender + emotions) refresh interval in frames per +// camera (1 = every frame, default 5; clamped to [1, 60]). Larger values +// cut the attribute-model GPU cost proportionally at the price of slower +// attribute updates — useful on smaller GPUs with the heavier v1.4 ONNX +// age-gender model. Returns 1 on success, -1 on invalid arguments. +extern "C" ANSFR_API int SetANSRFAttributeInterval(ANSCENTER::ANSFacialRecognition** Handle, int frames); diff --git a/modules/ANSFR/dllmain.cpp b/modules/ANSFR/dllmain.cpp index 36f7396..e75feb5 100644 --- a/modules/ANSFR/dllmain.cpp +++ b/modules/ANSFR/dllmain.cpp @@ -1330,6 +1330,18 @@ extern "C" ANSFR_API int GetANSRFModelInfo(ANSCENTER::ANSFacialRecognit } } +extern "C" ANSFR_API int SetANSRFAttributeInterval(ANSCENTER::ANSFacialRecognition** Handle, int frames) { + ANS_DBG("ANSFR","SetANSRFAttributeInterval: HandlePtr=%p, *Handle=%p, frames=%d", + (void*)Handle, (void*)(Handle ? *Handle : nullptr), frames); + try { + if (!Handle || !*Handle) return -1; + return (*Handle)->UpdateAttributeInterval(frames) ? 1 : -1; + } + catch (const std::exception&) { + return -1; + } +} + extern "C" ANSFR_API int RunInferenceComplete_LV( ANSCENTER::ANSFacialRecognition** Handle, cv::Mat** cvImage, diff --git a/tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp b/tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp index 1c0bff7..044faa7 100644 --- a/tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp +++ b/tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp @@ -1990,7 +1990,8 @@ int ClassroomAttributesVideoTest() // =========================================================================== static void RunSinglePackBenchmark(const std::string& label, const std::string& modelZip, const std::string& dbFolder, - int warmupFrames = 10, int measuredFrames = 100) + int warmupFrames = 10, int measuredFrames = 100, + int attributeInterval = 0) // 0 = keep the DLL default (5) { const std::string videoPath = "C:\\Programs\\DemoAssets\\Videos\\classroom.mp4"; const std::string detectorZip = kFaceDetectorZip; @@ -2015,6 +2016,10 @@ static void RunSinglePackBenchmark(const std::string& label, auto loadMs = std::chrono::duration_cast( std::chrono::steady_clock::now() - loadStart).count(); std::cout << "LoadANSRFEngine = " << le << " (" << loadMs << " ms)" << std::endl; + if (attributeInterval > 0) { + SetANSRFAttributeInterval(&h, attributeInterval); + std::cout << "Attribute interval overridden to " << attributeInterval << std::endl; + } std::cout << "Model info: " << h->GetActiveModelInfo() << std::endl; if (le != 1) { ReleaseANSRFHandle(&h); return; } @@ -2231,17 +2236,24 @@ int main(int argc, char** argv) } // Single-pack benchmark mode (one pack per process so the packs cannot - // influence each other): ANSFR-UnitTest.exe --bench old|new|both + // influence each other): ANSFR-UnitTest.exe --bench old|new|both [interval] + // The optional interval overrides the slow-attribute refresh interval + // (default 5) via SetANSRFAttributeInterval. if (argc >= 3 && std::string(argv[1]) == "--bench") { const std::string which = argv[2]; + const int interval = (argc >= 4) ? std::atoi(argv[3]) : 0; + const std::string tag = (interval > 0) + ? (" @interval " + std::to_string(interval)) : std::string(); if (which == "old") - RunSinglePackBenchmark("v1.1 arcface + OpenVINO attrs", + RunSinglePackBenchmark("v1.1 arcface + OpenVINO attrs" + tag, kFaceRecognizerV11Zip, - "C:\\Programs\\DemoAssets\\ANSFRDB\\v11_bench"); + "C:\\Programs\\DemoAssets\\ANSFRDB\\v11_bench", + 10, 100, interval); else if (which == "new") - RunSinglePackBenchmark("v1.4 adaface + ONNX attrs", + RunSinglePackBenchmark("v1.4 adaface + ONNX attrs" + tag, kFaceRecognizerV14Zip, - "C:\\Programs\\DemoAssets\\ANSFRDB\\v14_bench"); + "C:\\Programs\\DemoAssets\\ANSFRDB\\v14_bench", + 10, 100, interval); else ModelSpeedBenchmark(); return 0;