Leçon 1, Chapitre 1
En cours

Reconnaissance de texte – Défis Créer un algorithme pour trier le courrier postale

Yann KIDSHAKER 17 mars 2025

In this activity, we will build a Gift Courier Service which will work as follows:

  • Tobi has a gift, which he wants to deliver to one of the four International cities:
    • San Francisco
    • Toronto
    • Sydney
    • New Delhi
  • However tobi doesn’t know where to deliver the gift, instead we need to show tobi a pin-code number written on paper and tobi will deliver the gift to the corresponding city. The pin code numbers are as follows:
    • 94016  for San Francisco
    • 667777  for Toronto
    • 2001      for Sydney
    • 380001 for New Delhi
  •  As soon as you press the run button on the screen to run the script, a speech-recognition window pops up on the screen. You can show any pin-code on this screen pop-up and the gift will be sent to the corresponding city.

Let’s Code

Setting the Stage

  1. Open a new file in Pictoblox and choose the Python Coding environment.
  2. Import the time library. Your code must now look as follows:
    sprite = Sprite('Tobi')
    
    import time
  3. In this activity, we are going to have total 6 sprites on the stage. These are the following:
    1. Tobi sprite (By default)
    2. Gift sprite
    3. Home Button (4 of these)
  4. Click on Choose a sprite button (bottom right corner) and add all the sprites listed above, one-by-one.
  5. Also add the backdrop Blue Sky2 by clicking on the Choose a Backdrop button.
  6. Now, we need to label the 4 Home Button sprites according to the four International cities. For this follow the following steps:
    1. Click on the Home Button sprite
    2. Go to the Costumes tab
    3. Click on the T button, which is for writing text around the sprite
    4. Write the city name: San Francisco
  7. Similarly go to the sprites Home Button2, Home Button3, Home Button4 and repeat the above procedure.

Adding the Code

  1. Click on the Tobi.py file within the Project Files, we will write all our code here
  2. Since Tobi sprite is already initiated (by default), let us initiate the other five sprites as shown
    gift = Sprite('Gift')
    home1 = Sprite('Home Button')
    home2 = Sprite('Home Button2')
    home3 = Sprite('Home Button3')
    home4 = Sprite('Home Button4')
  3. Now, we will adjust the position of the six sprites on the stage. For this, we use the xy-grid to arrange the sprites all over the stage.
    The center point of the stage has the xy-coordinates (0, 0), we will arrange the sprites such that they cover the entire stage appropriately. We will use the following functions for this:

    1. gotoxy(): To move the sprite to a particular xy-coordinate
    2. goto(): To move the sprite to the position of a particular sprite
      sprite.gotoxy(-180, 0)
      home1.gotoxy(-80, 120)
      home2.gotoxy(180, 120)
      home3.gotoxy(-20, -140)
      home4.gotoxy(120, -140)
      gift.goto('Tobi')
  4. The stage should now look as shown below:
  5. Initiate the Text Recognition class so that we can use its functions
    tr = TextRecognition()
    
  6. Start the video on the stage by using the function video(). Since we are recognizing numbers, we also add the parameter ‘on flipped’ to prevent mirroring of letters
    tr.video('on flipped')
  7. Now, we want to analyze the pincodes, for this we will use the function analysecamera() of Text Recognition class and add the parameter ‘handWrittenText’ to it so that we can analyze pin-code written by our own hands on paper.
    We will write this inside a forever loop, so that we can analyze text continuously.

    while True:
      tr.analysecamera("handWrittenText")
  8. We want to write a conditional statement, using if, to check whether the pin code for San Francisco: 94016 is visible or not. If it is visible, then the sprite Gift must go to San Francisco (Home Button). For this, we use the following functions:
    1. handwrittentextresult(): To get the text recognition result of the text detected from the camera
    2. goto(): To send the Gift sprite to the location of sprite added as parameter
    3. str(): In-built function of python to convert pin-code from number to string data type.
        if tr.handwrittentextresult() == str(94016):
          gift.goto('Home Button')
  9. Let us also make the sprite say, where it is sending the gift. We will use the say() function from the Sprite class for this
      if tr.handwrittentextresult() == str(94016):
        sprite.say("Sending the gift to San Francisco")
        gift.goto('Home Button')
  10. Similarly, we will use the elif conditional statement to check the pin codes for Toronto: 66777, Sydney: 2001 and New Delhi:380001. We will use the same 3 functions (handwrittentextresult(), goto(), str()) we used in the case of San Francisco. We will also make the sprite say, where it is sending the gift. The code will be as shown
      elif tr.handwrittentextresult() == str(66777):
        sprite.say("Sending the gift to Toronto")
        gift.goto('Home Button2')  
        
      elif tr.handwrittentextresult() == str(2001):
        sprite.say("Sending the gift to Sydney")
        gift.goto('Home Button3')  
        
      elif tr.handwrittentextresult() == str(380001):
        sprite.say("Sending the gift to New Delhi")
        gift.goto('Home Button4') 
  11. If none of the pin codes are detected on the screen, then we want tobi to say ‘Try Again’, so we will add the appropriate code for that as well within the final else condition.
      else:
        sprite.say('Try Again', 2)
  12. Once, the gift is sent to the appropriate city, we need to bring the gift back to tobi so that it can be sent to another city. We will do this using goto() function on the gift instance. We will also use the sleep() function to wait for some time, before we can bring the gift back.
      time.sleep(3)
      gift.goto('Tobi')
  13. The entire while True (forever) loop will look as follows:
    while True:
      tr.analysecamera("handWrittenText")
      
      if tr.handwrittentextresult() == str(94016):
        sprite.say("Sending the gift to San Francisco")
        gift.goto('Home Button')
        
      elif tr.handwrittentextresult() == str(66777):
        sprite.say("Sending the gift to Toronto")
        gift.goto('Home Button2')  
        
      elif tr.handwrittentextresult() == str(2001):
        sprite.say("Sending the gift to Sydney")
        gift.goto('Home Button3')  
        
      elif tr.handwrittentextresult() == str(380001):
        sprite.say("Sending the gift to New Delhi")
        gift.goto('Home Button4')  
        
      else:
        sprite.say('Try Again', 2)
        
      time.sleep(3)
      gift.goto('Tobi')
  14. The final code will look as follows:
    sprite = Sprite('Tobi')
    
    import time
    
    gift = Sprite('Gift')
    home1 = Sprite('Home Button')
    home2 = Sprite('Home Button2')
    home3 = Sprite('Home Button3')
    home4 = Sprite('Home Button4')
    
    sprite.gotoxy(-180, 0)
    home1.gotoxy(-80, 120)
    home2.gotoxy(180, 120)
    home3.gotoxy(-20, -140)
    home4.gotoxy(120, -140)
    gift.goto('Tobi')
    
    tr = TextRecognition()
    tr.video('on flipped')
    
    while True:
      tr.analysecamera("handWrittenText")
      
      if tr.handwrittentextresult() == str(94016):
        sprite.say("Sending the gift to San Francisco")
        gift.goto('Home Button')
        
      elif tr.handwrittentextresult() == str(66777):
        sprite.say("Sending the gift to Toronto")
        gift.goto('Home Button2')  
        
      elif tr.handwrittentextresult() == str(2001):
        sprite.say("Sending the gift to Sydney")
        gift.goto('Home Button3')  
        
      elif tr.handwrittentextresult() == str(380001):
        sprite.say("Sending the gift to New Delhi")
        gift.goto('Home Button4')  
        
      else:
        sprite.say('Try Again', 2)
        
      time.sleep(3)
      gift.goto('Tobi')
    
  15. 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!