#include "ONNXFaceAttributes.h" #include #include #include #include namespace { // The exported classifier may or may not end with a softmax node // (Ultralytics kept flipping this across versions). Treat the output // as probabilities only when it already looks like a distribution. void SoftmaxIfNeeded(std::vector& v) { if (v.empty()) return; float sum = 0.0f; bool inRange = true; for (float x : v) { if (x < 0.0f || x > 1.0f) { inRange = false; break; } sum += x; } if (inRange && std::fabs(sum - 1.0f) < 0.05f) return; float maxVal = *std::max_element(v.begin(), v.end()); float denom = 0.0f; for (float& x : v) { x = std::exp(x - maxVal); denom += x; } if (denom > 0.0f) { for (float& x : v) x /= denom; } } void ResolveInputSize(const std::vector& dims, int& width, int& height) { // NCHW; dynamic dims come back as -1 — the shipped models are 224x224. width = 224; height = 224; if (dims.size() >= 4) { if (dims[2] > 0) height = static_cast(dims[2]); if (dims[3] > 0) width = static_cast(dims[3]); } } } namespace ANSCENTER { // ================================================================ // OnnxAgeGenderEstimator // ================================================================ OnnxAgeGenderEstimator::OnnxAgeGenderEstimator(const std::string& onnxPath, EngineType engineType, const OrtHandlerOptions& options, unsigned int numThreads) : BasicOrtHandler(onnxPath, engineType, options, numThreads) { _ready = true; } Ort::Value OnnxAgeGenderEstimator::transform(const cv::Mat& mat) { if (mat.empty()) throw std::runtime_error("OnnxAgeGenderEstimator::transform — input is empty."); int width = 0, height = 0; ResolveInputSize(input_node_dims, width, height); cv::Mat canvas = mat; if (canvas.channels() == 1) cv::cvtColor(canvas, canvas, cv::COLOR_GRAY2BGR); cv::Mat resized; cv::resize(canvas, resized, cv::Size(width, height), 0, 0, cv::INTER_LINEAR); cv::cvtColor(resized, resized, cv::COLOR_BGR2RGB); resized.convertTo(resized, CV_32FC3, 1.0 / 255.0); // ImageNet normalization (ViTFeatureExtractor defaults) static const float mean_vals[3] = { 0.485f, 0.456f, 0.406f }; static const float scale_vals[3] = { 1.f / 0.229f, 1.f / 0.224f, 1.f / 0.225f }; utils::transform::normalize_inplace(resized, mean_vals, scale_vals); std::vector shape = input_node_dims; if (shape.size() < 4) shape = { 1, 3, height, width }; if (shape[0] <= 0) shape[0] = 1; if (shape[2] <= 0) shape[2] = height; if (shape[3] <= 0) shape[3] = width; return utils::transform::create_tensor( resized, shape, *memory_info_handler, input_values_handler, utils::transform::CHW); } Ort::Value OnnxAgeGenderEstimator::transformBatch(const std::vector&) { throw std::runtime_error("OnnxAgeGenderEstimator::transformBatch — not supported."); } AgeGenderResults OnnxAgeGenderEstimator::runInfer(const cv::Mat& frame) { std::lock_guard lock(_mutex); try { if (!_ready || frame.empty()) return { -1, -1 }; Ort::Value input_tensor = transform(frame); DmlInferenceGuard dmlGuard(m_engineType, "OnnxAgeGenderEstimator::runInfer"); auto output_tensors = ort_session->Run( Ort::RunOptions{ nullptr }, input_node_names.data(), &input_tensor, 1, output_node_names.data(), num_outputs); // Two known export layouts, distinguished by tensor sizes: // (a) v1.4 pack: age_pred [1] (years) + gender_logits [1,2] // with class order [male, female] // (b) research export: a single [1,2] tensor of // [age_years, gender_logit] where sigmoid >= 0.5 -> Female const float* agePtr = nullptr; const float* genderLogits = nullptr; for (auto& tensor : output_tensors) { const size_t count = tensor.GetTensorTypeAndShapeInfo().GetElementCount(); if (count == 1 && !agePtr) agePtr = tensor.GetTensorMutableData(); else if (count >= 2 && !genderLogits) genderLogits = tensor.GetTensorMutableData(); } if (agePtr && genderLogits) { // layout (a) const float age = std::clamp(agePtr[0], 0.0f, 100.0f); const float m = genderLogits[0], f = genderLogits[1]; const float mx = std::max(m, f); const float em = std::exp(m - mx), ef = std::exp(f - mx); return { age, em / (em + ef) }; } if (genderLogits) { // layout (b) const float age = std::clamp(genderLogits[0], 0.0f, 100.0f); const float femaleProb = 1.0f / (1.0f + std::exp(-genderLogits[1])); return { age, 1.0f - femaleProb }; } return { -1, -1 }; } catch (const std::exception& error) { std::cerr << "OnnxAgeGenderEstimator error: " << error.what() << std::endl; return { -1, -1 }; } } // ================================================================ // OnnxEmotionsClassifier // ================================================================ std::vector OnnxEmotionsClassifier::LoadClassNames(const std::string& namesFilePath) { std::vector names; try { std::ifstream file(namesFilePath); std::string line; while (std::getline(file, line)) { while (!line.empty() && (line.back() == '\r' || line.back() == '\n' || line.back() == ' ' || line.back() == '\t')) line.pop_back(); if (!line.empty()) names.push_back(line); } } catch (const std::exception&) { names.clear(); } if (names.empty()) { names = { "neutral", "happy", "sad", "surprise", "anger" }; } return names; } OnnxEmotionsClassifier::OnnxEmotionsClassifier(const std::string& onnxPath, std::vector classNames, EngineType engineType, const OrtHandlerOptions& options, unsigned int numThreads) : BasicOrtHandler(onnxPath, engineType, options, numThreads) , _classNames(std::move(classNames)) { if (_classNames.empty()) _classNames = { "neutral", "happy", "sad", "surprise", "anger" }; _ready = true; } Ort::Value OnnxEmotionsClassifier::transform(const cv::Mat& mat) { if (mat.empty()) throw std::runtime_error("OnnxEmotionsClassifier::transform — input is empty."); int width = 0, height = 0; ResolveInputSize(input_node_dims, width, height); cv::Mat canvas = mat; if (canvas.channels() == 1) cv::cvtColor(canvas, canvas, cv::COLOR_GRAY2BGR); cv::Mat resized; cv::resize(canvas, resized, cv::Size(width, height), 0, 0, cv::INTER_LINEAR); cv::cvtColor(resized, resized, cv::COLOR_BGR2RGB); // Ultralytics classifier preprocessing: RGB, scaled to [0, 1] resized.convertTo(resized, CV_32FC3, 1.0 / 255.0); std::vector shape = input_node_dims; if (shape.size() < 4) shape = { 1, 3, height, width }; if (shape[0] <= 0) shape[0] = 1; if (shape[2] <= 0) shape[2] = height; if (shape[3] <= 0) shape[3] = width; return utils::transform::create_tensor( resized, shape, *memory_info_handler, input_values_handler, utils::transform::CHW); } Ort::Value OnnxEmotionsClassifier::transformBatch(const std::vector&) { throw std::runtime_error("OnnxEmotionsClassifier::transformBatch — not supported."); } std::map OnnxEmotionsClassifier::runInfer(const cv::Mat& frame) { std::lock_guard lock(_mutex); try { if (!_ready || frame.empty()) return { }; Ort::Value input_tensor = transform(frame); DmlInferenceGuard dmlGuard(m_engineType, "OnnxEmotionsClassifier::runInfer"); auto output_tensors = ort_session->Run( Ort::RunOptions{ nullptr }, input_node_names.data(), &input_tensor, 1, output_node_names.data(), num_outputs); auto info = output_tensors.at(0).GetTensorTypeAndShapeInfo(); const size_t count = info.GetElementCount(); if (count == 0) return { }; const float* vals = output_tensors.at(0).GetTensorMutableData(); std::vector scores(vals, vals + count); SoftmaxIfNeeded(scores); std::map emotions; for (size_t i = 0; i < scores.size(); ++i) { const std::string name = (i < _classNames.size()) ? _classNames[i] : ("class_" + std::to_string(i)); emotions[name] = scores[i]; } return emotions; } catch (const std::exception& error) { std::cerr << "OnnxEmotionsClassifier error: " << error.what() << std::endl; return { }; } } }