Navigation

    CreatiCode Scratch Forum

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • CreatiCode

    AI - Counting Fingers with Hand Detection (Difficulty 3)

    Tutorials
    1
    1
    991
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • info-creaticode
      CreatiCode last edited by admin

       

      Key Topics Covered

       

      • Using variables
      • Reading from tables
      • AI for Hand Detection

       
       

      Introduction

       

      In this project, you’ll build a fun and interactive finger-counting program using AI-powered hand detection. The program will detect how many fingers are stretched out and display that number on the screen.

      07c13483-a83b-4595-be32-dfa45fb875f3.gif

       
      Note: While it’s possible to use the visual capabilities of large language model AIs (see this tutorial), there’s a noticeable delay in receiving the recognition results. For real-time hand tracking, it’s essential to use the dedicated hand detection AI block from this tutorial.

       
       

      Step 1 - A New Project

       

      Please create a new project in the CreatiCode playground. Remove the sprites named “Sprite1”. We will only need to use the “Empty” sprite.

       
       

      Step 2 - Add Number Costumes

       

      We’ll show the detected finger count by switching between number costumes.

      • Go to the Costumes tab.
      • Add the costume “Glow-0” and delete the original costume.

      dffcc7ff-29e4-45d1-88cb-4f4490a49134.gif

       
      Now add “Glow-1” through “Glow-5”, so you have 6 costumes total.

      • Costume #1 = Number 0
      • Costume #6 = Number 5

      d2a72caf-64e9-49f1-8b2b-1c84d0c7df60.png

       
       

      Step 3 - Start Hand Detection

       

      Now let’s switch to the “Code” tab, and add the hand detection block from the “AI” category.

      0d65cfc5-d1d2-4141-b597-8b5950369323-image.png

       
      Click the green flag to activate the camera and begin detecting hands.

       
      2c45d310-35e8-476c-b26c-f1c61cdb8c15.gif

       
      As shown, each detected hand is represented by 21 landmark points: 1 for the wrist, and 4 for each finger.

       
       

      Step 4 - Review the Data Table

       

      When the hand detection block is added, a new table (named “i”) is created automatically.

      • Go to the Variables category.
      • Check the box next to table “i” (the name of the output table variable) to show it on the stage.
         
        99491fe7-3db9-4cee-b4d9-7cd56606456a.gif

       
       
      Now the table updates live with hand tracking data.

      • Each hand generates 47 rows of data.
      • You’ll see a new set of 47 rows added for each additional hand detected.
         
        b1b35179-d8eb-4eb4-9e8f-5d61afcee58c.gif

       
       

      Step 5 - Focus on the “Curl” Angle

       

      To determine if a finger is stretched out, we just need to check its curl angle, found in the first 5 rows of the table.

      • A stretched finger has a curl angle near 180 degrees.
      • A bent finger has a curl angle closer to 0.

      Try curling your index finger and watching the change in value:

      1.gif

       
      🧠 Tip: For simplicity, if the curl angle is greater than 150, we will treat that finger as stretched.

       
       

      Step 6 - Create a Counter Variable

       

      To count how many fingers are stretched, create a new variable named “counter”.
      This variable will store the total number of stretched fingers.

       
       

      Step 7 - Use a Forever Loop

       

      We want to continuously update the count in real time. Wrap the logic inside a forever loop, and reset the counter to 0 each time.

      a1cf27fc-102d-4e6a-a5ae-5e60aa81388e-image.png

       
       

      Step 8 - Check If a Hand Is Detected

       

      We should only check the data in the table if the camera sees at least one hand.

      • Each hand adds 47 rows to the table.
      • Use the “row count” block to check if the table has 47 or more rows.
         
        356c34cb-b788-4edf-b688-1306a1fb3c2f-image.png

       
       

      Step 9 - Loop Through Rows 1 to 5

       

      Create a variable called “row”, and use a for loop to go from 1 to 5, which represent the five fingers.
       
      6a8e12d2-ff26-419e-93e2-82c5f22b7561-image.png

       
       

      Step 10 - Read the “curl” Value for Each Row

       

      We can read the value in the “curl” column for any row like this:

      91cb82ea-9d39-43b1-a14f-cc79e1b43813.gif

       
      To read all 5 rows one by one, we just need to use the “row” variable to specify the row number in the block:

      2c39ff15-268a-4cc0-9402-008acd639af5-image.png

       
       

      Step 11 - Count the Stretched Fingers

       

      If the curl value is greater than 150, increase the counter by 1.

       
      c1692e1b-24f6-4ec7-9e21-df1829ccc3ad-image.png

       
      After the loop runs through all 5 rows, “counter” will store the number of fingers that are stretched out.

       
       

      Step 12 - Show the Count Using Costumes

       

      We’re almost done! Now, just switch to the correct costume based on the counter value.

      • Costume 1 = number 0
      • Costume 2 = number 1
      • Costume 3 = number 2
      • … and so on.

      So if “counter” is 2, we should show costume 3, which means the costume number is always counter + 1.
       
      0a9f98ae-795b-4e6e-8f35-3b39f7e63916-image.png

       
      Here is what you should get:

      07c13483-a83b-4595-be32-dfa45fb875f3.gif

       
       

      Additional Challenges

       

      This finger-counting program is very simple. There are a lot of places where you can make improvements. Here are some ideas:

      • Recognize Higher Numbers: When you show the number “6”, currently the program still reports “2”, since 2 fingers are stretched out. Can you make the program smarter? How about other digits like 7, 8 and 9?

      1e1d99b0-e320-4773-b99b-4052145bd3e3.png

      • Detect 2 Hands: You can try to recognize 2-digit numbers with 2 hands

      • Recognize Other Gestures: Besides numbers, you can use the “curl” angle to help recognize other hand gestures, such as “OK”, “Peace”, “Thumb Up”.

      03ff1bb4-e64c-4216-a3b6-3648d1bd35da.png

      1 Reply Last reply Reply Quote 0
      • Pinned by  info-creaticode info-creaticode 
      • First post
        Last post