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
+281 -14
View File
@@ -4,6 +4,7 @@
#include "ANSOVFaceDetector.h"
#include "SCRFDFaceDetector.h"
#include "ANSFaceRecognizer.h"
#include "ONNXFaceAttributes.h"
#include <faiss/IndexIDMap.h>
#include <faiss/IndexFlat.h>
#include <boost/uuid/uuid.hpp>
@@ -463,6 +464,16 @@ namespace ANSCENTER {
attributeModelsSuccess = false;
}
// Embedding/model consistency: stored embeddings must come from
// the recognizer model that just loaded. A v1.4 upgrade flips the
// default from ArcFace to AdaFace, which silently invalidates a
// pre-existing database — regenerate before the index builds.
if (!SyncEmbeddingsWithActiveModel()) {
LogThreadSafe("ANSFacialRecognition::LoadEngine",
"Warning: embedding/model sync failed — recognition may return "
"Unknown until faces are re-enrolled");
}
// Reload database/embeddings
if (!Reload()) {
LogThreadSafe("ANSFacialRecognition::LoadEngine",
@@ -818,9 +829,12 @@ namespace ANSCENTER {
}
bool ANSFacialRecognition::InitializeRecognizer() {
try {
ANS_DBG("ANSFR", "InitializeRecognizer: starting, modelPath=%s", _recognizerFilePath.c_str());
ANS_DBG("ANSFR", "InitializeRecognizer: starting, modelPath=%s model=%s",
_recognizerFilePath.c_str(), FaceRecognizerModelName(_requestedRecModel));
// Create recognizer instance
_recognizer = std::make_unique<ANSFaceRecognizer>();
auto recognizer = std::make_unique<ANSFaceRecognizer>();
recognizer->SetPreferredModel(_requestedRecModel);
_recognizer = std::move(recognizer);
_recognizer->SetMaxSlotsPerGpu(m_maxSlotsPerGpu);
// Configure model
@@ -861,7 +875,13 @@ namespace ANSCENTER {
return false;
}
ANS_DBG("ANSFR", "InitializeRecognizer: SUCCESS");
// Record what actually loaded — ADAFACE silently falls back to
// ARCFACE when the model pack has no adafacerecognizer.onnx.
if (auto* concrete = dynamic_cast<ANSFaceRecognizer*>(_recognizer.get())) {
_activeRecModel = concrete->GetActiveModel();
}
ANS_DBG("ANSFR", "InitializeRecognizer: SUCCESS, active model=%s",
FaceRecognizerModelName(_activeRecModel));
return true;
}
catch (const std::exception& e) {
@@ -924,8 +944,50 @@ namespace ANSCENTER {
return false;
}
}
namespace {
// EP options for the ONNX attribute models. On NVIDIA the models are
// built into TensorRT engines through the TRT EP and cached on disk
// next to the model pack (first load pays the build, every later load
// is instant). AMD/Intel/CPU resolve to DirectML/OpenVINO/CPU inside
// BasicOrtHandler and ignore every TRT field.
OrtHandlerOptions BuildAttributeOrtOptions(EngineType engineType,
const std::string& modelFolder) {
OrtHandlerOptions opts;
if (engineType == EngineType::NVIDIA_GPU) {
opts.preferTensorRT = true;
opts.trtFP16 = true;
opts.trtEngineCacheDir = CreateFilePath(modelFolder, "TRTCache");
std::error_code ec;
std::filesystem::create_directories(opts.trtEngineCacheDir, ec);
}
return opts;
}
}
bool ANSFacialRecognition::InitializeAgeGenderModel(const std::string& deviceName) {
try {
// Prefer the ONNX model shipped since v1.4; fall back to the
// legacy OpenVINO IR when it is absent (or fails to load).
std::string onnxModelPath = CreateFilePath(_recognizerModelFolder, "agegender.onnx");
if (FileExist(onnxModelPath)) {
try {
auto detector = std::make_unique<OnnxAgeGenderEstimator>(
onnxModelPath, engineType,
BuildAttributeOrtOptions(engineType, _recognizerModelFolder));
std::lock_guard<std::mutex> lock(_ageGenderMutex);
_ageGenderDetector = std::move(detector);
LogThreadSafe("ANSFacialRecognition::InitializeAgeGenderModel",
"Loaded ONNX age-gender model: " + onnxModelPath, LogLevel::Info);
return true;
}
catch (const std::exception& e) {
LogThreadSafe("ANSFacialRecognition::InitializeAgeGenderModel",
"ONNX age-gender model failed (" + std::string(e.what()) +
") — falling back to OpenVINO model");
}
}
std::string ageGenderModelPath = CreateFilePath(_recognizerModelFolder, "agegender.xml");
if (!FileExist(ageGenderModelPath)) {
@@ -949,18 +1011,20 @@ namespace ANSCENTER {
_ageGenderDetector.reset();
// Create new detector
_ageGenderDetector = std::make_unique<AgeGenderDetection>(ageGenderModelPath, true);
auto detector = std::make_unique<AgeGenderDetection>(ageGenderModelPath, true);
// Compile model for target device
ov::CompiledModel compiledModel = core->compile_model(
_ageGenderDetector->read(*core),
detector->read(*core),
deviceName
);
// Create inference request
_ageGenderDetector->request = compiledModel.create_infer_request();
_ageGenderDetector->inTensor = _ageGenderDetector->request.get_input_tensor();
_ageGenderDetector->inTensor.set_shape(_ageGenderDetector->inShape);
detector->request = compiledModel.create_infer_request();
detector->inTensor = detector->request.get_input_tensor();
detector->inTensor.set_shape(detector->inShape);
_ageGenderDetector = std::move(detector);
}
return true;
@@ -979,6 +1043,30 @@ namespace ANSCENTER {
}
bool ANSFacialRecognition::InitializeEmotionModel(const std::string& deviceName) {
try {
// Prefer the YOLOv11 classifier shipped since v1.4 (class list in
// emotionsrecognition.names); fall back to the OpenVINO IR.
std::string onnxModelPath = CreateFilePath(_recognizerModelFolder, "emotionsrecognition.onnx");
if (FileExist(onnxModelPath)) {
try {
std::string namesPath = CreateFilePath(_recognizerModelFolder, "emotionsrecognition.names");
auto classNames = OnnxEmotionsClassifier::LoadClassNames(namesPath);
auto detector = std::make_unique<OnnxEmotionsClassifier>(
onnxModelPath, std::move(classNames), engineType,
BuildAttributeOrtOptions(engineType, _recognizerModelFolder));
std::lock_guard<std::mutex> lock(_emotionsMutex);
_emotionsDetector = std::move(detector);
LogThreadSafe("ANSFacialRecognition::InitializeEmotionModel",
"Loaded ONNX emotion model: " + onnxModelPath, LogLevel::Info);
return true;
}
catch (const std::exception& e) {
LogThreadSafe("ANSFacialRecognition::InitializeEmotionModel",
"ONNX emotion model failed (" + std::string(e.what()) +
") — falling back to OpenVINO model");
}
}
std::string emotionModelPath = CreateFilePath(_recognizerModelFolder, "emotionsrecognition.xml");
if (!FileExist(emotionModelPath)) {
@@ -1002,18 +1090,20 @@ namespace ANSCENTER {
_emotionsDetector.reset();
// Create new detector
_emotionsDetector = std::make_unique<EmotionsDetection>(emotionModelPath, true);
auto detector = std::make_unique<EmotionsDetection>(emotionModelPath, true);
// Compile model for target device
ov::CompiledModel compiledModel = core->compile_model(
_emotionsDetector->read(*core),
detector->read(*core),
deviceName
);
// Create inference request
_emotionsDetector->request = compiledModel.create_infer_request();
_emotionsDetector->inTensor = _emotionsDetector->request.get_input_tensor();
_emotionsDetector->inTensor.set_shape(_emotionsDetector->inShape);
detector->request = compiledModel.create_infer_request();
detector->inTensor = detector->request.get_input_tensor();
detector->inTensor.set_shape(detector->inShape);
_emotionsDetector = std::move(detector);
}
return true;
@@ -1084,7 +1174,184 @@ namespace ANSCENTER {
return false;
}
}
bool ANSFacialRecognition::SyncEmbeddingsWithActiveModel() {
try {
if (!_db || !_recognizer) return false;
const std::string activeName = FaceRecognizerModelName(_activeRecModel);
std::string storedName;
std::vector<FaceRecord> faces;
{
std::lock_guard<std::mutex> lock(_databaseMutex);
storedName = _db->GetSetting("EMBEDDING_MODEL");
if (_db->GetAllFaces(faces) != 0) return false;
}
if (storedName.empty()) {
// Databases created before v1.4 never recorded the model —
// every embedding in them came from ArcFace.
storedName = faces.empty() ? activeName : "arcface";
}
if (storedName != activeName && !faces.empty()) {
LogThreadSafe("ANSFacialRecognition::SyncEmbeddingsWithActiveModel",
"Stored embeddings were generated by '" + storedName +
"' but the active recognizer is '" + activeName +
"' — regenerating " + std::to_string(faces.size()) +
" embeddings from the enrolled face images", LogLevel::Info);
if (!RegenerateEmbeddings()) return false;
}
{
std::lock_guard<std::mutex> lock(_databaseMutex);
_db->SetSetting("EMBEDDING_MODEL", activeName);
}
return true;
}
catch (const std::exception& e) {
LogThreadSafe("ANSFacialRecognition::SyncEmbeddingsWithActiveModel", e.what());
return false;
}
}
bool ANSFacialRecognition::RegenerateEmbeddings() {
try {
if (!_db || !_recognizer || !_recognizer->IsInitialized()) {
LogThreadSafe("ANSFacialRecognition::RegenerateEmbeddings",
"Recognizer/database not ready");
return false;
}
std::vector<FaceRecord> faces;
{
std::lock_guard<std::mutex> lock(_databaseMutex);
if (_db->GetAllFaces(faces) != 0) return false;
}
if (faces.empty()) return true;
size_t updated = 0, failed = 0;
for (const auto& face : faces) {
// InsertFace stored the aligned face crop — compute the new
// embedding directly on it, no re-detection needed.
cv::Mat crop = cv::imread(face.ImagePath, cv::IMREAD_COLOR);
if (crop.empty()) {
LogThreadSafe("ANSFacialRecognition::RegenerateEmbeddings",
"Cannot read enrolled face image (face ID " +
std::to_string(face.FaceId) + "): " + face.ImagePath);
++failed;
continue;
}
Object faceObject;
faceObject.mask = crop;
std::vector<float> embedding;
{
std::lock_guard<std::mutex> lock(_recognitionMutex);
embedding = _recognizer->Feature(crop, faceObject);
}
if (embedding.size() != static_cast<size_t>(_config._recOutputDim)) {
LogThreadSafe("ANSFacialRecognition::RegenerateEmbeddings",
"Embedding failed for face ID " + std::to_string(face.FaceId));
++failed;
continue;
}
std::lock_guard<std::mutex> lock(_databaseMutex);
if (_db->UpdateFaceEmbedding(face.FaceId, embedding.data()) > 0) ++updated;
else ++failed;
}
LogThreadSafe("ANSFacialRecognition::RegenerateEmbeddings",
"Regenerated " + std::to_string(updated) + "/" +
std::to_string(faces.size()) + " embeddings (" +
std::to_string(failed) + " failed)", LogLevel::Info);
_reloadNeeded.store(true);
return updated > 0 || failed == 0;
}
catch (const std::exception& e) {
LogThreadSafe("ANSFacialRecognition::RegenerateEmbeddings", e.what());
return false;
}
}
bool ANSFacialRecognition::SetRecognizerModelType(int modelType) {
try {
if (modelType < 0 || modelType > 1) {
LogThreadSafe("ANSFacialRecognition::SetRecognizerModelType",
"Invalid model type: " + std::to_string(modelType) +
" (0 = AdaFace, 1 = ArcFace)");
return false;
}
const auto requested = static_cast<FaceRecognizerModel>(modelType);
_requestedRecModel = requested;
// Engine not loaded yet — the preference is applied by LoadEngine.
if (!_recognizer) return true;
if (requested == _activeRecModel) return true;
LogThreadSafe("ANSFacialRecognition::SetRecognizerModelType",
"Switching recognizer model to '" +
std::string(FaceRecognizerModelName(requested)) + "'", LogLevel::Info);
if (!InitializeRecognizer()) {
LogThreadSafe("ANSFacialRecognition::SetRecognizerModelType",
"Failed to load recognizer for model '" +
std::string(FaceRecognizerModelName(requested)) + "'");
return false;
}
_recognizerModelFolder = _recognizer->GetModelFolder();
if (!SyncEmbeddingsWithActiveModel()) {
LogThreadSafe("ANSFacialRecognition::SetRecognizerModelType",
"Warning: embedding regeneration failed after model switch");
}
_reloadNeeded.store(true);
return Reload();
}
catch (const std::exception& e) {
LogThreadSafe("ANSFacialRecognition::SetRecognizerModelType", e.what());
return false;
}
}
int ANSFacialRecognition::GetRecognizerModelType() const {
return static_cast<int>(_recognizer ? _activeRecModel : _requestedRecModel);
}
std::string ANSFacialRecognition::GetActiveModelInfo() {
try {
const char* agegenderBackend = "disabled";
const char* emotionBackend = "disabled";
{
std::lock_guard<std::mutex> lock(_ageGenderMutex);
if (_ageGenderDetector && _ageGenderDetector->enabled())
agegenderBackend = _ageGenderDetector->backendName();
}
{
std::lock_guard<std::mutex> lock(_emotionsMutex);
if (_emotionsDetector && _emotionsDetector->enabled())
emotionBackend = _emotionsDetector->backendName();
}
nlohmann::json info;
info["recognizer_model"] = FaceRecognizerModelName(
_recognizer ? _activeRecModel : _requestedRecModel);
info["recognizer_loaded"] = (_recognizer != nullptr);
info["agegender_backend"] = agegenderBackend;
info["emotion_backend"] = emotionBackend;
return info.dump();
}
catch (const std::exception& e) {
LogThreadSafe("ANSFacialRecognition::GetActiveModelInfo", e.what());
return "{}";
}
}
bool ANSFacialRecognition::UpdateParameters(
float knownPersonThreshold,
bool enableAgeGender,