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
+39 -2
View File
@@ -97,6 +97,17 @@ namespace ANSCENTER
bool UpdateFaceQueue(int queueSize, int faceThresholdSize, bool enableFaceQueue);
bool GetFaceQueue(int& queueSize, int& faceThresholdSize, bool& enableFaceQueue);
// Recognizer model switching (0 = AdaFace [default], 1 = ArcFace).
// Before LoadEngine: just records the preference. After LoadEngine:
// re-initializes the recognizer, regenerates every stored embedding
// from the enrolled face crops (embeddings from different models are
// not comparable), and rebuilds the FAISS index.
bool SetRecognizerModelType(int modelType);
int GetRecognizerModelType() const;
// JSON summary of what is actually loaded: recognizer model plus the
// backend ("onnx" | "openvino" | "disabled") serving each attribute.
[[nodiscard]] std::string GetActiveModelInfo();
[[nodiscard]] std::vector<FaceResultObject> Recognize(const cv::Mat& frame); // Expect to return on 1 object
[[nodiscard]] std::vector<FaceResultObject> Inference(const cv::Mat& input);
[[nodiscard]] std::vector<FaceResultObject> Detect(const cv::Mat& input);
@@ -141,6 +152,11 @@ namespace ANSCENTER
bool InitializeAgeGenderModel(const std::string& deviceName);
bool InitializeEmotionModel(const std::string& deviceName);
bool InitializeHeadPoseModel(const std::string& deviceName);
// Regenerates stored embeddings when they were produced by a
// different recognizer model than the one currently loaded
// (tracked via the EMBEDDING_MODEL key in the face database).
bool SyncEmbeddingsWithActiveModel();
bool RegenerateEmbeddings();
void ensureUniqueUserIdWithHighestConfidence(std::vector<FaceResultObject>& resultObjects, CameraData* camData = nullptr);
Object GetLargestObject(const std::vector<Object>& objects);
bool AreFacesSimilar(const FaceResultObject& face1, const FaceResultObject& face2);
@@ -214,9 +230,13 @@ namespace ANSCENTER
std::unique_ptr<ANSFRBase>_recognizer = nullptr;
std::unique_ptr<ANSFDBase>_detector = nullptr;
std::unique_ptr<AntispoofingClassifier>_antiSpoofDetector = nullptr;
std::unique_ptr<AgeGenderDetection>_ageGenderDetector = nullptr;
std::unique_ptr<IAgeGenderEstimator>_ageGenderDetector = nullptr;
std::unique_ptr<HeadPoseDetection>_headPoseDetector = nullptr;
std::unique_ptr<EmotionsDetection>_emotionsDetector = nullptr;
std::unique_ptr<IEmotionsClassifier>_emotionsDetector = nullptr;
// Recognizer model preference (settable before/after LoadEngine)
// and what actually loaded (fallback may downgrade ADAFACE->ARCFACE).
FaceRecognizerModel _requestedRecModel{ FaceRecognizerModel::ADAFACE };
FaceRecognizerModel _activeRecModel{ FaceRecognizerModel::ADAFACE };
const size_t QUEUE_SIZE = 10;
const size_t FACE_THRESHOLD_SIZE = 5;
// Cached attribute JSON per tracked face (everything except head pose).
@@ -348,6 +368,23 @@ extern "C" ANSFR_API int GetParamters(ANSCENTER::ANSFacialRecognition** Handl
extern "C" ANSFR_API int UpdateFaceQueue(ANSCENTER::ANSFacialRecognition** Handle, int queueSize, int numKnownFaceInQueue, int enableFaceQueue);
extern "C" ANSFR_API int GetFaceQueue(ANSCENTER::ANSFacialRecognition** Handle, LStrHandle faceQueue);
// Recognizer model switching (ANS_FaceRecognizer v1.4+).
// modelType: 0 = AdaFace (default), 1 = ArcFace.
// Call before LoadANSRFEngine to pick the model for the initial load, or
// after it to switch live — a live switch reloads the recognizer engine and
// regenerates every stored face embedding from the enrolled face images
// (embeddings produced by different models are not comparable), so it can
// take a while on large databases. Returns 1 on success, 0 on failure, -1 on
// invalid arguments.
extern "C" ANSFR_API int SetANSRFRecognizerModel(ANSCENTER::ANSFacialRecognition** Handle, int modelType);
// Returns the ACTIVE model (0 = AdaFace, 1 = ArcFace) or -1 on error. May
// differ from what was requested when the model pack has no
// adafacerecognizer.onnx (pre-v1.4 zips fall back to ArcFace).
extern "C" ANSFR_API int GetANSRFRecognizerModel(ANSCENTER::ANSFacialRecognition** Handle);
// JSON describing the loaded models, e.g.
// {"recognizer_model":"adaface","agegender_backend":"onnx","emotion_backend":"onnx",...}
extern "C" ANSFR_API int GetANSRFModelInfo(ANSCENTER::ANSFacialRecognition** Handle, LStrHandle modelInfo);
extern "C" ANSFR_API int GetUserString(ANSCENTER::ANSFacialRecognition * *Handle, int userId, std::string& userRecord);