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:
@@ -375,6 +375,27 @@ namespace ANSCENTER {
|
|||||||
bool ANSFacialRecognition::LoadEngine() {
|
bool ANSFacialRecognition::LoadEngine() {
|
||||||
try {
|
try {
|
||||||
ANS_DBG("ANSFR", "LoadEngine: starting");
|
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)
|
// Unload existing engine (handles its own locking)
|
||||||
UnloadEngine();
|
UnloadEngine();
|
||||||
|
|
||||||
@@ -1287,6 +1308,14 @@ namespace ANSCENTER {
|
|||||||
}
|
}
|
||||||
const auto requested = static_cast<FaceRecognizerModel>(modelType);
|
const auto requested = static_cast<FaceRecognizerModel>(modelType);
|
||||||
_requestedRecModel = requested;
|
_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.
|
// Engine not loaded yet — the preference is applied by LoadEngine.
|
||||||
if (!_recognizer) return true;
|
if (!_recognizer) return true;
|
||||||
|
|||||||
@@ -237,6 +237,11 @@ namespace ANSCENTER
|
|||||||
// and what actually loaded (fallback may downgrade ADAFACE->ARCFACE).
|
// and what actually loaded (fallback may downgrade ADAFACE->ARCFACE).
|
||||||
FaceRecognizerModel _requestedRecModel{ FaceRecognizerModel::ADAFACE };
|
FaceRecognizerModel _requestedRecModel{ FaceRecognizerModel::ADAFACE };
|
||||||
FaceRecognizerModel _activeRecModel{ 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 QUEUE_SIZE = 10;
|
||||||
const size_t FACE_THRESHOLD_SIZE = 5;
|
const size_t FACE_THRESHOLD_SIZE = 5;
|
||||||
// Cached attribute JSON per tracked face (everything except head pose).
|
// 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
|
// after it to switch live — a live switch reloads the recognizer engine and
|
||||||
// regenerates every stored face embedding from the enrolled face images
|
// regenerates every stored face embedding from the enrolled face images
|
||||||
// (embeddings produced by different models are not comparable), so it can
|
// (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
|
// take a while on large databases. The explicit choice is persisted in the
|
||||||
// invalid arguments.
|
// 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);
|
extern "C" ANSFR_API int SetANSRFRecognizerModel(ANSCENTER::ANSFacialRecognition** Handle, int modelType);
|
||||||
// Returns the ACTIVE model (0 = AdaFace, 1 = ArcFace) or -1 on error. May
|
// Returns the ACTIVE model (0 = AdaFace, 1 = ArcFace) or -1 on error. May
|
||||||
// differ from what was requested when the model pack has no
|
// differ from what was requested when the model pack has no
|
||||||
|
|||||||
@@ -1802,6 +1802,19 @@ int TestModelSwitchV14()
|
|||||||
std::cout << "Model info: " << h->GetActiveModelInfo() << std::endl;
|
std::cout << "Model info: " << h->GetActiveModelInfo() << std::endl;
|
||||||
recognizesUser("recognized after switch to ArcFace");
|
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 ------------------------------------------------
|
// --- and back to AdaFace ------------------------------------------------
|
||||||
V14Check(SetANSRFRecognizerModel(&h, 0) == 1, "switch back to AdaFace", failures);
|
V14Check(SetANSRFRecognizerModel(&h, 0) == 1, "switch back to AdaFace", failures);
|
||||||
V14Check(GetANSRFRecognizerModel(&h) == 0, "active model is AdaFace (0)", failures);
|
V14Check(GetANSRFRecognizerModel(&h) == 0, "active model is AdaFace (0)", failures);
|
||||||
@@ -2206,6 +2219,11 @@ int main(int argc, char** argv)
|
|||||||
return TestOutputFormatParity();
|
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
|
// 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
|
||||||
if (argc >= 3 && std::string(argv[1]) == "--bench") {
|
if (argc >= 3 && std::string(argv[1]) == "--bench") {
|
||||||
|
|||||||
Reference in New Issue
Block a user