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
+114 -1
View File
@@ -19,6 +19,7 @@
#include <chrono>
#include <thread>
#include <iomanip>
#include <filesystem>
#include "ANSFR.h"
#include "fastdeploy/vision.h"
#include "ANSFilePlayer.h"
@@ -1713,6 +1714,117 @@ int SCRFDVideoRepro()
return 0;
}
// ===========================================================================
// ANS_FaceRecognizer v1.4 test:
// - AdaFace loads by default from the v1.4 model pack
// - ONNX age-gender + emotion models load (TensorRT EP on NVIDIA)
// - SetANSRFRecognizerModel switches adaface <-> arcface live, and stored
// embeddings are regenerated so recognition keeps working after a switch
// ===========================================================================
static bool V14Check(bool condition, const std::string& label, int& failures)
{
std::cout << (condition ? "[PASS] " : "[FAIL] ") << label << std::endl;
if (!condition) ++failures;
return condition;
}
int TestModelSwitchV14()
{
const std::string modelZip = "C:\\Programs\\Models\\ANS_FaceRecognizer_v1.4.zip";
const std::string detectorZip = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFDET\\ANS_GenericFD(GPU)_v1.0.zip";
const std::string dbFolder = "C:\\Programs\\DemoAssets\\ANSFRDB\\v14_test";
const std::string dbFile = dbFolder + "\\ANSFR_v14_test.db";
const std::string enrollImage = "C:\\Programs\\DemoAssets\\TestImages\\FacialRecognition\\Obama+Oprah1.jpg";
const std::string userName = "V14TestUser";
int failures = 0;
std::cout << "\n===== TestModelSwitchV14 =====" << std::endl;
// Fresh database each run so the test is repeatable
std::error_code ec;
std::filesystem::remove_all(dbFolder, ec);
cv::Mat frame = cv::imread(enrollImage, cv::IMREAD_COLOR);
if (!V14Check(!frame.empty(), "test image readable: " + enrollImage, failures))
return failures;
ANSCENTER::ANSFacialRecognition* h = nullptr;
int cr = CreateANSRFHandle(&h, "", "", dbFile.c_str(), modelZip.c_str(), detectorZip.c_str(),
0, 0.35f, 1, 1, 1, 30, 0.5f, 0, 0);
if (!V14Check(cr > 0 && h != nullptr, "CreateANSRFHandle", failures))
return failures;
int le = LoadANSRFEngine(&h);
if (!V14Check(le == 1, "LoadANSRFEngine (v1.4 model pack)", failures)) {
ReleaseANSRFHandle(&h);
return failures;
}
// --- default model + attribute backends -------------------------------
std::string info = h->GetActiveModelInfo();
std::cout << "Model info: " << info << std::endl;
V14Check(GetANSRFRecognizerModel(&h) == 0, "default recognizer model is AdaFace (0)", failures);
V14Check(info.find("\"recognizer_model\":\"adaface\"") != std::string::npos,
"model info reports adaface", failures);
V14Check(info.find("\"agegender_backend\":\"onnx\"") != std::string::npos,
"age-gender uses agegender.onnx backend", failures);
V14Check(info.find("\"emotion_backend\":\"onnx\"") != std::string::npos,
"emotions use emotionsrecognition.onnx backend", failures);
// --- enroll one user ---------------------------------------------------
int userId = InsertUser(&h, "0001", userName.c_str());
V14Check(userId > 0, "InsertUser", failures);
unsigned int bufLen = 0;
unsigned char* buf = ANSCENTER::ANSFRHelper::CVMatToBytes(frame, bufLen);
int faceInserted = InsertFaceBinary(&h, userId, buf, frame.cols, frame.rows);
delete buf;
V14Check(faceInserted > 0, "InsertFaceBinary (enrollment)", failures);
Reload(&h);
auto recognizesUser = [&](const char* label) {
// Full pipeline (detect + match). The Recognize API needs a config
// file with _recInputShape, which this test intentionally omits.
unsigned int len = 0;
unsigned char* bytes = ANSCENTER::ANSFRHelper::CVMatToBytes(frame, len);
std::string res = RunANSRFInferenceBinary(&h, bytes, frame.cols, frame.rows);
delete bytes;
std::cout << " recognition result: " << res << std::endl;
V14Check(res.find(userName) != std::string::npos, label, failures);
};
recognizesUser("recognized with AdaFace embeddings");
// --- live switch to ArcFace (regenerates stored embeddings) ------------
V14Check(SetANSRFRecognizerModel(&h, 1) == 1, "switch to ArcFace", failures);
V14Check(GetANSRFRecognizerModel(&h) == 1, "active model is ArcFace (1)", failures);
std::cout << "Model info: " << h->GetActiveModelInfo() << std::endl;
recognizesUser("recognized after switch to ArcFace");
// --- and back to AdaFace ------------------------------------------------
V14Check(SetANSRFRecognizerModel(&h, 0) == 1, "switch back to AdaFace", failures);
V14Check(GetANSRFRecognizerModel(&h) == 0, "active model is AdaFace (0)", failures);
recognizesUser("recognized after switch back to AdaFace");
// --- attributes through the full pipeline (ONNX age/gender + emotions) --
{
unsigned int len = 0;
unsigned char* bytes = ANSCENTER::ANSFRHelper::CVMatToBytes(frame, len);
std::string res = RunANSRFInferenceBinary(&h, bytes, frame.cols, frame.rows);
delete bytes;
std::cout << " inference (attributes): " << res << std::endl;
V14Check(res.find("Age") != std::string::npos || res.find("results") != std::string::npos,
"inference with ONNX attribute models returns results", failures);
}
ReleaseANSRFHandle(&h);
std::cout << "===== TestModelSwitchV14: "
<< (failures == 0 ? "ALL PASSED" : (std::to_string(failures) + " FAILURE(S)"))
<< " =====" << std::endl;
return failures;
}
int main(int argc, char** argv)
{
// ── GPU visibility bisect (Blackwell Myelin l2cm debugging) ─────────────
@@ -1735,7 +1847,8 @@ int main(int argc, char** argv)
//FaceDetectorTest();
//TestFaceRecognition();
//TestCompleteFR1();
ANSVISTest();
TestModelSwitchV14();
//ANSVISTest();
//ANSVISImageTest();
//ANSVISTest();
//ANSVISTestFilePlayer();