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
+73 -30
View File
@@ -87,6 +87,31 @@ namespace {
}
namespace ANSCENTER {
std::string ANSFaceRecognizer::ResolveRecognizerModelFile() {
const std::string adaPath = CreateFilePath(_modelFolder, "adafacerecognizer.onnx");
const std::string arcPath = CreateFilePath(_modelFolder, "ansfacerecognizer.onnx");
if (_preferredModel == FaceRecognizerModel::ADAFACE) {
if (std::filesystem::exists(adaPath)) {
_activeModel = FaceRecognizerModel::ADAFACE;
m_swapRB = false; // AdaFace consumes BGR
ANS_DBG("FaceRecognizer", "Recognizer model resolved: ADAFACE (%s)", adaPath.c_str());
return adaPath;
}
_logger.LogWarn("ANSFaceRecognizer::ResolveRecognizerModelFile",
"adafacerecognizer.onnx not found in model pack — falling back to ArcFace",
__FILE__, __LINE__);
}
_activeModel = FaceRecognizerModel::ARCFACE;
m_swapRB = true; // ArcFace consumes RGB
if (std::filesystem::exists(arcPath)) {
ANS_DBG("FaceRecognizer", "Recognizer model resolved: ARCFACE (%s)", arcPath.c_str());
return arcPath;
}
return std::string();
}
std::string ANSFaceRecognizer::GetOpenVINODevice() {
ov::Core core;
std::vector<std::string> available_devices = core.get_available_devices();
@@ -146,16 +171,16 @@ namespace ANSCENTER {
static_cast<int>(EngineType::AMD_GPU),
static_cast<int>(EngineType::CPU));
if (engineType == EngineType::NVIDIA_GPU) {
// 1. Load ONNX model
std::string onnxfile = CreateFilePath(_modelFolder, "ansfacerecognizer.onnx");
if (std::filesystem::exists(onnxfile)) {
// 1. Load ONNX model (adaface by default, arcface fallback)
std::string onnxfile = ResolveRecognizerModelFile();
if (!onnxfile.empty()) {
_modelFilePath = onnxfile;
_logger.LogDebug("ANSFaceRecognizer::Initialize. Loading ansfacerecognizer weight",
_logger.LogDebug("ANSFaceRecognizer::Initialize. Loading face recognizer weight",
_modelFilePath, __FILE__, __LINE__);
}
else {
_logger.LogError("ANSFaceRecognizer::Initialize. Model ansfacerecognizer.onnx does not exist",
_modelFilePath, __FILE__, __LINE__);
_logger.LogError("ANSFaceRecognizer::Initialize. No recognizer model (adafacerecognizer.onnx / ansfacerecognizer.onnx) exists",
_modelFolder, __FILE__, __LINE__);
return false;
}
@@ -213,24 +238,28 @@ namespace ANSCENTER {
}
else {
#ifdef USE_ONNX_ENGINE
std::string faceidModel = CreateFilePath(_modelFolder, "ansfacerecognizer.onnx");
if (std::filesystem::exists(faceidModel)) {
std::string faceidModel = ResolveRecognizerModelFile();
if (!faceidModel.empty()) {
_modelFilePath = faceidModel;
_logger.LogDebug("ANSFaceRecognizer::Initialize. Loading ANSFaceRecognizer weight",
_modelFilePath, __FILE__, __LINE__);
}
else {
_logger.LogError("ANSFaceRecognizer::Initialize. Model ansfacerecognizer.onnx does not exist",
_modelFilePath, __FILE__, __LINE__);
_logger.LogError("ANSFaceRecognizer::Initialize. No recognizer model (adafacerecognizer.onnx / ansfacerecognizer.onnx) exists",
_modelFolder, __FILE__, __LINE__);
return false;
}
ANS_DBG("FaceRecognizer", "Creating GlintArcFace with engineType=%d model=%s",
ANS_DBG("FaceRecognizer", "Creating %s with engineType=%d model=%s",
FaceRecognizerModelName(_activeModel),
static_cast<int>(engineType), faceidModel.c_str());
faceRecognizer = std::make_unique<GlintArcFace>(faceidModel, engineType);
if (_activeModel == FaceRecognizerModel::ADAFACE)
faceRecognizer = std::make_unique<AdaFace>(faceidModel, engineType);
else
faceRecognizer = std::make_unique<GlintArcFace>(faceidModel, engineType);
if (!faceRecognizer) {
_logger.LogFatal("ANSFaceRecognizer::Initialize",
"Failed to initialize ONNX face recognizer", __FILE__, __LINE__);
ANS_DBG("FaceRecognizer", "FAILED: GlintArcFace returned null");
ANS_DBG("FaceRecognizer", "FAILED: ONNX face recognizer returned null");
return false;
}
#else
@@ -279,29 +308,29 @@ namespace ANSCENTER {
if (!result) return false;
if (engineType == EngineType::NVIDIA_GPU) {
std::string onnxfile = CreateFilePath(_modelFolder, "ansfacerecognizer.onnx");
if (std::filesystem::exists(onnxfile)) {
std::string onnxfile = ResolveRecognizerModelFile();
if (!onnxfile.empty()) {
_modelFilePath = onnxfile;
_logger.LogDebug("ANSFaceRecognizer::LoadModel. Loading ansfacerecognizer weight",
_logger.LogDebug("ANSFaceRecognizer::LoadModel. Loading face recognizer weight",
_modelFilePath, __FILE__, __LINE__);
}
else {
_logger.LogError("ANSFaceRecognizer::LoadModel. ansfacerecognizer.onnx not found",
_modelFilePath, __FILE__, __LINE__);
_logger.LogError("ANSFaceRecognizer::LoadModel. No recognizer model (adafacerecognizer.onnx / ansfacerecognizer.onnx) found",
_modelFolder, __FILE__, __LINE__);
return false;
}
}
else {
std::string faceidModel = CreateFilePath(_modelFolder, "ansfacerecognizer.onnx");
std::string faceidModel = ResolveRecognizerModelFile();
#ifdef USE_ONNX_ENGINE
if (std::filesystem::exists(faceidModel)) {
if (!faceidModel.empty()) {
_modelFilePath = faceidModel;
_logger.LogDebug("ANSFaceRecognizer::LoadModel. Loading ansfacerecognizer weight",
_logger.LogDebug("ANSFaceRecognizer::LoadModel. Loading face recognizer weight",
_modelFilePath, __FILE__, __LINE__);
}
else {
_logger.LogError("ANSFaceRecognizer::LoadModel. ansfacerecognizer.onnx not found",
_modelFilePath, __FILE__, __LINE__);
_logger.LogError("ANSFaceRecognizer::LoadModel. No recognizer model (adafacerecognizer.onnx / ansfacerecognizer.onnx) found",
_modelFolder, __FILE__, __LINE__);
return false;
}
#else
@@ -834,7 +863,7 @@ namespace ANSCENTER {
}
try {
// CPU preprocessing: resize + BGR→RGB before GPU upload
// CPU preprocessing: resize + channel order before GPU upload
// Reduces PCIe transfer and eliminates GPU cvtColor/resize overhead
cv::Mat srcImg = inputImage;
if (srcImg.channels() == 1) {
@@ -846,8 +875,13 @@ namespace ANSCENTER {
} else {
cpuResized = srcImg;
}
// ArcFace needs RGB; AdaFace consumes the Mat's native BGR.
cv::Mat cpuRGB;
cv::cvtColor(cpuResized, cpuRGB, cv::COLOR_BGR2RGB);
if (m_swapRB) {
cv::cvtColor(cpuResized, cpuRGB, cv::COLOR_BGR2RGB);
} else {
cpuRGB = cpuResized;
}
m_gpuRgb.upload(cpuRGB, *m_gpuStream);
m_gpuStream->waitForCompletion();
@@ -963,14 +997,19 @@ namespace ANSCENTER {
cv::cuda::resize(d_img, d_resized, targetSize, 0, 0, cv::INTER_LINEAR, *m_gpuStream);
d_img = d_resized;
}
cv::cuda::GpuMat d_rgb;
cv::cuda::cvtColor(d_img, d_rgb, cv::COLOR_BGR2RGB, 0, *m_gpuStream);
batchGpu.emplace_back(std::move(d_rgb));
// ArcFace needs RGB; AdaFace consumes native BGR.
if (m_swapRB) {
cv::cuda::GpuMat d_rgb;
cv::cuda::cvtColor(d_img, d_rgb, cv::COLOR_BGR2RGB, 0, *m_gpuStream);
batchGpu.emplace_back(std::move(d_rgb));
} else {
batchGpu.emplace_back(std::move(d_img));
}
} else {
const auto& roi = faceROIs[i];
if (roi.empty()) continue;
// CPU preprocessing: resize + BGR→RGB before upload
// CPU preprocessing: resize + channel order before upload
cv::Mat srcImg = roi;
if (srcImg.channels() == 1) {
cv::cvtColor(srcImg, srcImg, cv::COLOR_GRAY2BGR);
@@ -982,7 +1021,11 @@ namespace ANSCENTER {
cpuResized = srcImg;
}
cv::Mat cpuRGB;
cv::cvtColor(cpuResized, cpuRGB, cv::COLOR_BGR2RGB);
if (m_swapRB) {
cv::cvtColor(cpuResized, cpuRGB, cv::COLOR_BGR2RGB);
} else {
cpuRGB = cpuResized;
}
cv::cuda::GpuMat d_rgb;
d_rgb.upload(cpuRGB, *m_gpuStream);
batchGpu.emplace_back(std::move(d_rgb));