EmotiEffCppLib
Cpp implementation of Emotion Efficient Library
Loading...
Searching...
No Matches
facial_analysis.h
Go to the documentation of this file.
1
6
7#ifndef FACIAL_ANALYSIS_H
8#define FACIAL_ANALYSIS_H
9
10#include <opencv2/opencv.hpp>
11#include <string>
12#include <vector>
13#include <xtensor/containers/xarray.hpp>
14
15namespace EmotiEffLib {
16
27std::vector<std::string> getAvailableBackends();
28
38std::vector<std::string> getSupportedModels(const std::string& backendName);
39
52 std::vector<std::string> labels;
53
60 xt::xarray<float> scores;
61};
62
77 std::string backend = "";
78
85
91 std::string featureExtractorPath = "";
92
98 std::string classifierPath = "";
99
105 std::string engagementClassifierPath = "";
106
112 std::string modelName = "";
113};
114
122public:
123 virtual ~EmotiEffLibRecognizer() = default;
124
125 /*****************************************************************************
126 * Initializers
127 *****************************************************************************/
136 static std::unique_ptr<EmotiEffLibRecognizer>
137 createInstance(const std::string& backend, const std::string& fullPipelineModelPath);
138
145 static std::unique_ptr<EmotiEffLibRecognizer> createInstance(const EmotiEffLibConfig& config);
146
147 /*****************************************************************************
148 * Getters for class name
149 *****************************************************************************/
156 inline std::string getEmotionClassById(int idx) { return idxToEmotionClass_[idx]; }
157
164 inline std::string getEngagementClassById(int idx) { return idxToEngagementClass_[idx]; }
165
166 /*****************************************************************************
167 * Public API
168 *****************************************************************************/
175 virtual xt::xarray<float> extractFeatures(const cv::Mat& faceImg) = 0;
176
183 virtual xt::xarray<float> extractFeatures(const std::vector<cv::Mat>& faceImgs);
184
192 virtual EmotiEffLibRes classifyEmotions(const xt::xarray<float>& features,
193 bool logits = true) = 0;
194
201 virtual EmotiEffLibRes classifyEngagement(const xt::xarray<float>& features) = 0;
202
210 virtual EmotiEffLibRes predictEmotions(const cv::Mat& faceImg, bool logits = true) = 0;
211
219 virtual EmotiEffLibRes predictEmotions(const std::vector<cv::Mat>& faceImgs,
220 bool logits = true);
221
228 virtual EmotiEffLibRes predictEngagement(const std::vector<cv::Mat>& faceImgs) = 0;
229
230protected:
236 virtual void initRecognizer(const std::string& modelPath);
237
245 EmotiEffLibRes processEmotionScores(const xt::xarray<float>& scores, bool logits);
246
253 EmotiEffLibRes processEngagementScores(const xt::xarray<float>& scores);
254
260 virtual void configParser(const EmotiEffLibConfig& config) = 0;
261
268 virtual xt::xarray<float> preprocess(const cv::Mat& img) = 0;
269
276 xt::xarray<float> engagementFeaturesPreprocess(const xt::xarray<float> features);
277
278private:
284 static void checkBackend(const std::string& backend);
285
286protected:
288 std::string modelName_ = "";
300 std::vector<std::string> idxToEngagementClass_ = {"Distracted", "Engaged"};
302 std::vector<std::string> idxToEmotionClass_;
304 bool isMtl_;
307};
308
309} // namespace EmotiEffLib
310
311#endif
Base class for emotion and engagement recognition.
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.
int engagementClassifierIdx_
Index of the engagement classifier model in the array with models.
virtual EmotiEffLibRes classifyEngagement(const xt::xarray< float > &features)=0
Classifies engagement based on extracted features.
int featureExtractorIdx_
Index of the model for features extraction in the array with models.
int classifierIdx_
Index of the classifier model in the array with models.
int fullPipelineModelIdx_
Index of the full pipeline model in the array with models.
std::string getEmotionClassById(int idx)
Gets the emotion class name by index.
const int engagementSlidingWindowSize_
Sliding window size for engagement prediction.
virtual xt::xarray< float > preprocess(const cv::Mat &img)=0
Preprocesses an image for feature extraction.
std::string getEngagementClassById(int idx)
Gets the engagement class name by index.
virtual void configParser(const EmotiEffLibConfig &config)=0
Parses the configuration to initialize the recognizer.
std::vector< std::string > idxToEngagementClass_
Engagement class names.
virtual ~EmotiEffLibRecognizer()=default
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 classifyEmotions(const xt::xarray< float > &features, bool logits=true)=0
Classifies emotions based on extracted features.
virtual EmotiEffLibRes predictEmotions(const cv::Mat &faceImg, bool logits=true)=0
Predicts emotions for a single face image.
virtual EmotiEffLibRes predictEngagement(const std::vector< cv::Mat > &faceImgs)=0
Predicts engagement for a sequence of face images.
virtual xt::xarray< float > extractFeatures(const cv::Mat &faceImg)=0
Extracts features from a single face image.
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 classifierPath
Path to the emotion classifier model file.
std::string backend
The inference backend to use.
std::string featureExtractorPath
Path to the feature extractor model file.
std::string fullPipelineEmotionModelPath
Path to the model with full emotion recognition pipeline.
std::string modelName
The name of the selected model.
std::string engagementClassifierPath
Path to the engagement classifier model file.
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.