ANSFR: support ANS_FaceRecognizer v1.4 models (AdaFace + ONNX attributes)

- Recognizer: adafacerecognizer.onnx (AdaFace, new default) with automatic
  fallback to ansfacerecognizer.onnx (ArcFace) for pre-v1.4 zips. AdaFace
  consumes BGR: new AdaFace handler in ONNXEngine skips the BGR2RGB swap,
  and the TensorRT paths gate their cvtColor on m_swapRB.
- New C API: SetANSRFRecognizerModel (0=AdaFace, 1=ArcFace),
  GetANSRFRecognizerModel, GetANSRFModelInfo. A live switch reloads the
  recognizer and regenerates all stored embeddings from the enrolled face
  crops; the generating model is persisted in a new SETTINGS table
  (EMBEDDING_MODEL) so upgrading an ArcFace database to the AdaFace
  default migrates automatically on LoadEngine.
- Attributes: prefer agegender.onnx (ViT: gender_logits [male,female] +
  age_pred in years; also handles the single-tensor research export) and
  emotionsrecognition.onnx (YOLOv11-cls, classes from .names) behind new
  IAgeGenderEstimator/IEmotionsClassifier interfaces, falling back to the
  legacy OpenVINO IRs. EP follows hardware: NVIDIA uses TensorRT EP with
  disk-cached engines, AMD DirectML, Intel OpenVINO.
- Emotion JSON mapping: handle the new Fear class and the never-matched
  surprise label (both map to Attentive).
- ANSFR-UnitTest: TestModelSwitchV14 covers default model selection, ONNX
  attribute backends, enrollment, and adaface<->arcface switching.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-26 11:06:10 +10:00
co-authored by Claude Fable 5
parent 881d783fa3
commit ac4369302a
15 changed files with 1111 additions and 51 deletions
+79
View File
@@ -265,6 +265,85 @@ namespace ANSCENTER {
}
}
int FaceDatabase::GetAllFaces(std::vector<FaceRecord>& faces) {
std::lock_guard<std::mutex> lock(_mutex);
faces.clear();
sqlite3_stmt* stmt = nullptr;
int rc = sqlite3_prepare_v2(m_db, "SELECT ID, USER, IMG_PATH FROM FACE;", -1, &stmt, NULL);
if (rc != SQLITE_OK) {
_logger.LogError("FaceDatabase::GetAllFaces", sqlite3_errmsg(m_db), __FILE__, __LINE__);
return -1;
}
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
FaceRecord record;
record.FaceId = sqlite3_column_int(stmt, 0);
record.UserId = sqlite3_column_int(stmt, 1);
const unsigned char* path = sqlite3_column_text(stmt, 2);
record.ImagePath = path ? reinterpret_cast<const char*>(path) : "";
faces.push_back(std::move(record));
}
sqlite3_finalize(stmt);
return 0;
}
int FaceDatabase::UpdateFaceEmbedding(int faceId, const float embedding[]) {
std::lock_guard<std::mutex> lock(_mutex);
sqlite3_stmt* stmt = nullptr;
int rc = sqlite3_prepare_v2(m_db, "UPDATE FACE SET EMBEDDING=? WHERE ID=?;", -1, &stmt, NULL);
if (rc != SQLITE_OK) {
_logger.LogError("FaceDatabase::UpdateFaceEmbedding", sqlite3_errmsg(m_db), __FILE__, __LINE__);
return -1;
}
int resultCode = 1;
if (sqlite3_bind_blob(stmt, 1, embedding, sizeof(float) * m_embedding_dim, SQLITE_STATIC) != SQLITE_OK)
resultCode = -2;
if (sqlite3_bind_int(stmt, 2, faceId) != SQLITE_OK)
resultCode = -2;
rc = sqlite3_step(stmt);
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
_logger.LogError("FaceDatabase::UpdateFaceEmbedding", sqlite3_errmsg(m_db), __FILE__, __LINE__);
resultCode = -3;
}
sqlite3_finalize(stmt);
return resultCode;
}
bool FaceDatabase::SetSetting(const std::string& key, const std::string& value) {
std::lock_guard<std::mutex> lock(_mutex);
// Older databases predate the SETTINGS table — create on demand.
sqlite3_exec(m_db,
"CREATE TABLE IF NOT EXISTS SETTINGS (KEY TEXT PRIMARY KEY, VALUE TEXT);",
nullptr, nullptr, nullptr);
sqlite3_stmt* stmt = nullptr;
int rc = sqlite3_prepare_v2(m_db,
"INSERT INTO SETTINGS (KEY, VALUE) VALUES (?, ?) "
"ON CONFLICT(KEY) DO UPDATE SET VALUE=excluded.VALUE;",
-1, &stmt, NULL);
if (rc != SQLITE_OK) {
_logger.LogError("FaceDatabase::SetSetting", sqlite3_errmsg(m_db), __FILE__, __LINE__);
return false;
}
sqlite3_bind_text(stmt, 1, key.c_str(), int(key.length()), SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, value.c_str(), int(value.length()), SQLITE_STATIC);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return (rc == SQLITE_ROW || rc == SQLITE_DONE);
}
std::string FaceDatabase::GetSetting(const std::string& key) {
std::lock_guard<std::mutex> lock(_mutex);
sqlite3_stmt* stmt = nullptr;
int rc = sqlite3_prepare_v2(m_db, "SELECT VALUE FROM SETTINGS WHERE KEY=?;", -1, &stmt, NULL);
if (rc != SQLITE_OK) {
// Missing table (legacy database) — treat as "no value".
return std::string();
}
std::string value;
sqlite3_bind_text(stmt, 1, key.c_str(), int(key.length()), SQLITE_STATIC);
if (sqlite3_step(stmt) == SQLITE_ROW) {
const unsigned char* v = sqlite3_column_text(stmt, 0);
if (v) value = reinterpret_cast<const char*>(v);
}
sqlite3_finalize(stmt);
return value;
}
int FaceDatabase::DeleteFace(int id) {
try {
// Fix #4: Single lock scope — GetFaceById + DELETE in one atomic section