EmotiEffLib
repository·main·Indexed 22 days ago
https://github.com/sb-ai-lab/emotiefflibA lightweight, high-performance library for facial emotion and engagement recognition. It supports both Python and C++ interfaces with PyTorch and ONNX backends, enabling real-time analysis of static images and video sequences. The library includes pre-trained models and provides workflows for predicting facial expressions and engagement levels.
What's inside emotiefflib
- EmotiEffLib (formerly HSEmotion) is a lightweight library designed for efficient emotion and engagement recognition in both photos and videos. It is optimized for real-time analysis across various platforms and supports both Python and C++ interfaces. The library provides flexibility through backend support for PyTorch and ONNX.
Train EmotiEffNet models using PyTorch
mainThe primary method for training EmotiEffNet models (such as
EmotiEffNet-B2) and classifying emotional features for the AFEW and VGAF datasets is via theaffectnet/train_emotions-pytorch.ipynbnotebook.Prerequisite: Before running the main training notebook, you must run the preprocessing notebooks for the specific datasets you are using:
affectnet/train_emotions.ipynbAFEW_train.ipynbVGAF_train.ipynb
Access EmotiEffLib tutorials for Python and C++
mainEmotiEffLib provides specific tutorials for both Python and C++ interfaces. You can find step-by-step guides in the following directories:
- Python tutorials:
tutorials/python - C++ tutorials:
tutorials/cpp
- Python tutorials:
Install dependencies for EmotiEffLib Python examples
mainTo run the Python tutorials and examples provided in the repository, you must install the necessary dependencies using
pipfrom therequirements.txtfile located in the examples directory.pip install -r requirements.txtInstall EmotiEffLib from source
mainIf you are working with the local repository, you can install the library directly from the source code using
pip install .with optional dependency extras:- Standard (ONNX support):
pip install . - With Torch support:
pip install .[torch] - With Engagement prediction (requires TensorFlow):
pip install .[engagement] - Full installation:
pip install .[all]orpip install .[torch,engagement]
# Install EmotiEffLib with ONNX and Torch support from local source pip install .[torch]- Standard (ONNX support):
Train TensorFlow emotional models
mainTo train emotional models using TensorFlow (e.g.,mobilenet_7), use theaffectnet/train_emotions.ipynbnotebook located in theaffectnetdirectory.Build and run EmotiEffLib C++ examples
mainTo run the C++ tutorials (which are provided as Jupyter notebooks using the
xcpp17kernel), follow these steps to set up your environment, build the core library, and prepare the necessary models and data.1. Install Python Dependencies
Ensure all required Python packages are installed:
pip install -r requirements.txt2. Build EmotiEffCppLib
You must build the
emotieffcpplibpackage using Libtorch and ONNXRuntime. To ensure compatibility with the Jupyter C++ kernel (xeus-cling), you must include the following CMake flags:-DBUILD_TESTS=ON: Required to reuse the library built for tests.-DBUILD_SHARED_LIBS=ON: Required becausexeus-clingonly works with shared libraries.
3. Install xeus-cling
Install the
xeus-clingkernel to enable C++ execution within Jupyter. After installation, verify that thexcpp17kernel is available by running:jupyter kernelspec list4. Prepare Models
Run the model preparation script to format models for the C++ runtime:
python3 <EmotiEffLib_root>/models/prepare_models_for_emotieffcpplib.py5. Setup Test Data
Download and unpack the required test datasets:
cd <EmotiEffLib_root>/tests ./download_test_data.sh tar -xzf data.tar.gz6. Execute Tutorials
Launch Jupyter Notebook and select the
xcpp17kernel to run the examples.Quick start guides for Python and C++
mainThe library provides several tutorial notebooks to get started quickly with different tasks:
Python Interface
- One image emotion recognition: Notebook link
- Predict emotions on video: Notebook link
- Predict engagement and emotions on video: Notebook link
C++ Interface
- One image emotion recognition: Notebook link
- Predict emotions on video: Notebook link
- Predict engagement and emotions on video: Notebook link
Create personalized engagement detection models
mainTo create personalized models for engagement detection, use the Jupyter notebooks provided in the
training_and_examples/personalized_models/directory. The process follows a pipeline of feature extraction, universal model training, custom dataset processing, and user adaptation:- Extract Visual Features: Use
01 - Features-extractor.ipynbto extract visual features from video files. - Train Universal Model: Use
02 - Engagement user-independent model training.ipynbto train a universal engagement recognition model that is independent of specific users. - Process Custom Datasets: Use
03 - Custom dataset features extractor.ipynbto extract visual features specifically from your personalized dataset. - Adapt to Users: Use
04 - Engagement user-adaptation.ipynbto create models that are specifically adapted to individual users based on the extracted features.
- Extract Visual Features: Use
Prepare models for the mobile application
mainTo run the EmotiEffLib mobile application, you must convert the existing models to mobile-friendly formats. Navigate to the
mobile_appdirectory and execute the conversion scripts for TFLite and PyTorch Lite.Note: Ensure you have the necessary model files available before running these scripts.
cd mobile_app python to_tflite.py python to_pytorchlite.pyPrepare models for C++ inference
mainIf you are using the
EmotiEffCppLib(C++ interface), you must first prepare the models for inference using the provided Python script:python models/prepare_models_for_emotieffcpplib.pyIntegrate EmotiEffCppLib into a C++ project
mainTo use EmotiEffCppLib as a dependency in your own C++ project, add the repository as a submodule and configure your
CMakeLists.txtto include the library and its headers. You must specify the paths to your chosen inference engine (Libtorch or ONNX Runtime).# At least one of these variables should be specified set(WITH_TORCH "/path/to/libtorch") set(WITH_ONNX "/path/to/ONNXRuntime") add_subdirectory("${PROJECT_SOURCE_DIR}/path/to/emotiefflib/emotieffcpplib") include_directories( "${PROJECT_SOURCE_DIR}/path/to/emotiefflib/emotieffcpplib/include" ) target_link_libraries(your_project PRIVATE emotiefflib)