16#include <xtensor/core/xmath.hpp>
17#include <xtensor/misc/xsort.hpp>
18#include <xtensor/views/xview.hpp>
20namespace fs = std::filesystem;
35 std::vector<std::string> modelsList = {
"enet_b0_8_best_vgaf",
"enet_b0_8_best_afew",
36 "enet_b2_8",
"enet_b0_8_va_mtl",
"enet_b2_7"};
37 if (backendName ==
"onnx") {
38 modelsList.push_back(
"mbf_va_mtl");
39 modelsList.push_back(
"mobilevit_va_mtl");
44std::unique_ptr<EmotiEffLibRecognizer>
46 const std::string& fullPipelineModelPath) {
47 checkBackend(backend);
49 if (backend ==
"torch")
50 return std::make_unique<EmotiEffLibRecognizerTorch>(fullPipelineModelPath);
51#elif defined(WITH_ONNX)
52 return std::make_unique<EmotiEffLibRecognizerOnnx>(fullPipelineModelPath);
54 throw std::runtime_error(
"EmotiEffCppLib wasn't compiled with any backend support.");
58std::unique_ptr<EmotiEffLibRecognizer>
63 return std::make_unique<EmotiEffLibRecognizerTorch>(config);
64#elif defined(WITH_ONNX)
65 return std::make_unique<EmotiEffLibRecognizerOnnx>(config);
67 throw std::runtime_error(
"EmotiEffCppLib wasn't compiled with any backend support.");
72 xt::xarray<float> result;
73 for (
size_t i = 0; i < faceImgs.size(); ++i) {
78 result = xt::zeros<float>({faceImgs.size(), features.shape(1)});
80 xt::view(result, i, xt::all()) = xt::view(features, 0, xt::all());
90 for (
size_t i = 0; i < faceImgs.size(); ++i) {
94 result.
scores = xt::zeros<float>({faceImgs.size(), res.scores.shape(1)});
96 result.
labels.insert(result.
labels.end(), res.labels.begin(), res.labels.end());
99 xt::view(result.
scores, i, xt::all()) = xt::view(res.scores, 0, xt::all());
107 modelName_ = fs::path(modelPath).filename().string();
110 bool is7 =
modelName_.find(
"_7") != std::string::npos;
136 xt::xarray<float> scores = score;
139 x = xt::view(scores, xt::all(),
140 xt::range(xt::placeholders::_, -2));
146 auto preds = xt::argmax(x, 1);
150 xt::xarray<float> max_x = xt::amax(x, {1}, xt::evaluation_strategy::immediate);
151 xt::xarray<float> e_x = xt::exp(x - max_x);
152 e_x /= xt::sum(e_x, {1}, xt::evaluation_strategy::immediate);
155 xt::view(scores, xt::all(), xt::range(xt::placeholders::_, -2)) =
164 for (
auto pred : preds) {
173 auto preds = xt::argmax(score, 1);
177 for (
auto pred : preds) {
187 xt::xarray<float> features_slices;
188 for (
int i = 0; i < maxIters; ++i) {
190 features_slices = xt::zeros<float>({
static_cast<size_t>(maxIters),
192 features.shape(1) * 2});
195 auto mean_x = xt::repeat(xt::expand_dims(xt::stddev(x, {0}), 0), x.shape(0), 0);
196 xt::view(features_slices, i, xt::all()) = xt::concatenate(xt::xtuple(mean_x, x), 1);
198 return features_slices;
201void EmotiEffLibRecognizer::checkBackend(
const std::string& backend) {
203 auto it = std::find(backends.begin(), backends.end(), backend);
204 if (it == backends.end()) {
205 throw std::runtime_error(
206 "This backend (" + backend +
207 ") is not supported. Please check your EmotiEffLib build or configuration.");
Header file for the ONNX backend implementation of the EmotiEffLibRecognizer.
Header file for the Torch backend implementation of the EmotiEffLibRecognizer.
virtual void initRecognizer(const std::string &modelPath)
Initializes the internal state of recognizer based on a modelPath.
EmotiEffLibRes processEngagementScores(const xt::xarray< float > &scores)
Common method to processes engagement scores and generate classification results.
bool isMtl_
Whether the model is multi-task learning (MTL).
xt::xarray< float > engagementFeaturesPreprocess(const xt::xarray< float > features)
Preprocesses engagement features.
static std::unique_ptr< EmotiEffLibRecognizer > createInstance(const std::string &backend, const std::string &fullPipelineModelPath)
Creates an instance of the EmotiEffLibRecognizer with the specified backend and model path.
const int engagementSlidingWindowSize_
Sliding window size for engagement prediction.
std::vector< std::string > idxToEngagementClass_
Engagement class names.
std::vector< std::string > idxToEmotionClass_
Emotion class names.
EmotiEffLibRes processEmotionScores(const xt::xarray< float > &scores, bool logits)
Common method to processes emotion scores and generate classification results.
std::string modelName_
Name of the model.
virtual EmotiEffLibRes predictEmotions(const cv::Mat &faceImg, bool logits=true)=0
Predicts emotions for a single face image.
virtual xt::xarray< float > extractFeatures(const cv::Mat &faceImg)=0
Extracts features from a single face image.
Header file for the EmotiEffLib library, providing emotion and engagement recognition functionality.
std::vector< std::string > getAvailableBackends()
Retrieves the list of available inference backends.
std::vector< std::string > getSupportedModels(const std::string &backendName)
Retrieves the list of supported emotion or engagement recognition models.
Configuration structure for the EmotiEffLibRecognizer.
std::string backend
The inference backend to use.
Structure to store the results of EmotiEffLib.
xt::xarray< float > scores
The confidence scores for each input frame.
std::vector< std::string > labels
The predicted emotion/engagement labels.