Aldebaran documentation What's new in NAOqi 2.4.3?

ALMood Tutorials

NAOqi Core - Overview | API | Tutorials


Python

Get current valence

almood_tutorial_getvalence.py

#! /usr/bin/env python
# -*- encoding: UTF-8 -*-

"""Example: Use currentPersonState Method"""

import qi
import argparse
import sys
import time


def main(session):
    """
    This example uses the currentPersonState method.
    """
    # Get the service ALMood.

    moodService = session.service("ALMood")
    moodService.subscribe("Tutorial_GetValence", "Active")
    # The preloading of all ALMood extractors may take up to 3 secondes:
    time.sleep(3)
    print moodService.currentPersonState()["valence"]["value"]

    moodService.unsubscribe("Tutorial_GetValence")


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--ip", type=str, default="127.0.0.1",
                        help="Robot IP address. On robot or Local Naoqi: use '127.0.0.1'.")
    parser.add_argument("--port", type=int, default=9559,
                        help="Naoqi port number")

    args = parser.parse_args()
    session = qi.Session()
    try:
        session.connect("tcp://" + args.ip + ":" + str(args.port))
    except RuntimeError:
        print ("Can't connect to Naoqi at ip \"" + args.ip + "\" on port " + str(args.port) +".\n"
               "Please check your script arguments. Run with -h option for help.")
        sys.exit(1)
    main(session)

To execute this script, type:

python almood_tutorial_getvalence.py --qi-url=tcp://<robot name or robot id>:9559

Get emotional reaction after a provocation

almood_tutorial_reaction.py

#! /usr/bin/env python
# -*- encoding: UTF-8 -*-

"""Example: Use getEmotionalReaction Method"""

import qi
import argparse
import sys
import time


def main(session):
    """
    This example uses the getEmotionalReaction method.
    """
    # Get the services ALMood and ALTextToSpeech.

    moodService = session.service("ALMood")
    tts = session.service("ALTextToSpeech")
    moodService.subscribe("Tutorial_RecordMood", "Active")
    # The preloading of all ALMood extractors may take up to 2 secondes:
    time.sleep(2)

    # The robot tries to provocate an emotion by greeting you
    tts.say("You look great today !")
    # The robot will try to analysis your reaction during the next 3 seconds
    print moodService.getEmotionalReaction()

    moodService.unsubscribe("Tutorial_RecordMood")


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--ip", type=str, default="127.0.0.1",
                        help="Robot IP address. On robot or Local Naoqi: use '127.0.0.1'.")
    parser.add_argument("--port", type=int, default=9559,
                        help="Naoqi port number")

    args = parser.parse_args()
    session = qi.Session()
    try:
        session.connect("tcp://" + args.ip + ":" + str(args.port))
    except RuntimeError:
        print ("Can't connect to Naoqi at ip \"" + args.ip + "\" on port " + str(args.port) +".\n"
               "Please check your script arguments. Run with -h option for help.")
        sys.exit(1)
    main(session)

To execute this script, type:

python almood_tutorial_get_reaction.py --qi-url=tcp://<robot name or robot id>:9559