Leçon 1,
Chapitre 1
En cours
Detection d’objet – Défis Détecter des objets sur une image
In this activity, we will write a code that will perform object detection. The object detection will be performed on an image that we upload on the PictoBlox stage as a backdrop.

The code will move a Square Box on the location of the detected objects, one-by-one and will make the Square box say the class of the object.
Let’s Code
- Open a new file in Pictoblox and choose the Python Coding environment.
- Click on Choose a sprite button (bottom right corner) and select the sprite Square Box.
- In this session, we will perform object detection on the image below:

- For this, first upload this image as a backdrop in the PictoBlox by following the below steps:
- Right click on the above image and choose the save image as option to download it on your computer.
- Hover your mouse over the Choose a Backdrop button (bottom right hand corner) and choose the Upload Backdrop option.
- Select the image you just downloaded and click Open.
- Click on Python (Beta tab).
- Click on Square Box.py from the Project Files. The image is now uploaded as backdrop.
- The sprite object ‘Square Box’ is already initiated by default. Let us import the time library.
sprite = Sprite('Square Box') import time - Let us now also initiate the ObjectDetection object and Pen object.
obj = ObjectDetection() pen = Pen() - Now, we will use pen.clear() to clear any pen markings from previous sessions.
pen.clear() - We will set the threshold of object detection equal to 0.5. This is the level of confidence (50%) we want to keep for detecting the objects.
obj.setthreshold(0.5) - We will also add the following two functions from the ObjectDetection class:
- analysestage(): for analysing the image on stage
- disablebox(): for disabling boxes that appear on the stage, around objects
obj.analysestage() obj.disablebox()
- Now, we want to detect all the objects in the image, one by one and put the Square Box sprite on them. For this, we will iterate through all the objects using the for loop.We will use the following functions for this:
- Inbuilt function range() to loop through all the objects.
- count() function of ObjectDetection class gives us the maximum number of objects detected. We add ‘+1’ since the end parameter of range() function is excluded.
for i in range(1,obj.count()+1):
- Let us set the x-position, y-position and width of the Square Box according to each object. We will use the following functions for the task:
- sprite.setx() – Set the x-position of the sprite (Square Box)
- obj.x(i) – Get the x-position of the object ‘i’ (1 to obj.count())
- sprite.sety() – Set the y-position of the sprite (Square Box)
- obj.y(i) – Get the y-position of the object ‘i’
- sprite.setsize() – Set the size of the sprite (Square Box)
- obj.width(i) – Get the width of the object ‘i’
for i in range(1,obj.count()+1): sprite.setx(obj.x(i)) sprite.sety(obj.y(i)) sprite.setsize(obj.width(i))
- We will also make the Square Box (sprite), say the class of the respective object that it detects:
sprite.say(obj.classname(i)) - We will also use the stamp() function from the Pen class to stamp a Square Box on each object that is detected.
pen.stamp() - Finally, we will add a sleep() function from the time extension, to have some time between detection of each object.
time.sleep(2) - The entire code would be as follows:
sprite = Sprite('Square Box') import time obj = ObjectDetection() pen = Pen() pen.clear() obj.setthreshold(0.5) obj.analysestage() obj.disablebox() for i in range(1,obj.count()+1): sprite.setx(obj.x(i)) sprite.sety(obj.y(i)) sprite.setsize(obj.width(i)) sprite.say(obj.classname(i)) pen.stamp() time.sleep(2) - Press the Run button to test the code.

Extra Images for Object Detection
You can also experiment your code on the below images:


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:
- Click on Browse.
- Search and Select your saved Project file(.sb3) and Click Open.
- Click on Upload to submit the assignment.

The file type allowed is the SB3 file generated from the PictoBlox program. The maximum file size allowed is 5 MB.
Good luck!