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;
+9 -2
View File
@@ -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