Leçon 1, Chapitre 1
En cours

Reconnaissance vocale – Défis Créer son assistant vocal en python

Yann KIDSHAKER 17 mars 2025

In this activity, we will make our own version of Alexa that replies to us and plays songs based on our voice command. We will build this using the PictoBlox python environment.

Songs to play

Lets code

  1. Open a new file in Pictoblox and choose the Python Coding environment.
  2. Import the time library.
    sprite = Sprite('Tobi')
    
    import time
  3. To upload the two songs (Mario and Spiderman) we downloaded in the PictoBlox environment:
    1. Go to the Sounds tab.
    2. Hover the mouse pointer over Choose a Sound button and click on Upload Sound.
    3. Upload both the sounds here, one by one.
  4. You will see that the Tobi sprite is already initiated (by default). Let us also initiate the following two classes:
    1. Speech Recognition: This class has functions to recognize our speech, and perform actions accordingly.
    2. Text to Speech: This class has functions to convert the text we enter in the code (as a string), to speech.
      sprite = Sprite('Tobi')
      
      import time             
      
      sr = SpeechRecognition()
      ts = TexttoSpeech()
  5. Now, in order to perform speech recognition, that is to analyse our speech we will use the analysespeech() function from the SpeechRecognition class.
    This function takes in two parameters:

    1. 4: Number of seconds, the speech will be recorded for
    2. “en-US”: The language that will be spoken
      sr.analysespeech(4, "en-US")
  6. We will store the result of the speech that we analysed above in the variable command, using the function speechresult() of SpeechRecognition class. This function has the speech recorded and stored, in the form of a string.
    command = sr.speechresult()
  7. We will convert the result stored (as string) in the variable command to lowercase, using the lower() function of python.
    command = command.lower()
  8. We want to check, whether the word ‘mario’ was spoken, in the speech recorded. For this we will do the following:
    1. Use the if conditional statement.
    2. We will check, whether the word/string ‘mario’ is in the string stored inside the command variable using the python keyword ‘in’.
      if 'mario' in command:
  9. If the word ‘mario’ is found in the speech, that is, in case if condition returns True, we want PictoBlox to speak ‘Playing Mario Song’. For this we use the speak() function from TexttoSpeech class.
    Also, we want the Tobi sprite to play the Mario song. For this we use the function playuntildone() from Sprite class.

    if 'mario' in command:
      ts.speak('Playing Mario Song')
      sprite.playuntildone("Mario")
  10. Similarly, we can use elif condition, to play the Spiderman song, when the word ‘spiderman’ is detected in the speech.
    elif 'spiderman' in command:
      ts.speak('Playing Spiderman Song')
      sprite.playuntildone("Spiderman")
  11. If neither ‘mario’ nor ‘spiderman’ word is detected in the speech, we can make the PictoBlox say ‘Sorry I am unable to understand the command’.
    else:
      ts.speak('Sorry I am unable to understand the command')
  12. Finally, we will stop any of the songs playing using the function stopallsounds() from the class. We will do this after waiting for 10 seconds.
    time.sleep(10)
    sprite.stopallsounds()
  13. The entire code looks as follows:
    sprite = Sprite('Tobi')
    
    import time
    
    sr = SpeechRecognition()
    ts = TexttoSpeech()
    
    
    sr.analysespeech(4, "en-US")
    
    command = sr.speechresult()
    command = command.lower()
    
    if 'mario' in command:
      ts.speak('Playing Mario Song')
      sprite.playuntildone("Mario")
    elif 'spiderman' in command:
      ts.speak('Playing Spiderman Song')
      sprite.playuntildone("Spiderman")
    else:
      ts.speak('Sorry I am unable to understand the command')
      
    time.sleep(10)
    sprite.stopallsounds()
      
    
  14. Press the Run button to test the script.

Assignment

Before you move on to the next lesson, a small assignment awaits you!

You must upload the PictoBlox program you created in this activity to the website. Submitting the assignment is a must in order to receive the certificate after completing the course.

Follow the steps below to upload your assignment:

  1. Click on Browse.
  2. Search and Select your saved Project file(.sb3) and Click Open.
  3. Click on Upload to submit the assignment.
evive Alert
The file type allowed is the SB3 file generated from the PictoBlox program. The maximum file size allowed is 5 MB.

Good luck!