Mastering Emotion detection


In this tutorial, we will use the ExcitementState and PleasureState characteristics to interpret the basic emotion of the human in front of Pepper.


Before stepping in this tutorial, you should:

  • Know how to use the HumanAwareness service. For further details, see: Human.

Let’s start a new project

  • Start a new project, let’s call it BasicEmotionPepper.
  • Robotify it and make sure it implements the QiSDK & the Robot Life Cycle.

For further details, see: Creating a robot application.

Observing basic emotions

Create the BasicEmotion enumeration to represent the human basic emotion:

 * Represent a basic emotion.
public enum BasicEmotion {

And a listener to react to a basic emotion change:

 * Listener used to notify when the basic emotion changes.
public interface OnBasicEmotionChangedListener {
    void onBasicEmotionChanged(BasicEmotion basicEmotion);

We need a class to observe the basic emotion of the human in front of Pepper. Create the BasicEmotionObserver class:

 * Observe the basic emotion of the first human seen by the robot.
public class BasicEmotionObserver {

    // Store the basic emotion listener.
    private OnBasicEmotionChangedListener listener;
    // Store the HumanAwareness service.
    private HumanAwareness humanAwareness;
    // Store the observed emotion.
    private Emotion observedEmotion;
    // Store the last excitement, pleasure and basic emotion.
    private ExcitementState lastExcitement;
    private PleasureState lastPleasure;
    private BasicEmotion lastBasicEmotion;

     * Start the observation.
     * @param qiContext the qiContext
    public void startObserving(QiContext qiContext) {
        // Get the HumanAwareness service.
        humanAwareness = qiContext.getHumanAwareness();

        // Retrieve the humans around and update the observed emotion.
        List<Human> humansAround = humanAwareness.getHumansAround();

        // Update the observed emotion when the humans around change.

     * Stop the observation.
    public void stopObserving() {
        // Clear observed emotion.

        // Remove listeners on HumanAwareness.
        if (humanAwareness != null) {
            humanAwareness = null;

     * Set the listener.
     * @param listener the listener
    public void setListener(OnBasicEmotionChangedListener listener) {
        this.listener = listener;

    private void updateObservedEmotion(List<Human> humansAround) {
        // Clear observed emotion.

        if (!humansAround.isEmpty()) {
            // Update observed emotion.
            Human observedHuman = humansAround.get(0);
            observedEmotion = observedHuman.getEmotion();

            // Get and store human excitement and pleasure.
            lastExcitement = observedEmotion.getExcitement();
            lastPleasure = observedEmotion.getPleasure();

            // Notify the listener.

            // Notify the listener when excitement changes.
            observedEmotion.addOnExcitementChangedListener(excitementState -> {
                if (excitementState != lastExcitement) {
                    lastExcitement = excitementState;

            // Notify the listener when pleasure changes.
            observedEmotion.addOnPleasureChangedListener(pleasureState -> {
                if (pleasureState != lastPleasure) {
                    lastPleasure = pleasureState;

    private void clearObservedEmotion() {
        // Remove listeners on observed emotion.
        if (observedEmotion != null) {
            observedEmotion = null;

    private BasicEmotion computeBasicEmotion(ExcitementState excitement, PleasureState pleasure) {
        if (excitement == ExcitementState.UNKNOWN || pleasure == PleasureState.UNKNOWN) {
            return BasicEmotion.UNKNOWN;

        switch (pleasure) {
            case POSITIVE:
                return (excitement == ExcitementState.CALM) ? BasicEmotion.CONTENT : BasicEmotion.JOYFUL;
            case NEGATIVE:
                return (excitement == ExcitementState.CALM) ? BasicEmotion.SAD : BasicEmotion.ANGRY;

        return BasicEmotion.NEUTRAL;

    private void notifyListener() {
        // Compute the basic emotion.
        BasicEmotion basicEmotion = computeBasicEmotion(lastExcitement, lastPleasure);
        // Notify the listener only if the basic emotion changed.
        if (basicEmotion != lastBasicEmotion) {
            lastBasicEmotion = basicEmotion;
            if (listener != null) {

This class observes the ExcitementState and PleasureState of the first human seen by using the HumanAwareness service, transforms them into a BasicEmotion and notifies its listener when this emotion changes.

Here is the transformation matrix we used, based on James Russel’s work:

This transformation is done in the computeBasicEmotion method.

Subscribe to the observer

Make your MainActivity implement the OnBasicEmotionChangedListener interface:

public class MainActivity extends RobotActivity implements RobotLifecycleCallbacks, OnBasicEmotionChangedListener

And override the onBasicEmotionChanged method:

public void onBasicEmotionChanged(BasicEmotion basicEmotion) {
    Log.i(TAG, "Basic emotion changed: " + basicEmotion);

Store the BasicEmotionObserver in the MainActivity:

// Store the basic emotion observer.
private BasicEmotionObserver basicEmotionObserver;

Create it in and subscribe to it the onCreate method:

// Create the basic emotion observer and listen to it.
basicEmotionObserver = new BasicEmotionObserver();

And unsubscribe from it in the onDestroy method:

// Stop listening to basic emotion observer and remove it.
basicEmotionObserver = null;

In the onRobotFocusGained callback, start the emotion observation:

// Start the basic emotion observation.

And stop it in the onRobotFocusLost callback:

// Stop the basic emotion observation.

Let’s try it

github_icon The sources for this tutorial are available on GitHub.

Step Action

Install and run the application.

For further details, see: Running an application.

Choose “Detect human emotions”.

Stay in front of Pepper, try to express an emotion with your smile, your voice or by touching Pepper’s sensors.

You should see some log information displaying your emotion when it changes.