ANSFR: name the real cause when enrollment cannot compute embeddings
Enrolling a face while the recognizer engine is down used to store the user row and silently drop the embedding: InsertUser is a pure DB write, Feature() returns empty when uninitialized, and InsertFace's size check misreported that as "Invalid embedding generated" — blaming the face. The result surfaces hours later as every match returning Unknown/2.0 with nothing in the log saying why (seen on the A400: 45 users, zero embeddings). - ANSFRBase gains a public IsInitialized() accessor (flag existed but was protected). - InsertFace / InsertMultipleFaces gate on it before computing the embedding and log the true cause: recognizer not initialized, face NOT enrolled, fix engine errors then re-enroll. - The residual invalid-embedding message now reports actual vs expected size and the remaining causes (unusable crop / recognizer reloading). - Reload() warns when the database has users but zero embeddings — the one line that identifies stale half-enrolled databases at startup. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+42
-3
@@ -634,6 +634,19 @@ namespace ANSCENTER {
|
||||
"Built new index with " + std::to_string(newFaissIndex->ntotal) +
|
||||
" embeddings, " + std::to_string(newUserDict.size()) + " users", LogLevel::Info);
|
||||
|
||||
// Users without a single embedding cannot ever match — every face
|
||||
// comes back Unknown/2.0 and nothing else in the log says why. The
|
||||
// usual cause: faces were enrolled while the recognizer engine
|
||||
// could not load (InsertFace stores the user but no embedding).
|
||||
if (newFaissIndex->ntotal == 0 && !newUserDict.empty()) {
|
||||
LogThreadSafe("ANSFacialRecognition::Reload",
|
||||
"Face database has " + std::to_string(newUserDict.size()) +
|
||||
" users but ZERO embeddings — all matches will return Unknown. "
|
||||
"Faces were likely enrolled while the recognizer engine was "
|
||||
"failing to load (check earlier engine errors). Re-enroll the "
|
||||
"users' faces once the engine loads.", LogLevel::Warn);
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// PHASE 2: Atomic swap (brief exclusive lock ~microseconds)
|
||||
// Inference is blocked only during this swap.
|
||||
@@ -1353,6 +1366,17 @@ namespace ANSCENTER {
|
||||
// Get largest face (CPU operation)
|
||||
Object faceObject = GetLargestObject(detectedFaces);
|
||||
|
||||
// The recognizer must be up before we can compute an embedding.
|
||||
// Feature() returns empty when uninitialized, which the size check
|
||||
// below would misreport as a bad face — name the real cause here.
|
||||
if (!_recognizer || !_recognizer->IsInitialized()) {
|
||||
LogThreadSafe("ANSFacialRecognition::InsertFace",
|
||||
"Cannot enroll face for user ID " + std::to_string(userId) +
|
||||
": recognizer is NOT initialized (engine failed to load?). "
|
||||
"No embedding stored — fix the engine errors, then re-enroll.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// LOCK ONLY DURING RECOGNITION (shared recognizer)
|
||||
std::vector<float> embedding;
|
||||
{
|
||||
@@ -1363,8 +1387,10 @@ namespace ANSCENTER {
|
||||
// Validate embedding
|
||||
if (embedding.empty() || embedding.size() != _config._recOutputDim) {
|
||||
LogThreadSafe("ANSFacialRecognition::InsertFace",
|
||||
"Invalid embedding generated for user ID " + std::to_string(userId) +
|
||||
" (size: " + std::to_string(embedding.size()) + ")");
|
||||
"Invalid embedding for user ID " + std::to_string(userId) +
|
||||
" (size: " + std::to_string(embedding.size()) + ", expected " +
|
||||
std::to_string(_config._recOutputDim) + ") — face crop unusable "
|
||||
"or recognizer busy/reloading. Face NOT enrolled.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1461,6 +1487,16 @@ namespace ANSCENTER {
|
||||
// Get largest face
|
||||
Object faceObject = GetLargestObject(detectedFaces);
|
||||
|
||||
// Same readiness gate as InsertFace — name the real cause instead
|
||||
// of letting the size check blame the face crop.
|
||||
if (!_recognizer || !_recognizer->IsInitialized()) {
|
||||
LogThreadSafe("ANSFacialRecognition::InsertMultipleFaces",
|
||||
"Cannot enroll face for user ID " + std::to_string(userId) +
|
||||
": recognizer is NOT initialized (engine failed to load?). "
|
||||
"No embedding stored — fix the engine errors, then re-enroll.");
|
||||
return faceIds;
|
||||
}
|
||||
|
||||
// LOCK ONLY DURING RECOGNITION (shared recognizer)
|
||||
std::vector<float> embedding;
|
||||
{
|
||||
@@ -1470,7 +1506,10 @@ namespace ANSCENTER {
|
||||
|
||||
if (embedding.empty() || embedding.size() != _config._recOutputDim) {
|
||||
LogThreadSafe("ANSFacialRecognition::InsertMultipleFaces",
|
||||
"Invalid embedding generated for user ID " + std::to_string(userId));
|
||||
"Invalid embedding for user ID " + std::to_string(userId) +
|
||||
" (size: " + std::to_string(embedding.size()) + ", expected " +
|
||||
std::to_string(_config._recOutputDim) + ") — face crop unusable "
|
||||
"or recognizer busy/reloading. Face NOT enrolled.");
|
||||
return faceIds;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user