ANSFR: persist explicit recognizer-model choice across restarts

SetANSRFRecognizerModel now records the choice in the face database
(SETTINGS key RECOGNIZER_MODEL_PREF), and LoadEngine restores it when no
explicit choice was made in the current session - so a runtime switch to
ArcFace (or AdaFace) survives server restarts without re-calling the API.

Kept separate from EMBEDDING_MODEL on purpose: only an actual API call is
recorded as a preference. An automatic ArcFace fallback (pre-v1.4 zip
without adafacerecognizer.onnx) is not persisted, so upgrading such a
deployment to the v1.4 pack still gets the AdaFace default.

ANSFR-UnitTest: TestModelSwitchV14 (now also runnable via --test) gains a
simulated restart - release handle, recreate on the same database, and
assert the persisted ArcFace choice is restored and still recognizes the
enrolled user. Verified ALL PASSED.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-26 11:47:32 +10:00
co-authored by Claude Fable 5
parent 06829ae0e3
commit 13eacc4ab2
3 changed files with 56 additions and 2 deletions
+29
View File
@@ -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<std::mutex> 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<FaceRecognizerModel>(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<std::mutex> lock(_databaseMutex);
_db->SetSetting("RECOGNIZER_MODEL_PREF", FaceRecognizerModelName(requested));
}
// Engine not loaded yet — the preference is applied by LoadEngine.
if (!_recognizer) return true;