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.