Files
ANSCORE/modules/ANSFR/ONNXFaceAttributes.h
T
nghianguyenandClaude Fable 5 ac4369302a 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>
2026-08-26 11:06:10 +10:00

70 lines
2.6 KiB
C++

#ifndef ONNXFACEATTRIBUTES_H
#define ONNXFACEATTRIBUTES_H
#pragma once
// ONNX Runtime backends for the face attribute models shipped with
// ANS_FaceRecognizer v1.4:
// - agegender.onnx ViT-Base age/gender head (224x224, ImageNet norm,
// 2 logits: [age 0..100, gender sigmoid -> Female >= 0.5])
// - emotionsrecognition.onnx YOLOv11 classifier (class list in
// emotionsrecognition.names)
//
// Execution provider follows the detected hardware through BasicOrtHandler:
// NVIDIA -> TensorRT EP (disk-cached engines) with CUDA EP fallback,
// AMD -> DirectML, Intel -> OpenVINO, otherwise CPU.
#include "ANSFRCommon.h"
#include "ONNXEngine.h"
#include <mutex>
namespace ANSCENTER {
class OnnxAgeGenderEstimator : public BasicOrtHandler, public IAgeGenderEstimator {
public:
OnnxAgeGenderEstimator(const std::string& onnxPath,
EngineType engineType,
const OrtHandlerOptions& options,
unsigned int numThreads = 2);
~OnnxAgeGenderEstimator() override = default;
AgeGenderResults runInfer(const cv::Mat& frame) override;
bool enabled() const override { return _ready; }
const char* backendName() const override { return "onnx"; }
private:
Ort::Value transform(const cv::Mat& mat) override;
Ort::Value transformBatch(const std::vector<cv::Mat>& images) override;
std::recursive_mutex _mutex;
bool _ready{ false };
};
class OnnxEmotionsClassifier : public BasicOrtHandler, public IEmotionsClassifier {
public:
OnnxEmotionsClassifier(const std::string& onnxPath,
std::vector<std::string> classNames,
EngineType engineType,
const OrtHandlerOptions& options,
unsigned int numThreads = 2);
~OnnxEmotionsClassifier() override = default;
std::map<std::string, float> runInfer(const cv::Mat& frame) override;
bool enabled() const override { return _ready; }
const char* backendName() const override { return "onnx"; }
// Reads one class name per line; returns the legacy 5-emotion list
// when the file is missing/empty so the classifier still produces
// labelled output.
static std::vector<std::string> LoadClassNames(const std::string& namesFilePath);
private:
Ort::Value transform(const cv::Mat& mat) override;
Ort::Value transformBatch(const std::vector<cv::Mat>& images) override;
std::recursive_mutex _mutex;
std::vector<std::string> _classNames;
bool _ready{ false };
};
}
#endif