diff --git a/modules/ANSFR/ANSFR.cpp b/modules/ANSFR/ANSFR.cpp index 0ab83e7..2f730d1 100644 --- a/modules/ANSFR/ANSFR.cpp +++ b/modules/ANSFR/ANSFR.cpp @@ -375,6 +375,27 @@ namespace ANSCENTER { bool ANSFacialRecognition::LoadEngine() { try { ANS_DBG("ANSFR", "LoadEngine: starting"); + + // Restore the last explicitly chosen recognizer model so a + // runtime SetRecognizerModelType survives restarts. A choice + // made this session (before LoadEngine) takes precedence. + if (!_recModelExplicit && _db) { + std::string pref; + { + std::lock_guard lock(_databaseMutex); + pref = _db->GetSetting("RECOGNIZER_MODEL_PREF"); + } + if (pref == "arcface") + _requestedRecModel = FaceRecognizerModel::ARCFACE; + else if (pref == "adaface") + _requestedRecModel = FaceRecognizerModel::ADAFACE; + if (!pref.empty()) { + LogThreadSafe("ANSFacialRecognition::LoadEngine", + "Restored persisted recognizer model preference: " + pref, + LogLevel::Info); + } + } + // Unload existing engine (handles its own locking) UnloadEngine(); @@ -1287,6 +1308,14 @@ namespace ANSCENTER { } const auto requested = static_cast(modelType); _requestedRecModel = requested; + _recModelExplicit = true; + + // Persist the explicit choice so it survives restarts (restored + // by LoadEngine when no choice is made in that session). + if (_db) { + std::lock_guard lock(_databaseMutex); + _db->SetSetting("RECOGNIZER_MODEL_PREF", FaceRecognizerModelName(requested)); + } // Engine not loaded yet — the preference is applied by LoadEngine. if (!_recognizer) return true; diff --git a/modules/ANSFR/ANSFR.h b/modules/ANSFR/ANSFR.h index 85388cf..f91a42e 100644 --- a/modules/ANSFR/ANSFR.h +++ b/modules/ANSFR/ANSFR.h @@ -237,6 +237,11 @@ namespace ANSCENTER // and what actually loaded (fallback may downgrade ADAFACE->ARCFACE). FaceRecognizerModel _requestedRecModel{ FaceRecognizerModel::ADAFACE }; FaceRecognizerModel _activeRecModel{ FaceRecognizerModel::ADAFACE }; + // True once SetRecognizerModelType was called this session. An + // explicit choice is persisted (RECOGNIZER_MODEL_PREF in the face + // DB) and restored by LoadEngine on later runs; without it the + // stored preference (if any) wins over the built-in default. + bool _recModelExplicit{ false }; const size_t QUEUE_SIZE = 10; const size_t FACE_THRESHOLD_SIZE = 5; // Cached attribute JSON per tracked face (everything except head pose). @@ -374,8 +379,10 @@ extern "C" ANSFR_API int GetFaceQueue(ANSCENTER::ANSFacialRecognition** Handl // after it to switch live — a live switch reloads the recognizer engine and // regenerates every stored face embedding from the enrolled face images // (embeddings produced by different models are not comparable), so it can -// take a while on large databases. Returns 1 on success, 0 on failure, -1 on -// invalid arguments. +// take a while on large databases. The explicit choice is persisted in the +// face database (RECOGNIZER_MODEL_PREF) and restored automatically on the +// next LoadANSRFEngine, so it survives restarts without re-calling this. +// Returns 1 on success, 0 on failure, -1 on invalid arguments. extern "C" ANSFR_API int SetANSRFRecognizerModel(ANSCENTER::ANSFacialRecognition** Handle, int modelType); // Returns the ACTIVE model (0 = AdaFace, 1 = ArcFace) or -1 on error. May // differ from what was requested when the model pack has no diff --git a/tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp b/tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp index 0d3c4b2..1ba5360 100644 --- a/tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp +++ b/tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp @@ -1802,6 +1802,19 @@ int TestModelSwitchV14() std::cout << "Model info: " << h->GetActiveModelInfo() << std::endl; recognizesUser("recognized after switch to ArcFace"); + // --- persistence: the explicit ArcFace choice must survive a restart --- + ReleaseANSRFHandle(&h); + h = nullptr; + { + int cr2 = CreateANSRFHandle(&h, "", "", dbFile.c_str(), modelZip.c_str(), detectorZip.c_str(), + 0, 0.35f, 1, 1, 1, 30, 0.5f, 0, 0); + V14Check(cr2 > 0 && h != nullptr, "re-create handle (simulated restart)", failures); + V14Check(LoadANSRFEngine(&h) == 1, "reload engine after restart", failures); + V14Check(GetANSRFRecognizerModel(&h) == 1, + "persisted ArcFace choice restored after restart", failures); + recognizesUser("recognized after restart with persisted ArcFace"); + } + // --- and back to AdaFace ------------------------------------------------ V14Check(SetANSRFRecognizerModel(&h, 0) == 1, "switch back to AdaFace", failures); V14Check(GetANSRFRecognizerModel(&h) == 0, "active model is AdaFace (0)", failures); @@ -2206,6 +2219,11 @@ int main(int argc, char** argv) return TestOutputFormatParity(); } + // v1.4 model-switch + persistence test: ANSFR-UnitTest.exe --test + if (argc >= 2 && std::string(argv[1]) == "--test") { + return TestModelSwitchV14(); + } + // Single-pack benchmark mode (one pack per process so the packs cannot // influence each other): ANSFR-UnitTest.exe --bench old|new|both if (argc >= 3 && std::string(argv[1]) == "--bench") {