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
+54
View File
@@ -1044,6 +1044,60 @@ namespace ANSCENTER {
}
}
// ====================================================================
// AdaFace
// ====================================================================
Ort::Value AdaFace::transform(const cv::Mat& mat)
{
if (mat.empty())
throw std::runtime_error("AdaFace::transform — input is empty.");
// AdaFace consumes BGR — OpenCV Mats are already BGR, no channel swap.
cv::Mat canvas;
cv::resize(mat, canvas, cv::Size(input_node_dims.at(3), input_node_dims.at(2)));
if (canvas.type() != CV_32FC3)
canvas.convertTo(canvas, CV_32FC3);
utils::transform::normalize_inplace(canvas, mean_val, scale_val);
std::vector<int64_t> shape = input_node_dims;
if (shape[0] == -1) shape[0] = 1;
return utils::transform::create_tensor(
canvas, shape, *memory_info_handler,
input_values_handler, utils::transform::CHW);
}
Ort::Value AdaFace::transformBatch(const std::vector<cv::Mat>& images)
{
if (images.empty())
throw std::runtime_error("AdaFace::transformBatch — batch is empty.");
const int width = input_node_dims.at(3);
const int height = input_node_dims.at(2);
std::vector<cv::Mat> batch;
batch.reserve(images.size());
for (const auto& mat : images) {
if (mat.empty())
throw std::runtime_error("AdaFace::transformBatch — empty image in batch.");
cv::Mat canvas;
cv::resize(mat, canvas, cv::Size(width, height));
if (canvas.type() != CV_32FC3)
canvas.convertTo(canvas, CV_32FC3);
utils::transform::normalize_inplace(canvas, mean_val, scale_val);
batch.push_back(std::move(canvas));
}
std::vector<int64_t> shape = input_node_dims;
shape[0] = static_cast<int64_t>(images.size());
return utils::transform::create_tensor_batch(
batch, shape, *memory_info_handler,
input_values_handler, utils::transform::CHW);
}
// ====================================================================
// GlintCosFace
// ====================================================================
+34
View File
@@ -674,6 +674,40 @@ namespace ANSCENTER {
Ort::Value transformBatch(const std::vector<cv::Mat>& images) override;
};
// ====================================================================
// AdaFace — face recognition
//
// Same session/inference path as GlintArcFace (detect/detectBatch are
// inherited and dispatch through the virtual transform), but AdaFace
// was trained on BGR input — the official repo flips RGB→BGR before
// normalize — so the BGR2RGB conversion is omitted: OpenCV Mats are
// already BGR. Normalization is identical: (x - 127.5) / 127.5.
// ====================================================================
class AdaFace : public GlintArcFace
{
public:
explicit AdaFace(const std::string& _onnx_path,
unsigned int _num_threads = 1)
: GlintArcFace(_onnx_path, _num_threads)
{
}
explicit AdaFace(const std::string& _onnx_path,
EngineType engineType,
unsigned int _num_threads = 1)
: GlintArcFace(_onnx_path, engineType, _num_threads) {
}
~AdaFace() override = default;
private:
static constexpr float mean_val = 127.5f;
static constexpr float scale_val = 1.f / 127.5f;
Ort::Value transform(const cv::Mat& mat) override;
Ort::Value transformBatch(const std::vector<cv::Mat>& images) override;
};
// ====================================================================
// MOVINET — action recognition
// ====================================================================