Guess the Number with Pepper using tablet
This tutorial will show you how to interact with Pepper tablet to play a “guess the number” game.
The ideas are:
Make pepper display a random number between 1-10 on a webpage on its tablet with a start button.
Upon a user pressing the start button, make Pepper say: which number is displayed on my tablet?
User mentions number, audio picked up by Pepper's microphone array.
Pepper transcribes the audio and checks number with the one of the tablet, and responds either: Yes, that's correct, OR No, [too high/too low]. Try again.
Approach
Before creating the demo, it’s essential to understand that the design of the WebserverComponent
goes hand in hand with the requirements of the demo, as the WebserverComponent
is the component that handles any events from a web client. For this specific tutorial, the WebserverComponent
should handle a button click event and send live transcripts produced by Dialogflow service to a connected client over a WebSocket connection.
This tutorial can be divided into two parts:
The design of HTML and
WebserverComponent
How to create a button and catch a click event on it?
How to render Dialogflow intermediate transcripts dynamically in a text box?
The demo program connects all the components (
NaoqiMicrophoneSensor
(or localDesktopMicrophone
),WebserverComponent
,DialogflowComponent
, etc)
The design of html and Webserver
1. How to create a button and catch a click event on it?
In the demo_pepper_guess_number.html
, we need to add a button and make it listen to the click event. Once the button is clicked, the isClicked
flag will be set to true, and it will send the flag to the Flask server via WebSocket.
// Flag to track if the game should start
var isClicked = false;
// Get a reference to the startButton element
var startButton = document.getElementById('startButton');
// Event listener for the start button click
startButton.addEventListener('click', function() {
isClicked = true; // Set the flag
sendFlagToServer(isClicked);
});
// Function to send flag to Flask server
function sendFlagToServer(flag) {
socket.emit('clicked_flag', flag);
}
On the Flaser server (webserver_pepper_tablet.py
), we need to register a handler to react to the "clicked_flag" event.
# register clicked_flag event handler
@self.socketio.on('clicked_flag')
def handle_flag(flag):
if flag:
self.output_message(ButtonClicked(button=flag))
If the flag is set to true, a SICMessage
“ButtonClicked” will be sent to the SIC channel as an output of this component. This output will be received on a callback on the demo side (demo_pepper_guess_number.py
) to determine whether the button has been clicked or not.
2. How to render Dialogflow intermediate transcripts dynamically in a text box?
In the html, first add a textarea element inside the body section.
<div>
<textarea id="textbox" rows="4" cols="50"></textarea>
</div>
In Javascript, we create a function to update the content of the textbox .
When a web client receives an “update_textbox” event emitted from the Flask server, the event handler is triggered. Then it calls the updateTextbox(text)
function passing the received text
as an argument.
On the Flaser server (webserver_pepper_tablet.py
), we emit the “update_textbox” event along with the transcript transcribed from Dialogflow.
The demo program connects all the components
The demo program is located in tests/demo_webserver/demo_pepper_guess_number.py.
There are many SIC components we need to connect to in this demo.
Components running on a pepper:
NaoqiMicrophoneSensor
NaoqiTextToSpeechActuator
NaoqiTabletComponent
Components running on a local machine:
WebserverComponent
DialogflowComponent
Here is the schematic overview of how this program works: The demo_pepper_guess_number
progam serves as the user application control, sending an HtmlMessage
as input for the WebserverComponent
to render. Upon receiving the ButtonClicked
message with a value of “True”, it sends a GetIntentRequest
to the DialogflowComponent
. The resulting RecognitionResult
is received in the on_dialog
callback, and upon obtaining the transcript result, a TranscriptMessage
is sent to the WebserverComponent
for dynamic transcript rendering in the above-mentioned textbox.
During each GetIntentRequest
request, the resulting query_text
generated by Dialogflow is passed to the extract_and_compare_number
function. This function extracts a number from the query_text
and compares it with the randomly generated number x
. When the detected number is higher/lower than or equal to x
, we will request the NaoqiTextToSpeechActuator
to make Pepper say the corresponding response.