Leçon 1, Chapitre 1
En cours

Computer Vision – Défis Créer un algorithme pour reconnaitre des célérités

Yann KIDSHAKER 17 mars 2025

In this activity, we will write a code that will perform Celebrity Detection on the PictoBlox stage. We will upload an image containing one or more celebrities as a PictoBlox backdrop. The code we run to analyze this stage will detect the following things from the image:

  1. Face of celebrity, x-position, y-position and height of face
  2. Celebrity name
  3. Confidence(in %) with which the algorithm have detected the celebrity

This will be done for all the celebrities present in the image.

Let’s Code

  1. Open a new file in Pictoblox and choose the Python Coding environment.
  2. Click on Choose a sprite button (bottom right corner) and select the sprite Square Box.
  3. In this session, we will perform celebrity detection on the image below:
  4. For this, first upload this image as a backdrop in the PictoBlox by following the below steps:
    1. Right click on the above image and choose the save image as option to download it on your computer.
    2. Hover your mouse over the Choose a Backdrop button (bottom right hand corner) and choose the Upload Backdrop option.
    3. Select the image you just downloaded and click Open.
    4. Click on Python (Beta tab).
    5. Click on Square Box.py from the Project Files. The image is now uploaded as backdrop.
  5. The sprite object ‘Square Box’ is already initiated by default. Let us import the time library.
    sprite = Sprite('Square Box')
    
    import time
  6. Let us now also initiate the Computer Vision class, so that we can use the functions from this class.
    cv = ComputerVision()
  7. In order to analyze the backdrop image that we have added for celebrity detection we use the function analysebackdrop([1]) from Computer Vision class and choose the parameter “celebrity”.
    cv.analysebackdrop("celebrity")
  8. Now, we will calculate the number of celebrities present in the backdrop image using imagefeaturecount() function. This function returns a string, so we will convert it to integer using int() function of python and store it in a variable.
    num_celebrities = int(cv.imagefeaturecount("celebrity"))
    
  9. We want to detect all the celebrities in the image, one-by-one. Hence, we will use a for loop to go through all of them by keeping the range from 1 to num_celebrities + 1. We add ‘+1’ since the end parameter of range() function is excluded.
    for i in range(1, num_celebrities + 1):
  10. Now, we want to set the x-position of the Square Box sprite equal to the celebrity face that is detected.
    1. In order to extract the x-position of the celebrity face, we use the imagefeatureinfo() function from the Computer Vision class and add the appropriate parameters as follows:
      1. [1] = “celebrity” (for what we want to detect)
      2. [2] = i (for the current celebrity)
      3. [3] = “xPos” (for x-position)
    2. We enclose the above function inside the setx() function from Sprite class to set the x-position of the sprite.
      for i in range(1, num_celebrities + 1):
        sprite.setx(cv.imagefeatureinfo("celebrity", i, "xPos"))
  11. Using similar steps we used in the above point, we can set the y-position of the sprite equal to the celebrity face.
    1. Here, again we will use the imagefeatureinfo() function with the difference in parameter [3] = “yPos” (for y-position).
    2. We will enclose this function inside the sety() function to set the y-position of the sprite.
      for i in range(1, num_celebrities + 1):
        sprite.setx(cv.imagefeatureinfo("celebrity", i, "xPos"))
        sprite.sety(cv.imagefeatureinfo("celebrity", i, "yPos"))
  12. Using similar steps we used in the above point, we can set the size of the sprite equal to the height of the celebrity face.
    1. Here, again we will use the imagefeatureinfo() function with the difference in parameter [3] = “width” (for y-position).
    2. We will enclose this function inside the setsize() function to set the size of the sprite.
      for i in range(1, num_celebrities + 1):
        sprite.setx(cv.imagefeatureinfo("celebrity", i, "xPos"))
        sprite.sety(cv.imagefeatureinfo("celebrity", i, "yPos"))
        sprite.setsize(cv.imagefeatureinfo("celebrity", i, "height"))
  13. Now that we have positioned the Square Box sprite and set its size, we will make it say the name of the corresponding celebrity. For this, we again use the function imagefeatureinfo() with the same parameters as above except that we do not add the parameter [3].
    We put this function in the say() function of the Sprite class.

      sprite.say(cv.imagefeatureinfo("celebrity", i))
  14. Now that we have displayed the name of celebrity, we want to pause the display for 1 second before we display the confidence. We use sleep() function of time module for this.
      time.sleep()
  15. In AI, “Confidence” is the probability with which the machine learning algorithm is sure of its prediction. We will calculate the confidence of our Computer Vision algorithm on its detection of a particular celebrity, and store it in a variable. For this, we will use the following functions:
    1. We use the function imagefeatureinfo() with the same parameters as above except for parameter [3] where we add [3] = “confidence”.
    2. The return value of the function imagefeatureinfo() is a string. So we convert it to a float using the float() in-built function of python.
    3. We enclose the above code in the first parameter [1] of the in-built function round([1], 4) of python, to keep the number of decimal places limited to ‘4‘.
        confidence = round(float(cv.imagefeatureinfo("celebrity", i, 'confidence')), 4)
      
  16. Now, we will make the sprite say the ‘confidence percentage’ by multiplying the confidence value by 100. We will also convert the ‘confidence percentage’, from number to a string using the inbuilt function str() of python.
      sprite.say("Confidence = " + str(confidence * 100) + "%")
    
  17. Finally, we add a time.sleep() function to add a pause between the detection of two celebrities. The final code will look as below:
    sprite = Sprite('Square Box')
    import time
    
    cv = ComputerVision()
    
    cv.analysebackdrop("celebrity")
    
    num_celebrities = int(cv.imagefeaturecount("celebrity"))
    
    
    for i in range(1, num_celebrities + 1):
      sprite.setx(cv.imagefeatureinfo("celebrity", i, "xPos"))
      sprite.sety(cv.imagefeatureinfo("celebrity", i, "yPos"))
      sprite.setsize(cv.imagefeatureinfo("celebrity", i, "height"))
      sprite.say(cv.imagefeatureinfo("celebrity", i))
      
      time.sleep(1)
      confidence = round(float(cv.imagefeatureinfo("celebrity", i, 'confidence')), 4)
      sprite.say("Confidence = " + str(confidence * 100) + "%")
      time.sleep(2)
  18. Press the Run button to test the script.

Further Testing

You can further try, celebrity detection on the following images:

Test image- 1

Test image- 2

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!