EmotiEffLib

repository·main·Indexed 22 days ago

https://github.com/sb-ai-lab/emotiefflib

A 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.

Tokens
20.1K
Snippets
62
Records
76
Agent score
77%

What's inside emotiefflib

  1. Overview of EmotiEffLib

    main
    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.
  2. Train EmotiEffNet models using PyTorch

    main

    The primary method for training EmotiEffNet models (such as EmotiEffNet-B2) and classifying emotional features for the AFEW and VGAF datasets is via the affectnet/train_emotions-pytorch.ipynb notebook.

    Prerequisite: Before running the main training notebook, you must run the preprocessing notebooks for the specific datasets you are using:

    • affectnet/train_emotions.ipynb
    • AFEW_train.ipynb
    • VGAF_train.ipynb
  3. Install EmotiEffLib from source

    main

    If 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] or pip install .[torch,engagement]
    # Install EmotiEffLib with ONNX and Torch support from local source
    pip install .[torch]
  4. Build and run EmotiEffLib C++ examples

    main

    To run the C++ tutorials (which are provided as Jupyter notebooks using the xcpp17 kernel), 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.txt

    2. Build EmotiEffCppLib

    You must build the emotieffcpplib package 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 because xeus-cling only works with shared libraries.

    3. Install xeus-cling

    Install the xeus-cling kernel to enable C++ execution within Jupyter. After installation, verify that the xcpp17 kernel is available by running:

    jupyter kernelspec list

    4. Prepare Models

    Run the model preparation script to format models for the C++ runtime:

    python3 <EmotiEffLib_root>/models/prepare_models_for_emotieffcpplib.py

    5. Setup Test Data

    Download and unpack the required test datasets:

    cd <EmotiEffLib_root>/tests
    ./download_test_data.sh
    tar -xzf data.tar.gz

    6. Execute Tutorials

    Launch Jupyter Notebook and select the xcpp17 kernel to run the examples.

  5. Quick start guides for Python and C++

    main

    The library provides several tutorial notebooks to get started quickly with different tasks:

    Python Interface

    C++ Interface

  6. Create personalized engagement detection models

    main

    To 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:

    1. Extract Visual Features: Use 01 - Features-extractor.ipynb to extract visual features from video files.
    2. Train Universal Model: Use 02 - Engagement user-independent model training.ipynb to train a universal engagement recognition model that is independent of specific users.
    3. Process Custom Datasets: Use 03 - Custom dataset features extractor.ipynb to extract visual features specifically from your personalized dataset.
    4. Adapt to Users: Use 04 - Engagement user-adaptation.ipynb to create models that are specifically adapted to individual users based on the extracted features.
  7. Prepare models for the mobile application

    main

    To run the EmotiEffLib mobile application, you must convert the existing models to mobile-friendly formats. Navigate to the mobile_app directory 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.py
  8. Integrate EmotiEffCppLib into a C++ project

    main

    To use EmotiEffCppLib as a dependency in your own C++ project, add the repository as a submodule and configure your CMakeLists.txt to 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)