AI - Recognize 0's and 1's (Difficulty: 4)
-
Key Topics Covered
Introduction
Many AI programs can recognize handwritten numbers. For example, when we deposit a check using a phone app, it can detect the account number and the amount written on the check just from a picture.
In this tutorial, you’ll build a smart program that can recognize whether a handwritten number is 0 or 1:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/818f8f6b-8798-4c6b-a720-76934d052349.gif" width="520" style="border-radius: 5px; border: 1px solid #29622d;">
Step 1 - Create a New Project
Start by creating a new Scratch project. Then:
- Delete Sprite1 (Cody).
- Rename the “Empty1” sprite to AI.
- Add a new empty sprite named Pen.
The Pen sprite will be used to draw numbers on the screen. The AI sprite will contain the code to guess if the number is a 0 or a 1.
Step 2 - Initialize the “Pen” Extension
Next, please add the “Pen” extension, and set the pen’s color to black, size to 10. We will also erase all existing drawings from the pen.

Step 3 - Draw the Drawing Area
When the project starts, we’ll draw a square that shows the area where users can write their numbers. Use the “draw rectangle” block from the Pen extension (not from the “Looks” category).
- Move the sprite to the center of the stage.
- Set the rectangle’s width and height to 200.
- Set the fill color to 100% transparent.
- Choose any border color you like.

Here’s how it will look:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/1c02fa17-8087-4df9-997e-82111b6027e5.png" width="480" style="border-radius: 5px; border: 1px solid #29622d;">
Step 4 - A Forever Loop To Move the Pen Sprite
We want to let users draw using the mouse. So, we’ll make the Pen sprite follow the mouse pointer.
After drawing the rectangle, use a forever loop to keep the Pen sprite moving to the mouse’s position:

Try it out! Move your mouse around and watch how the Pen sprite changes its x and y positions accordingly:https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/dec78f67-de20-4a7b-b03e-817105ed6767.gif" width="500" style="border-radius: 5px; border: 1px solid #29622d;">
Step 5 - Mouse Down to Draw
To draw using the mouse, we’ll check whether the mouse is clicked (pressed down).
- If the mouse is down, put the pen down.
- If it’s not, lift the pen up.

Now our drawing area is ready for drawing:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/7ca24a52-27d5-48e9-bce6-00567d960cb0.gif" width="500" style="border-radius: 5px; border: 1px solid #29622d;">
Step 6 - The “Clear” Button
To allow the user to clear the drawing and restart, let’s add a “clear” button. Find the “add button” block from the “widget” category, and add the block at the very top:

Run the program again, and you will see the button at the bottom:https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/219e6cca-bc59-4a03-a113-290b5143c87c.png" width="460" style="border-radius: 5px; border: 1px solid #29622d;">
Now add 4 more blocks: when the clear button (“button1”) is clicked, we need to
- Erase all the drawings
- Move the Pen sprite to the center and redraw the rectangle.

Now you can try to draw something and erase it with the clear button.
Step 7 - The “Guess” Button
Add another button named “Guess” in the bottom right corner.

When this button is clicked, we’ll send a message to the AI sprite so it can guess the number:
Step 8 - Prepare the AI Sprite
Now let’s switch to work on the AI sprite. First, please draw a small square in the center of its costume, so we can see where it is on the stage. Make sure it is not too big or too small.

https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/57fb2f53-ce71-42a3-a675-db892e2dc132.png" width="350" style="border-radius: 5px; border: 1px solid #29622d;">
Step 9 - Scan the Middle Row
When the AI sprite gets the “guess” message, it should scan the middle row from left to right.
- Make a variable named x.
- Use a for-loop to set x from -100 to 100, increasing by 5 each step.
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/bfc2988d-ea3e-4708-8474-2f57459a21ce.png" width="400" style="">
Click the “Guess” button to test it:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/a8ec6efd-2a05-447a-bbec-2660e4779008.gif" width="480" style="border-radius: 5px; border: 1px solid #29622d;">
Step 10 - Touching Black Before and After Each Move
Now let’s check if the AI sprite is touching black color (the user’s drawing) at each step.
- Make two variables: “touching before move” and “touching after move”.
- Record their values before and after the sprite moves.
- Add a 1-second wait after each move so you can observe the values.
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/4248c2dc-60bb-426d-8d06-0bffe1b22f14.png" width="500" style="">
Now let’s test it. You should observe the following:
- When the AI sprite is starting from the left, both variables are false;
- When the sprite touches the black color for the first time, “touching before move” remains as false, but “touching after move” becomes “true”.
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/419ff6a0-3f6b-48af-9159-a347c3263625.gif" width="500" style="border-radius: 5px; border: 1px solid #29622d;">
Step 11 - Detecting New Edges
We can now detect the left edges of black strokes using the two variables.
- If the sprite starts touching black after a move, but was not touching black before, that’s a new edge.
- When that happens, make a stamp for testing purposes.
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/a696614e-dfc8-4c18-82d3-615489c93573.png" width="800" style="">
Now let’s test it. No matter how many lines we draw, the AI sprite will make a stamp the first time it touches each line:https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/52fa4288-469a-4f75-ab9f-1fd1ddd91250.gif" width="490" style="border-radius: 5px; border: 1px solid #29622d;">
Step 12 - Counting the Edges
Instead of stamping, let’s count how many times we detect a new edge.
- Make a new variable called “counter”.
- Initialize counter as 0
- Every time we detect a new edge, increase the counter by 1.
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/dcae371d-7b8c-4ffc-985b-3c3e0d320e06.png" width="800" style="">
Run the project. The counter tells how many vertical edges the AI sees—each edge is a transition from white to black.https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/c6b43d2f-f848-4b93-b7f6-fd28be7bda5a.gif" width="500" style="border-radius: 5px; border: 1px solid #29622d;">
Step 13 - 0 or 1?
Now we can guess the number!
- If the user draws a 1, the counter shows 1 edge.
- If they draw a 0, it shows 2 edges.
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/0ef70853-a533-4344-84b0-74ef41cd5360.png" width="300" style="">
So we use the value of the counter to guess:
- If counter = 1 → it’s a “1”
- If counter = 2 → it’s a “0”
(We’re assuming users will only draw 0 or 1.)
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/8d3b2f87-e5dd-46a5-9d58-c194b21432ba.png" width="700" style="">
Please test it a few times:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/ff210950-566c-4320-98bb-7ca141a0ed57.gif" width="500" style="border-radius: 5px; border: 1px solid #29622d;">
Step 14 - Handling Smaller 0’s
When the user draws a very small or narrow “0”, we can only detect one edge, so we will guess incorrectly.

That is because our AI sprite box is touching both strokes at the same time. So to fix it, we simply need to make our AI sprite much smaller, such as a size of 5:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/aabc277e-44fd-48e5-9ec9-5628889752b5.gif" width="500" style="border-radius: 5px; border: 1px solid #29622d;">
Step 15 - Speed Up
Lastly, to speed up the scanning, we can make a new block named “guess”, and run it without screen refresh:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/bbf2bdb4-f074-4a24-912d-87e9f52a12af.png" width="600" style="border-radius: 5px; border: 1px solid #29622d;">
Then we move all the blocks into that block’s definition stack:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/0fcb3cfb-9c82-46dd-aa71-ffd72e073ace.png" width="570" style="">
Now our program runs much faster, because we won’t update the stage when the AI sprite moves across the stage:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/818f8f6b-8798-4c6b-a720-76934d052349.gif" width="520" style="border-radius: 5px; border: 1px solid #29622d;">
Additional Challenges
This tutorial shows a very common technique that analyzes an image using edge detection. As a challenge, please try to extend this program to recognize some other numbers. You need some smart ideas to handle them in a robust way.
For example, if you are trying to recognize the number “6”, you will need to scan all rows, not just the middle row. Then you can check if the number of edges is 1 at the top, becomes 2 towards the bottom, and then reverts to 1 at last.
Similarly, if you are trying to recognize the number “8”, you can check if the number of edges is 2 at the top and bottom, but 1 in the middle.
Since the user might draw the number in the top or bottom part of the box, you may also need a preparation step, where you scan the image to find out the range of Y positions where there is any drawing. Then you can only focus on those rows.
Here is a demo of an enhanced version of the project:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/6770197a-efac-4743-a8d3-2f3bf725fd41.gif" width="460" style="border-radius: 5px; border: 1px solid #29622d;">
-
I CreatiCode pinned this topic on
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login