-2

I have some random yes/no questions. I want the robot to ask these questions and then listen for the user's response. However, with my current code, the answer the robot outputs is always "None" (aka it is not hearing any response), even when I shout it right in the mic:

import random
import time
import qi

# survey questions
questions = [
    "Is the sky blue?",
    "Do you like pizza?",
    "Have you traveled abroad?",
    "Did you watch a movie last week?",
    "Is coffee your favorite drink?",
    # ... and so on ...
]

selected_questions = random.sample(questions, 5)

session = qi.Session()
session.connect("tcp://192.168.1.135:9559")  

tts = session.service("ALTextToSpeech")
tablet_service = session.service("ALTabletService")
speech_recognition = session.service("ALSpeechRecognition")
speech_recognition.setLanguage("English")
memory = session.service("ALMemory")

question_template = """
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <style>
        body {{
            font-size: 32px;
            text-align: center;
            margin-top: 30vh;
        }}
    </style>
</head>
<body>
    <div>{question}</div>
</body>
</html>
"""

for question in selected_questions:
    question_html = question_template.format(question=question)
    tablet_service.showWebview(question_html)
    tts.say(question)

    speech_detected = False
    user_answer = None
    timeout_duration = 10  # Timeout duration in seconds
    start_time = time.time()
    while not speech_detected or user_answer not in ["yes", "no"]:
        if time.time() - start_time > timeout_duration:
            break  # Timeout reached, proceed to the next question

        time.sleep(0.1)
        event = memory.getData("WordRecognized")
        if event and event[0] in ["yes", "no"]:
            user_answer = event[0]
            speech_detected = True

    print("User's answer:", user_answer)

session.close()

I can't figure out what the error is - I even had to add the time clause because otherwise it would run indefinitely.

  • 1
    Probably what's happening is that the `if event and event[0] in ["yes", "no"]` condition is never triggered for some reason. I suggest putting a `print(event)` or something similar to see what words if any the service is detecting. – Michael Cao Jul 17 '23 at 15:10
  • @MichaelCao This was helpful, thank you so much! I tried this and it is giving the weirdest responses, like I'll say absolutely nothing and it'll spit back "Good evening" or something. Any idea what could be happening? – user22241123 Jul 18 '23 at 16:35
  • Sorry, I'm not familiar with the service, so I can't say whether that's an issue on their end or yours. – Michael Cao Jul 18 '23 at 18:27

1 Answers1

-1

ALSpeechRecognition is not active all the time, you must start it via subscribe and before that specify a list of accepted phrases with setVocabulary. See a working example at the ALSpeechRecognition Tutorial.

Ales Horak
  • 114
  • 6