Table of Contents

say()

Function Definition: say(message = "Hello!", time)

Parameters

NameTypeDescriptionExpected ValuesDefault Value
messagestringThe text you want to write.String"Hello!"
timeintTime for which the message is displayed. If value is 0 then the sprite will say until another speech or thought block is activated, or the stop sign is pressed.Integer0

Description

The function gives its sprite a speech bubble with the specified text — the speech bubble stays until another speech or thought block is activated, or the stop sign is pressed.

Example

The example demonstrates how to use say and think of sprite in a project.

Code

sprite = Sprite('Tobi')
import time

sprite.say("Saying for 2 seconds", 2)
sprite.say("Saying forever")
time.sleep(2)

sprite.think("Thinking for 2 seconds", 2)
sprite.think("Thinking forever")

Output

Read More
The example demonstrates the costume change in PictoBlox.

Code

sprite = Sprite('Tobi')

sprite.gotoxy(0, 0)
sprite.setsize(200)
sprite.switchcostume("Tobi")

while True:
  sprite.say(sprite.costume("name"), 1)
  sprite.nextcostume()

Output

Read More
The example demonstrates how to use a repeat block to recite a table in PictoBlox.

Code

sprite = Sprite('Tobi')

sprite.input("Which table you want to recite")
number = int(sprite.answer())

for i in range(1, 10):
  sprite.say(str(number) + " x " + str(i) + " = " + str(number*i), 1)

Output

Read More
Learn about AI-based face expression detection, a technology that uses artificial intelligence algorithms and computer vision techniques to analyze images or videos of human faces and recognize emotions or expressions.

Introduction

AI-based face expression detection refers to the use of artificial intelligence algorithms and computer vision techniques to analyze images or videos of human faces and recognize the emotions or expressions being displayed. The technology can detect and analyze subtle changes in facial features, such as eye movement, mouth shape, and eyebrow position, to determine whether a person is happy, sad, angry, surprised, or expressing other emotions.

Discover the various fields that utilize this technology, including psychology, marketing, and human-computer interaction. Additionally, read about the logic and code behind face detection with a camera feed, including the initialization of parameters, face detection library, loop execution, and if-else conditions. Explore how the technology continuously analyzes emotions, and how the humanoid responds with different facial expressions and movements.

Code

sprite = Sprite('Tobi')

fd = FaceDetection()

quarky = Quarky()

import time

humanoid = Humanoid(7, 2, 6, 3, 8, 1)

# Turn the video ON with 0% transparency
fd.video("ON", 0)
fd.enablebox()

# Run this script forever
while 1:
  fd.analysecamera()          # Analyse image from camera 
  sprite.say(fd.expression()) # Say the face expressions
  
  if fd.isexpression(1, "happy"): # if face expression is happy
    quarky.showemotion("happy")   # show happy emotion on Quarky
    humanoid.action("dance2", 1000, 1)
    
  if fd.isexpression(1, 'sad'):
    quarky.showemotion("crying")
    humanoid.action("updown", 1000, 1)
    
  if fd.isexpression(1, 'surprise'):
    quarky.showemotion('surprise')
    humanoid.action("moonwalker", 1000, 1)
    
  if fd.isexpression(1, 'angry'):
    quarky.showemotion('angry')    
    humanoid.action("flapping2", 1000, 1)
  else:
    humanoid.home()
    
# Comment the above script, uncomment the below script and 
# run this script to clear the stage and quarky display

fd.disablebox()
fd.video("off")    
quarky.cleardisplay()

Logic

The example demonstrates how to use face detection with a camera feed. Following are the key steps happening:

  1. The code is using face detection to recognize facial expressions and control a humanoid and a display device called Quarky accordingly.
  2. Then, the program turns on the video with 0% transparency and enables the bounding box for face detection.
  3. The code then enters an infinite loop where it continuously analyzes the image from the camera using face detection and says the detected facial expressions.
  4. The code then checks if the expression is happy, sad, surprised, or angry using the if statement. If the expression is happy, the Quarky device displays a happy emotion, and the humanoid performs the “dance2” action for specific time. Similarly, for sad, surprised, and angry expressions, Quarky displays the respective emotion, and the humanoid performs the associated action.
  5. If no facial expression is detected, the humanoid is set to its “home” position. Finally, if the program needs to be stopped.

Output

Read More
Learn how to create a Machine Learning Model to count nuts and bolts from the camera feed or images. See how to open the ML environment, collect and upload data, label images, train the model, and export the python script.

Introduction

In this example project we are going to explore how to create a Machine Learning Model which can count the number of nuts and bolts from the camera feed or images. You will learn how to open the ML environment, collect and upload data, label images, train the model, and export the Python script.

Object Detection in Machine Learning Environment

Object Detection is an extension of the ML environment that allows users to detect images and make bounding boxes into different classes. This feature is available only in the desktop version of PictoBlox for Windows, macOS, or Linux. As part of the Object Detection workflow, users can add classes, upload data, train the model, test the model, and export the model to the Block Coding Environment.

Opening Image Detection Workflow

Alert: The Machine Learning Environment for model creation is available in the only desktop version of PictoBlox for Windows, macOS, or Linux. It is not available in Web, Android, and iOS versions.

Follow the steps below:

  1. Open PictoBlox and create a new file.
  2.  Select the Python Coding Environment.
  3. Select the “Open ML Environment” option under the “Files” tab to access the ML Environment.
  4. You’ll be greeted with the following screen.
    Click on “Create New Project”.
  5. A window will open. Type in a project name of your choice and select the “Object Detection” extension. Click the “Create Project” button to open the Object Detection window.

You shall see the Object Detection workflow. Your environment is all set.

Collecting and Uploading the Data

The left side panel will give you three options to gather images:

  1. Using the Webcam to capture images.
  2. Uploading images from your device’s hard drive.
  3. Downloading images from a repository of images.

Uploading images from your device’s hard drive

  1. Now it’s time to upload the images which you downloaded from another source or captured from your camera. Click on the “Select from device” option from the Import Images block.
  2. Now click on the “Choose images from your computer” and go to the folder where you downloaded your images.
  3. Select all images which you want to upload then click on “open” option.
  4. Now page of PictoBlox looks like this:

Making Bounding Box – Labelling Images

A bounding box is an imaginary rectangle that serves as a point of reference for object detection and creates a collision box for that object.

We draw these rectangles over images, outlining the object of interest within each image by defining its X and Y coordinates. This makes it easier for machine learning algorithms to find what they’re looking for, determine collision paths and conserves valuable computing resources.

  1. Labeling is essential for Object Detection. Click on the “Bbox” tab to make the labels.

    Notes:

    1. Notice how the targets are marked with a bounding box. The labels appear in the “Label List” column on the right.
    2. A single image can have multiple targets. Every target must be enclosed in a bounding box.
    3. The bounding boxes are color coded.
  2. To create the bounding box in the images, click on the “Create Box” button, to create a bounding box. After the box is drawn, go to the “Label List” column and click on the edit button, and type in a name for the object under the bounding box. This name will become a class. Once you’ve entered the name, click on the tick mark to label the object.

  3. Options in Bounding Box:

    1. Auto Save: This option allows you to auto-save the bounding boxes with the labels they are created. You do not need to save the images every time this option is enabled.
    2. Manual Save: This option disables the auto-saving of the bounding boxes. When this option is enabled you have to save the image before moving on to the next image for labeling.
    3. Create Box: This option starts the cursor on the images to create the bounding box. When the box is created, you can label it in the Label List.
    4. Save Box: This option saves all the bounding boxes created under the Label List.
  4. File List: It shows the list of images available for labeling in the project.
  5. Label List: It shows the list of Labels created for the selected image.
  6. Class Info: It shows the summary of the classes with the total number of bounding boxes created for each class.
  7.   You can view all the images under the “Image” tab.

Training the Model

In Object Detection, the model must locate and identify all the targets in the given image. This makes Object Detection a complex task to execute. Hence, the hyperparameters work differently in the Object Detection Extension.

  1. Go to the “Train” tab. You should see the following screen:
  2. Click on the “Train New Model” button.
  3. Select all the classes, and click on “Generate Dataset”.
  4.  Once the dataset is generated, click “Next”. You shall see the training configurations.
  5. Observe the hyperparameters.
    1. Model name- The name of the model.
    2. Batch size- The number of training samples utilized in one iteration. The larger the batch size, the larger the RAM required.
    3. The number of iterations- The number of times your model will iterate through a batch of images.
    4. Number of layers- The number of layers in your model. Use more layers for large models.
  6. Specify your hyperparameters. If the numbers go out of range, PictoBlox will show a message.
  7. Click “Create”, It creates a new model according to inserting the value of the hyperparameter.
  8. Click “Start Training”, If desired performance is reached, click on the “Stop” 
    1. Total Loss
    2. Regularization Loss
    3. Localization Loss
    4. Classification Loss
  9. After the training is completed, you’ll see four loss graphs: 
    Note: Training an Object Detection model is a time taking task. It might take a couple of hours to complete the training.
  10. You’ll be able to see the graphs under the “Graphs” panel. Click on the buttons to view the graph.

    1. The graph between “Total loss” and “Number of steps”
    2. The graph between “Regularization loss” and “Number of steps”.
    3. The graph between “Localization” and “Number of steps”.
    4. The graph between “Classification loss” and “Number of steps”.

Evaluating the Model

Now, let’s move to the “Evaluate” tab. You can view True Positives, False Negatives, and False Positives for each class here along with metrics like Precision and Recall.

Testing the Model

The model will be tested by uploading an Image from the device:

Export in Python Coding

    1. Click on the “PictoBlox” button, and PictoBlox will load your model into the Python Coding Environment if you have opened the ML Environment in the Python Coding.
    2. Make a new folder(name is img) in Python script and add a number of images for testing.
      |
    3. Also, add the same images in the backdrop and delete the default backdrop.
    4. Change in Python script according to requirement.

Code

####################imports####################
# Do not change
sprite = Sprite('Tobi')
sprite1 = Sprite('Stage')
import cv2
import numpy as np
import tensorflow.compat.v2 as tf
import os
import time
# Do not change
####################imports####################

#Following are the model and video capture configurations
# Do not change

detect_fn = tf.saved_model.load(
    "saved_model")

# cap = cv2.VideoCapture(0)                                          # Using device's camera to capture video
# if (cap.isOpened()==False):
#   print("Please change defalut value of VideoCapture(k)(k = 0, 1, 2, 3, etc). Or No webcam device found")

folder="img"
for filename in os.listdir(folder):
  img = cv2.imread(os.path.join(folder,filename))
  if img is not None:
    x=filename
    

    a=0
    b=0
    y=x.split('.')
   
      
  
    font = cv2.FONT_HERSHEY_SIMPLEX
    fontScale = 1
    color_box = (50,50,255)
    color_text = (255,255,255)
    thickness = 2
    
    
    class_list = ['Bolt','Nut',]                        # List of all the classes 
  
  #This is the while loop block, computations happen here
    image_np=img
    height, width, channels = image_np.shape                       # Get height, wdith  
    image_resized = cv2.resize(image_np,(320,320))                   # Resize image to model input size  
    image_resized = cv2.cvtColor(image_resized, cv2.COLOR_BGR2RGB) # Convert bgr image array to rgb image array  
    input_tensor = tf.convert_to_tensor(image_resized)             # Convert image to tensor
    input_tensor = input_tensor[tf.newaxis, ...]                   # Expanding the tensor dimensions
    
    detections = detect_fn(input_tensor)                           #Pass image to model
    
    num_detections = int(detections.pop('num_detections'))         #Postprocessing
    detections = {key: value[0, :num_detections].numpy() for key, value in detections.items()}
    detections['num_detections'] = num_detections
    detections['detection_classes'] = detections['detection_classes'].astype(np.int64)
    width=320
    height=320
    # Draw recangle around detection object
    for j in range(len(detections['detection_boxes'])):
      # Set minimum threshold to 0.3
      if(detections['detection_scores'][j] > 0.3):
        # Starting and end point of detected object
        starting_point = (int(detections['detection_boxes'][j][1]*width),int(detections['detection_boxes'][j][0]*height))
        end_point = (int(detections['detection_boxes'][j][3]*width),int(detections['detection_boxes'][j][2]*height))
        # Class name of detected object
        className = class_list[detections['detection_classes'][j]-1]
        # Starting point of text
        print(className)
        if(className=="Bolt"):
          a=a+1
        elif(className=="Nut"):
          b=b+1
        starting_point_text = (int(detections['detection_boxes'][j][1]*width),int(detections['detection_boxes'][j][0]*height)-5)
        # Draw rectangle and put text
        image_resized = cv2.rectangle(image_resized, starting_point, end_point,color_box, thickness)
        image_resized = cv2.putText(image_resized,className, starting_point_text, font,fontScale, color_text, thickness, cv2.LINE_AA)
        # Show image in new window
    cv2.imshow("Detection Window",image_resized)
    sprite1.switchbackdrop(y[0])
    print(a)
    print(b)

    if cv2.waitKey(25) & 0xFF == ord('q'):                          # Press 'q' to close the classification window
      break
    time.sleep(2)
    sprite.say("Total number of bolts is "+str(a))
    time.sleep(1)
    sprite.say("Total number of Nuts is "+str(b))
    time.sleep(5)
    sprite.say("")
cv2.waitKey(5)                                               
cv2.destroyAllWindows()                                             # Closes input window

Logic

The example demonstrates how to count nuts and bolts from an image of a stage. Following are the key steps happening:

  1. Creates a sprite object named “Tobi”. A sprite is typically a graphical sprite that can be animated or displayed on a screen.
  2. Creates another sprite object named ‘Stage’. It represents the backdrop.
  3. Imports the ‘time’ module, which provides functions to work with time-related operations using import time.
  4. Imports the ‘os’ module, which provides functions to work with outside folders and images inside them.
  5. Make a new folder in the project files and upload testing images from your computer, as well as the same images in the backdrop.
  6. One by one, go through all uploaded images using the ‘for’ loop.
  7. Store images in the variable ‘img’ and their labels in the variable ‘filename’.
  8. Initialize two count variables named ‘a’ and ‘b’ with a 0 value, which stores the count of nuts and bolts in the image.
  9. Split the label of the image and store only the name of the image in a variable (name y), which is used to change the backdrop.
  10. Define the new height (which is equal to 320) and width (which is equal to 320) of the detection window so we can easily see bounding boxes on the detected image.
  11. Write a condition in the code to count the number of nuts and bolts.
  12. If a nut is detected, it will increase the count of the nut variable (a), and if a bolt is detected, it will increase the count of the bolt variable (b).
  13. Switch backdrop according to image name using a predefined function (sprite1.switchbackdrop()) for changing backdrop in PictoBlox.
  14. Show an updated detection window in the output, which contains bounding boxes for detected objects.
  15. Write a predefined function (sprite.say()) by which ‘Tobi’ will say the number of nuts and bolts.
  16. Also adds waiting time so we can clearly see the output of the model.

Final Result

Conclusion

Creating a Machine Learning Model to count nuts and bolts can be both complex and time-consuming. Through the steps demonstrated in this project, you can create your own Machine Learning Model that can detect and count nuts and bolts in an image. Once trained, you can export the model into the Python Coding Environment, where you can tweak it further to give you the desired output. Try creating a Machine Learning Model of your own today and explore the possibilities of Object Detection in PictoBlox!

Read More
The example demonstrates the sprite direction in Python.

Code

sprite = Sprite('Arrow1')

sprite.gotoxy(0, 0)
sprite.setsize(300)

while True:
  if sprite.iskeypressed("left arrow"):
    sprite.left(3)
  if sprite.iskeypressed("right arrow"):
    sprite.right(3)
  sprite.say("Direction is " + str(sprite.direction()))

Output

Read More
The Language Translator with ChatGPT is a powerful system that enables real-time translation and conversation support, facilitating multilingual communication.

Introduction

The Language Translator with ChatGPT and Speech Recognition is a system that helps people communicate across languages by providing real-time translation and conversation support. It combines language translation, chatbot capabilities, and speech recognition to facilitate multilingual communication.

Language Translator Using ChatGPT is a project that trains the ChatGPT language model with multilingual data to enable it to understand and translate text between different languages. It utilizes ChatGPT’s natural language processing abilities to generate human-like responses, making it ideal for building a language translation system. The training data includes sentence pairs in different languages and their corresponding translations.

Logic

The code represents a conversation between the sprite character “Tobi” and the AI models. The sprite asks the user for a definition, the user responds, the AI generates a result based on the response, and the sprite says and speaks the result.

Follow the steps below:

  1. Open PictoBlox and create a new file.
  2. Choose a suitable coding environment for block-based coding.
  3. Add the extensions to your project from the extension palette located at the bottom right corner of PictoBlox.
  4. We create an instance of the Text to Speech class called speech. This class allows us to convert text into spoken audio.
  5. Next, we create an instance of the ChatGPT model called gpt. ChatGPT is a language model that can generate human-like text responses based on the input it receives. 
  6. The sprite asks the user to provide a sentence by using the input() function and stores the input in the variable ‘l‘.
  7. The translatelanguage() method of the ‘gpt‘ object is then used to translate the sentence stored in ‘l’ into Hindi. The translated sentence is stored in the variable ‘result‘.
  8. The sprite uses the say() method say() to speak out the translated sentence stored in ‘result’.
  9. The speak() method of the speech() object is called to convert the translated sentence into audio and play it.

Code

sprite = Sprite('Tobi')
gpt = ChatGPT()
speech = TexttoSpeech()

sprite.input("Provide a Sentece i will traslated into Hindi ")
l = str(sprite.answer())

gpt.translatelanguage(l,"hindi")
result=gpt.chatGPTresult()

sprite.say(result,2)
speech.speak(result)

Output

Read More
Convert any word or phrase into a delightful sequence of emojis with our Emoji Converter.

Introduction

Are you looking to add some fun and expressiveness to your conversations? Look no further! I’m here to help you convert any word or phrase into a colorful array of emojis. Whether you want to spice up your messages, or social media posts, or simply bring a smile to someone’s face, I’ve got you covered.

Just type in the word or phrase you want to transform, and I’ll generate a delightful sequence of emojis that capture the essence of your text. Emojis are a universal language that transcends words from happy faces to animals, objects, and everything in between.

So, let’s get started and infuse your text with a touch of emoji magic! 🎉🔥

Logic

This code allows the user to interact with the sprite and provide emojis, which are then transformed into a response using the ChatGPT model. The sprite then speaks the generated response using the provided emojis.

  1. Open PictoBlox and create a new file.
  2. Choose a suitable coding environment for python-based coding.
  3. Define a sprite , Tobi.
  4. Then, we create an instance of the ChatGPT model using the ChatGPT class.
  5. The sprite asks the user to input the emojis they want to use by calling the input method.
  6. The sprite uses its answer method to get the user’s response, which is then converted to a string using str().
  7. The movieToemoji method of the ChatGPT model converts the user’s response into emojis.
  8. The chatGPTresult method retrieves the result of the ChatGPT conversation.
  9. Finally, the sprite says the result for 5 seconds using the said method.

Code

sprite = Sprite('Tobi')
gpt = ChatGPT()

sprite.input("Please let me know which emojis you'd like me to use by typing them here.")
answer= str(sprite.answer())

gpt.movieToemoji(answer)
result=gpt.chatGPTresult()

sprite.say(result,5)

Output

Read More
Learn about AI-based face expression detection, computer vision techniques to analyze images or videos of human faces and recognize emotions or expressions.

Introduction

AI-based face expression detection refers to the use of artificial intelligence algorithms and computer vision techniques to analyze images or videos of human faces and recognize the emotions or expressions being displayed. The technology can detect and analyze subtle changes in facial features, such as eye movement, mouth shape, and eyebrow position, to determine whether a person is happy, sad, angry, surprised, or expressing other emotions.

Discover the various fields that utilize this technology, including psychology, marketing, and human-computer interaction. Additionally, read about the logic and code behind face detection with a camera feed, including the initialization of parameters, face detection library, loop execution, and if-else conditions. Explore how the technology continuously analyzes emotions, and how the Humanoid responds with different facial expressions and movements.

Code

sprite = Sprite('Tobi')
fd = FaceDetection()
quarky = Quarky()
import time

humanoid = Humanoid(7, 2, 6, 3, 8, 1)

# Turn the video ON with 0% transparency
fd.video("ON", 0)
fd.enablebox()

# Run this script forever
while 1:
  fd.analysecamera()          # Analyse image from camera 
  sprite.say(fd.expression()) # Say the face expressions
  
  if fd.isexpression(1, "happy"): # if face expression is happy
    quarky.showemotion("happy")   # show happy emotion on Quarky
    humanoid.action("dance2", 1000, 1)
    
  if fd.isexpression(1, 'sad'):
    quarky.showemotion("crying")
    humanoid.action("updown", 1000, 1)
    
  if fd.isexpression(1, 'surprise'):
    quarky.showemotion('surprise')
    humanoid.action("moonwalker", 1000, 1)
    
  if fd.isexpression(1, 'angry'):
    quarky.showemotion('angry')    
    humanoid.action("flapping2", 1000, 1)
  else:
    humanoid.home()
    
# Comment the above script, uncomment the below script and 
# run this script to clear the stage and quarky display

fd.disablebox()
fd.video("off")    
quarky.cleardisplay()

Logic

The example demonstrates how to use face detection with a camera feed. Following are the key steps happening:

  1. Creates a sprite object named ‘Tobi’. A sprite is typically a graphical element that can be animated or displayed on a screen.also creates a Quarky object.
  2. Creates a face detection object named ‘fd’. This object is responsible for detecting faces in images or video using fd = FaceDetection()
  3. Imports the ‘time’ module, which provides functions to work with time-related operations using import time.
  4.  Creates a humanoid object with specific pins assigned to control various actions of the humanoid robot.
  5.  Turns on the video display with 0% transparency for the face detection module using fd.video(“ON”, 0).
  6.  Enables the face detection module to draw boxes around detected faces using fd.enablebox().
  7. The code enters an infinite loop using while 1, which means it will keep running indefinitely until interrupted.
  8. Analyzes the image from the camera for face detection using fd.analysecamera().
  9. The sprite says the detected face expressions obtained from the face detection module using sprite.say(fd.ex * pression()).
  10. The code checks for different face expressions using if statements and performs corresponding actions.
  11. For example, if the face expression is determined to be “happy“, the Quarky device shows a “happy” emotion, and the humanoid performs a dance action.
  12. Similarly, other face expressions like “sad”, “surprised”, and “angry” trigger specific emotional displays on Quarky and corresponding actions on the humanoid.
  13. If none of the predefined face expressions match, the humanoid goes back to its default or “home” position.

Output

Read More
Convert any word or phrase into a delightful sequence of emojis with our Emoji Converter.

Introduction

Are you looking to add some fun and expressiveness to your conversations? Look no further! I’m here to help you convert any word or phrase into a colorful array of emojis. Whether you want to spice up your messages, or social media posts, or simply bring a smile to someone’s face, I’ve got you covered.

Just type in the word or phrase you want to transform, and I’ll generate a delightful sequence of emojis that capture the essence of your text. Emojis are a universal language that transcends words from happy faces to animals, objects, and everything in between.

So, let’s get started and infuse your text with a touch of emoji magic! 🎉🔥

Logic

This code allows the user to interact with the sprite and provide emojis, which are then transformed into a response using the ChatGPT model. The sprite then speaks the generated response using the provided emojis.

  1. Open PictoBlox and create a new file.
  2. Choose a suitable coding environment for Block-based coding.
  3. Define a sprite , Tobi.
  4. Then, we create an instance of the ChatGPT model using the ChatGPT class.
  5. The sprite, named Tobi, asks for a world that can be converted into emojis by using the command sprite.input(“Please provide a world that I can convert into emojis”).
  6. After receiving the input, Tobi uses the answer() function to generate a response based on the provided world.
  7. Next, the language model, ChatGPT, is involved. It has a function called movieToemoji() that takes the generated response from Tobi and performs some operation related to converting a movie into emojis.
  8. Finally, the result of the operation performed by ChatGPT is stored in the variable result. Tobi then uses the command sprite.say(result, 5) to display the result for 5 seconds.
  9. In summary, the code represents a scenario where Tobi the sprite asks for a world, ChatGPT processes the input and performs some operation related to movies and emojis, and Tobi displays the result.

Code

sprite = Sprite('Tobi')
gpt = ChatGPT()


sprite.input("Please provide a world that i can convert into an emojis")
answer=sprite.answer()

gpt.movieToemoji(answer)
result=gpt.chatGPTresult()
sprite.say(result,5)

Output

Read More
Welcome to the Noun Detector, a powerful tool that utilizes ChatGPT and the spaCy library to identify and extract nouns from text.

Introduction

Welcome to the Noun Detector! This powerful tool utilizes the capabilities of ChatGPT and leverages the spaCy library to identify and extract nouns from text. By employing advanced natural language processing techniques, the Noun Detector analyzes sentences and highlights the essential elements that represent people, places, objects, or concepts.

Noun Detector is designed to excel at identifying and extracting nouns from text. Experience the Noun Detector’s capabilities firsthand and unlock the power of noun extraction in your language-processing endeavors. Try it out and witness the precision and efficiency of this invaluable tool!

Code

sprite = Sprite('Tobi')

quarky=Quarky()
gpt = ChatGPT()

gpt.askOnChatGPT("AIAssistant", "Genrate simple random sentence for me")
result=gpt.chatGPTresult()
gpt.getgrammerfromtext("GrammerNoun",result)
noun=gpt.chatGPTresult()

sprite.say(result,5)
print(result)
print(noun)


sprite.input("Indentify and write the noun in sentance")
answer= str(sprite.answer())

if the answer in noun:
  sprite.say("You have a strong understanding of noun concepts. Well done!",5)
else:
  sprite.say("Please check the terminal for the correct answer as your response is incorrect",5)

Logic

  1. Open PictoBlox and create a new file.
  2. Choose a suitable coding environment for block-based coding.
  3. We have a sprite character named Tobi.
  4. Add the ChatGPT extensions to your project from the extension palette located at the bottom right corner of PictoBlox.
  5. We will ask the AI assistant to create a random sentence for us.
  6. The AI assistant will generate the sentence and identify the nouns in it.
  7. Tobi will then say the generated sentence out loud for 5 seconds. The sentence and the identified nouns will be displayed on the screen.
  8. Next, Tobi will ask you to identify and write the noun in the sentence. You need to type your answer.
  9. If your answer matches any of the identified nouns, Tobi will appreciate you.
  10. But if your answer is incorrect, Tobi will say to check the terminal.
  11. So, give it a try and see if you can identify the noun correctly!

Output

Read More
Expand your vocabulary and enhance your writing with the Synonyms and Antonyms Word Converter.

Introduction

The Synonyms and Antonyms Word Converter is a powerful tool powered by the ChatGPT extension that allows users to effortlessly find synonyms and antonyms for words. It harnesses the capabilities of the advanced language model to provide accurate and contextually relevant word alternatives.

With the Synonyms and Antonyms Word Converter, you can expand your vocabulary, enhance your writing, and improve your communication skills. Whether you’re a writer seeking more expressive language or a student looking to diversify your word choices, this tool is designed to assist you in finding suitable alternatives quickly and easily.

Using the ChatGPT extension, the Synonyms and Antonyms Word Converter engage in interactive conversations, making it an intuitive and user-friendly tool. By providing a word as input, you can receive a list of synonyms or antonyms, depending on your preference, helping you to diversify your language and convey your ideas with precision.

Code

sprite = Sprite('Tobi')
gpt = ChatGPT()
str2 = ""
var2=""

sprite.input("Please provide a word for which you would like to find synonyms and antonyms")
answer= str(sprite.answer())

gpt.getsynonymsAntonymsfromText("Synonyms",answer)
str1=gpt.chatGPTresult()


for i in str1:
    if not i.isdigit():
        str2 += i
        
print("Synonyms words are:", str2)

gpt.getsynonymsAntonymsfromText("Antonyms",answer)
var1=gpt.chatGPTresult()

for j in var1:
    if not j.isdigit():
        var2 += j
        
print("Antonyms words are:", var2)

Logic

  1. Open PictoBlox and select the environment as appropriate Python Coding Environment.
  2. Create a new file.
  3. Select the environment as appropriate Python Coding Environment.
  4. First, an instance of the Sprite class is created, with the name “Tobi”.
  5. To add the ChatGPT extension, click on the extension button located as shown in the image. This will enable the ChatGPT extension, allowing you to incorporate its capabilities into your project.
  6. Two empty strings, str2 and var2, are declared to store the resulting synonyms and antonyms, respectively.
  7. The user is prompted to provide a word for which they want to find synonyms and antonyms using the input() method from the Sprite library.
  8. The user’s input is stored in the answer variable as a string.
  9. The getsynonymsAntonymsfromText() method is called on the gpt object to find synonyms for the provided word. The category “Synonyms” is specified.
  10. The resulting synonyms are obtained from gpt.chatGPTresult() and stored in the str1 variable.
  11. The code then iterates over each character in str1 and appends non-digit characters to str2, filtering out any numerical values.
  12. Finally, the code prints the extracted synonyms stored in str2.
  13. The process is repeated for finding antonyms, where the getsynonymsAntonymsfromText() method is called with the category “Antonyms“, and the resulting antonyms are stored in the var1 variable.
  14. Non-digit characters are extracted and stored in var2, which contains the antonyms.
  15. The code concludes by printing the extracted antonyms stored in var2.
  16. Press Run to run the code.
  17. Sprite Tobi asks for the word you want synonyms/antonyms for.
  18. Go to the terminal. the terminal will display the synonyms and antonyms for a word.

Output

Read More
Engage in interactive conversations with the AI assistant powered by ChatGPT and Sprite Tobi.

Introduction

The Chatbox with ChatGPT Extension is a versatile tool that enables developers to integrate AI-driven conversations into their applications. It leverages the power of the ChatGPT model to create interactive and intelligent chat experiences. With this extension, you can build chatbots, virtual assistants, or conversational agents that understand and respond to user inputs naturally.

The code creates a character named “Tobi” and uses speech recognition to understand spoken commands. It then asks a question to the AI assistant (ChatGPT) and displays the response on the screen, converts it into speech, and makes the character “Tobi” speak the response.

Code

sprite = Sprite('Tobi')
gpt = ChatGPT()

speech = TexttoSpeech()

sr = SpeechRecognition()
ts = TexttoSpeech()
sr.analysespeech(4, "en-US")
command = sr.speechresult()
answer = command.lower()

# sprite.input("Provide a valid word")
# answer= str(sprite.answer())


gpt.askOnChatGPT("AIAssistant", answer)
# Ask on chatGPT other OPTION "friendAI" "sarcasticAI"
result=gpt.chatGPTresult()
print(result)
speech.speak(result)

sprite.say(result,5)

Logic

  1. Open PictoBlox and create a new file.
  2. Choose a suitable coding environment for Block-based coding.
  3. We create an instance of  Speech recognition. This class allows us to convert spoken audio into text.
  4. Next, we create an instance of the ChatGPT model called gpt. ChatGPT is a language model that can generate human-like text responses based on the input it receives. 
  5. Recognize speech for 5 seconds using recognize speech for ()s in the () block.
  6. The sprite object has already been initialized.
  7. We analyze the spoken speech for 4 seconds and assume it is in the English language using analysespeech().
  8.  We store the recognized speech as text in the variable command using speechresult().
  9. We convert the recognized speech to lowercase and store it in the variable answer.
  10. We ask the AI model ChatGPT (acting as an AI assistant) a question based on the user’s input stored in the answer.
  11. We retrieve the response from the AI model and store it in the variable result using chatGPTresult().
  12. We display the response on the screen using print(result).
  13.  We convert the response into speech and play it aloud using the speech object using speak().
  14.  The character “Tobi” speaks the response for 5 seconds using say().
  15. Press the Run button to run the code.

Output

I asked ChatGPT for a joke, and it responded with an interesting response.

Read More
Learn how to set the bounding box threshold, and detect signals such as 'Go', 'TurnRight', 'TurnLeft', and 'Stop' to control quadruped movements.

Introduction

Sign detection is being performed using a camera and a RecognitionCards object. The RecognitionCards object is set up with a threshold value and is enabled to draw a box around the detected object. The robot uses sensors, cameras, and machine learning algorithms to detect and understand the sign, and then performs a corresponding action based on the signal detected.

These robots are often used in manufacturing, healthcare, and customer service industries to assist with tasks that require human-like interaction and decision-making.

Code

sprite = Sprite('Tobi')
quarky = Quarky()

import time
quad=Quadruped(4,1,8,5,3,2,7,6)

recocards = RecognitionCards()
recocards.video("on flipped")
recocards.enablebox()
recocards.setthreshold(0.6)
quad.home()
while True:
  recocards.analysecamera()
  sign = recocards.classname()
  sprite.say(sign + ' detected')
  
  if recocards.count() > 0:
    if 'Go' in sign:
      quarky.drawpattern("jjjijjjjjiiijjjiiiiijjjjijjjjjjijjj")
      quad.move("forward",1000,1)
      
      
    if 'Turn Left' in sign:
      quarky.drawpattern("jjjddjjjjjdddjdddddddjjjdddjjjjddjj")
      quad.move("lateral right",1000,1)
      
      
    if 'Turn Right' in sign:
      quarky.drawpattern("jjggjjjjgggjjjgggggggjgggjjjjjggjjj")
      quad.move("lateral left",1000,1)
      
      
    if 'U Turn' in sign:
      quarky.drawpattern("jjjbjjjjjjbjjjjbbbbbjjjbbbjjjjjbjjj")
      quad.move("backward",1000,1)
      
      
    else:
      quad.home()

Logic

  1. This code is using several objects to detect and respond to certain signs or images captured by a camera.
  2. First, it creates a Sprite object with the name ‘Tobi’, and a Quarky object. It also imports a time module.
  3. Next, a Quadruped object is created with some parameters. Then, a RecognitionCards object is created to analyze the camera input. The object is set to enable a box around the detected object and to set the threshold of detection to 0.6.
  4. The code then puts the Quadruped object in its home position and enters an infinite loop.
  5. Within the loop, the code captures the camera input and uses the RecognitionCards object to analyze it. If an object is detected, the object’s class name is retrieved and used by the Sprite object to say that the object was detected.
  6. If the count of detected objects is greater than zero, the code checks if the detected object is a specific sign.
  7. If the object is a ‘Go‘ sign, the Quarky object will draw a specific pattern, and the Quadruped object will move forward.
  8. If the object is a ‘Turn Left‘ sign, the Quarky object will draw a different pattern and the Quadruped object will move to the right.
  9. If the object is a ‘Turn Right‘ sign, the Quarky object will draw another pattern, and the Quadruped object will move to the left.
  10. Finally, if the object is a ‘U Turn‘ sign, the Quarky object will draw a fourth pattern, and the Quadruped object will move backward.
  11. If the detected object is not any of the specific signs, the Quadruped object will return to its home position.
  12. So, this code helps a robot understand hand signs and move in response to them!

Output

Read More
Learn how to create a dataset and Machine Learning Model for an automated game from the user's input. See how to open the ML environment, upload data, label images, train the model, and export the Python script.

Introduction

In this example project, we are going to create a Machine Learning Model where fish automatically feed on randomly generated food.

Data Collection

  • Now, we are going to collect the data of  automated fish feast Game .
  • This data will contain the actions that we have taken to accomplish the game successfully.
  • We will use the data we collect here, to teach our device how to play the automated fish feast game , i.e. to perform machine learning.
  • The data that you collect will get saved in your device as a csv (comma separated values) file. If you open this file in Microsoft Excel, it will look as shown below:

Follow the steps below

  1. Open PictoBlox and create a new file.
  2. Select the coding environment as Python Coding Environment.
  3. Now write code in python.

Code for making dataset

  1. Creates a sprite object named “Fish”. A sprite is typically a graphical element that can be animated or displayed on a screen.
  2. Creates another sprite object named “Orange” and also upload backdrop of “Underwater2” .
  3. Click on the Fish.py file from the Project files section.
    sprite = Sprite('Fish')
  4. Similarly, declare new sprite on the Fish.py file.
    sprite1 = Sprite('Orange')
  5. Then we will import the time, random, os, math, TensorFlow as tf  and Pandas as pd modules using the import keyword for using delay in the program later.
    1. Time – For using delay in the program.
    2. Random – For using random position.
    3. Pandas as pd – For using Data Frame.
    4. Math– For using math functions in code.
    5. Os– For reading files from Program files.
      import random
      import time
      import tensorflow as tf
      import pandas as pd
      import os
      import math
  6. Now, make 3 variables curr_x, curr_y, ang_f, mov_f and score with initial values 4, 3, 50, and 0 respectively.
    1. curr_x – To store the initial x – position of fish.
    2. curr_y – To store the initial y – position of fish.
    3. ang_f – To store increment value in angle of fish on pressing specific key.
    4. mov_f – To store increment value in movement of fish on pressing specific key.
    5. angle – To store initial angle of fish.
    6. score – To store the score while playing the game.
      curr_x = 4
      curr_y = 3
      ang_f= 10
      mov_f= 5
      score = 0
      angle = 90
  7. Now set initial position and angle of fish.
    sprite.setx(curr_x)
    sprite.sety(curr_y)
    sprite.setdirection(DIRECTION=90)
  8. Now, make a function settarget() in which we are generating food at a random position. We pass one argument “t” in the function for generating target food in the greed position of the t gap.
    1. x and y – To generate the food at random position on stage.
    2. time.sleep – For giving the time delay.
    3. sprite1.set()– To Set the position of food at random position on stage.
      def settarget(t):
      x = random.randrange(-200, 200, t)
      y = random.randrange(-155, 155, t)
      time.sleep(0.1)
      sprite1.setx(x)
      sprite1.sety(y)
      return x, y 
  9. Now set the target (food). In this, fish are chasing the food, and target_x  and  target_y should be equal to the x and y positions of the food.
    target_x, target_y = settarget(40) 
  10. Now create a data frame of name “Chase_Data.csv” to collect the data for machine learning and if this name csv exist then directly add data in it.
    if(os.path.isfile('Chase_Data.csv')):
      data=pd.read_csv('Chase_Data.csv')
    else:
      data = pd.DataFrame({"curr_X": curr_x, "curr_Y": curr_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "direction": angle, "Action": "RIGHT"}, index=[0])
    
  11. After that, we will use the while True loop to run the code indefinitely. Don’t forget to add a colon ‘:’ just after the loop to avoid errors.
    while True:
  12. Now write the script for moving the Fish in forward direction and change clockwise or anticlockwise direction by fix value with the help of a conditional statement.
    1. If the up arrow key is pressed then fish will move mov_f position in same direction.
    2. After pressing the up arrow key action taken should be stored in the Data frame with data.append command.
      if sprite.iskeypressed("up arrow"):
          data = data.append({"curr_X": curr_x, "curr_Y": curr_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "direction": angle, "Action": "UP"}, ignore_index=True)
          sprite.move(mov_f)
  13. Repeat the process for the set direction in clockwise or anticlockwise.
    if sprite.iskeypressed("left arrow"):
        data = data.append({"curr_X": curr_x, "curr_Y": curr_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "direction": angle, "Action": "LEFT"}, ignore_index=True)
        angle = angle - ang_f
        sprite.setdirection(DIRECTION=angle)
     if sprite.iskeypressed("right arrow"):
        data = data.append({"curr_X": curr_x, "curr_Y": curr_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "direction": angle, "Action": "RIGHT"}, ignore_index=True)
        angle = angle + ang_f
        sprite.setdirection(DIRECTION=angle)
  14. Write the conditional statement for the storing data in csv file after few score.
    if(score>0 and score%2==0):
        data.to_csv('Chase_Data.csv',index=False)
  15. Again write the conditional statement for the score variable if the fish and food position difference is less then 20 then the score should be increased by one.
    if abs(curr_x-target_x)<20 and abs(curr_y-target_y)<20: 
        score = score + 1 
        sprite.say(("your score is: {}".format(score)))
  16. If the score is equal to or greater than 40 then data should be printed on Chase Data.csv file.
    if (score >= 40):
          print(data)
          data.to_csv('Chase Data.csv')
          break
        target_x, target_y = settargetLED()
  17. Now update the curr_x and curr_y variables by storing the current position of the fish and delaying movement by 0.02 seconds.
    curr_x=math.floor(sprite.x())
    curr_y=math.floor(sprite.y())
    time.sleep(0.02)
  18. The final code is as follows:
    sprite = Sprite('Fish')
    sprite1 = Sprite('Orange')
     
    import random
    import time
    import tensorflow as tf
    import pandas as pd
    import os
    import math
    		
    curr_x = -170
    curr_y = 138
    score=0
    ang_f=10
    mov_f=5
    angle=90
    sprite.say(("your score is: {}".format(score)))
    sprite.setx(curr_x)
    sprite.sety(curr_y)
    sprite.setdirection(DIRECTION=90)
    
    def settarget(t):
      x = random.randrange(-200, 200, t)
      y = random.randrange(-155, 155, t)
      time.sleep(0.1)
      sprite1.setx(x)
      sprite1.sety(y)
      return x, y
    
    target_x, target_y = settarget(40)
    
    if(os.path.isfile('Chase_Data.csv')):
      data=pd.read_csv('Chase_Data.csv')
    else:
      data = pd.DataFrame({"curr_X": curr_x, "curr_Y": curr_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "direction": angle, "Action": "RIGHT"}, index=[0])
    
    while True:
      angle=sprite.direction()
      if sprite.iskeypressed("up arrow"):
        data = data.append({"curr_X": curr_x, "curr_Y": curr_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "direction": angle, "Action": "UP"}, ignore_index=True)
        sprite.move(mov_f)
    
         
      if sprite.iskeypressed("left arrow"):
        data = data.append({"curr_X": curr_x, "curr_Y": curr_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "direction": angle, "Action": "LEFT"}, ignore_index=True)
        angle = angle - ang_f
        sprite.setdirection(DIRECTION=angle)
        
         
      if sprite.iskeypressed("right arrow"):
        data = data.append({"curr_X": curr_x, "curr_Y": curr_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "direction": angle, "Action": "RIGHT"}, ignore_index=True)
        angle = angle + ang_f
        sprite.setdirection(DIRECTION=angle)
        
      if(score>0 and score%2==0):
        data.to_csv('Chase_Data.csv',index=False)
      
      if abs(curr_x-target_x)<20 and abs(curr_y-target_y)<20:
        score = score + 1
        sprite.say(("your score is: {}".format(score)))
        if (score >= 40):
          data.to_csv('Chase_Data.csv',index=False)
          break
        target_x, target_y = settarget(40)
      curr_x=math.floor(sprite.x())
      curr_y=math.floor(sprite.y())
      time.sleep(0.02)
     
    
  19. Press the Run button and play fish feast game to collect data.
  20. Store this dataset on your local computer.

    Numbers(C/R) in Machine Learning Environment

    Datasets on the internet are hardly ever fit to directly train on. Programmers often have to take care of unnecessary columns, text data, target columns, correlations, etc. Thankfully, PictoBlox’s ML Environment is packed with features to help us pre-process the data as per our liking.

    Let’s create the ML model.

    Opening Image Classifier Workflow

    Alert: The Machine Learning Environment for model creation is available in the only desktop version of PictoBlox for Windows, macOS, or Linux. It is not available in Web, Android, and iOS versions.

    Follow the steps below:

    1. Open PictoBlox and create a new file.
    2. Select the coding environment as Block Coding Environment.
    3. Select the “Open ML Environment” option under the “Files” tab to access the ML Environment.
    4. You’ll be greeted with the following screen.
      Click on “Create New Project“.
    5. You shall see the Numbers C/R workflow with an option to either “Upload Dataset” or “Create Dataset”.

      Uploading/Creating Dataset

      Datasets can either be uploaded or created on the ML Environment. Lets see how it is done.

      Uploading a dataset
      1. To upload a dataset, click on the Upload Dataset button and the Choose CSV from your files button.
        Note: An uploaded dataset must be a “.csv” file.
      2. Once uploaded the first 50 rows of the uploaded CSV document will show up in the window.

      3. If you look at the output column, all the values are currently “0”. Hence, first we need to create an output column.
        1. In the Dataset table, click on the tick near Select All to de-select all the columns.
        2. click on the tick of Action column to select it. We will make this column the output.
        3. The output column must always be numerical. Hence click on the button Text to Number to convert the data within this column to numerical type.
        4. Now select it again and press the Set as Output button to set this column as Output.
        5. There is also many which is not useful in training our model and needs to be disable. So select it and click the Disable button in the Selected columns section.

          Creating a Dataset
          1. To create a dataset, click on the Create Dataset button.
          2. Select the number of rows and columns that are to be added and click on the Create button. More rows and columns can be added as and when needed.

          Notes:

          1. Each column represents a feature. These are the values used by the model to train itself.
          2. The “Output” column contains the target values. These are the values that we expect the model to return when features are passed.
          3. The window only shows the first 50 rows of the dataset.
          4. Un-check the “Select All” checkbox to un-select all the columns.

          Training the Model

          After data is pre-processed and optimized, it’s fit to be used in model training. To train the model, simply click the “Train Model” button found in the “Training” panel.

          By training the model, meaningful information is extracted from the numbers, and that in turn updates the weights. Once these weights are saved, the model can be used to make predictions on data previously unseen.

          The model’s function is to use the input data and predict the output. The target column must always contain numbers.

          However, before training the model, there are a few hyperparameters that need to be understood. Click on the “Advanced” tab to view them.

          There are three hyperparameters that can be altered in the Numbers(C/R) Extension:

          1. Epochs– The total number of times the data will be fed through the training model. Therefore, in 10 epochs, the dataset will be fed through the training model 10 times. Increasing the number of epochs can often lead to better performance.
          2. Batch Size– The size of the set of samples that will be used in one step. For example, if there are 160 data samples in the dataset, and the batch size is set to 16, each epoch will be completed in 160/16=10 steps. This hyperparameter rarely needs any altering.
          3. Learning Rate– It dictates the speed at which the model updates the weights after iterating through a step. Even small changes in this parameter can have a huge impact on the model performance. The usual range lies between 0.001 and 0.0001.
          Note: Hover the mouse pointer over the question mark next to the hyperparameters to see their description.

          It’s a good idea to train a numeric classification model for a high number of epochs. The model can be trained in both JavaScript and Python. In order to choose between the two, click on the switch on top of the Training panel.

          Alert: Dependencies must be downloaded to train the model in Python, JavaScript will be chosen by default.

          The accuracy of the model should increase over time. The x-axis of the graph shows the epochs, and the y-axis represents the accuracy at the corresponding epoch.

          A window will open. Type in a project name of your choice and select the “Numbers(C/R)” extension. Click the “Create Project” button to open the Numbers(C/R) window.

          Testing the Model

          To test the model, simply enter the input values in the “Testing” panel and click on the “Predict” button.

          The model will return the probability of the input belonging to the classes.

      Export in Python Coding

      Click on the “PictoBlox” button, and PictoBlox will load your model into the Python Coding Environment if you have opened the ML Environment in Python Coding.

    Code

    1. Creates a sprite object named “Fish”. A sprite is typically a graphical element that can be animated or displayed on a screen.
    2. Creates another sprite object named “Orange” and also upload backdrop of “Underwater2” .
    3. Click on the Fish.py file from the Project files section.
      sprite = Sprite('Fish')
    4. Similarly, declare new sprite on the Fish.py file.
      sprite1 = Sprite('Orange')
    5. Then we will import the time, random, os, math, TensorFlow as tf  and Pandas as pd modules using the import keyword for using delay in the program later.
      1. Time – For using delay in the program.
      2. Random – For using random position.
      3. Pandas as pd – For using Data Frame.
      4. Math– For using math functions in code.
      5. Os– For reading files from Program files.
        import random
        import time
        import tensorflow as tf
        import pandas as pd
        import os
        import math
    6. Now, make 3 variables curr_x, curr_y, ang_f, mov_f,angle and score with initial values -170, 138, 30, 15, 90 and 0 respectively.
      1. curr_x – To store the initial x – position of fish.
      2. curr_y – To store the initial y – position of fish.
      3. ang_f – To store increment value in angle of fish on pressing specific key.
      4. mov_f – To store increment value in movement of fish on pressing specific key.
      5. angle – To store initial angle of fish.
      6. score – To store the score while playing the game.
        curr_x = -170
        curr_y = 138
        ang_f= 30
        mov_f= 15
        score = 0
        angle = 90
    7. Now set initial position and angle of fish.
      sprite.setx(curr_x)
      sprite.sety(curr_y)
      sprite.setdirection(DIRECTION=90)
    8. Now, make a function settarget() in which we are generating food at a random position. We pass one argument “t” in the function for generating target food in the greed position of the t gap.
      1. x and y – To generate the food at random position on stage.
      2. time.sleep – For giving the time delay.
      3. sprite1.set()– To Set the position of food at random position on stage.
        def settarget(t):
        x = random.randrange(-200, 200, t)
        y = random.randrange(-155, 155, t)
        time.sleep(0.1)
        sprite1.setx(x)
        sprite1.sety(y)
        return x, y 
    9. Now set the position of food. In this, fish are chasing the food, and target_x  and  target_y should be equal to the x and y positions of the food.
      target_x, target_y = settarget(40) 
    10. Now, make a function runprediction() in which we are predicting class (Left, Up, right) by taking argument from user . We pass three arguments “diff_x”, “diff-y”, “ang” in the function.
      1. inputvalue – To store input parameters of function in array.
      2. model.predict() – For predicting output from trained model.
      3. np.argmax(,)– To find the most probable prediction output.
        def runprediction(diff_x, diff_y, ang):
          inputValue=[diff_x, diff_y, ang]
          #Input Tensor
          inputTensor = tf.expand_dims(inputValue, 0)
          #Predict
          predict = model.predict(inputTensor)
          predict_index = np.argmax(predict[0], axis=0)
          #Output
          predicted_class = class_list[predict_index]
          return predicted_class
    11. After that, we will use the while True loop to run the code indefinitely. Don’t forget to add a colon ‘:’ just after the loop to avoid errors.
      while True:
    12. In while loop find angle of sprite and call runprediction function by passing arguments in it.
       angle=sprite.direction()
        move = runprediction(curr_x- target_x, curr_y-target_y, angle)
    13. Now write the script for moving the Fish in forward direction and change clockwise or anticlockwise direction by fix value with the help of a conditional statement.
      1. If the predicted value is “UP” then fish will move mov_f position in same direction.
      2. If the predicted value is “LEFT” then fish will change direction by some constant value in anticlockwise direction.
      3. If the predicted value is “RIGHT” then fish will change direction by some constant value in clockwise direction.
        if move == "UP":
            sprite.move(mov_f)
            curr_x=sprite.x()
            curr_y=sprite.y()
          if move == "LEFT":
            angle = angle - ang_f
            sprite.setdirection(DIRECTION=angle)
          if move == "RIGHT":
            angle = angle + ang_f
            sprite.setdirection(DIRECTION=angle)
    14.  Again write the conditional statement for the score variable if the fish and food position difference is less then 20 then the score should be increased by one also set new position of target.
      if abs(curr_x-target_x)<20 and abs(curr_y-target_y)<20: 
          score = score + 1 
          sprite.say(("your score is: {}".format(score)))
      target_x, target_y = settarget()
    15.  Now add delay function for delaying movement by 0.02 seconds.
      time.sleep(0.02)
    16. The final code is as follows
      sprite = Sprite('Fish')
      sprite1 = Sprite('Orange')
       
      import random
      import time
      import numpy as np
      import tensorflow as tf
      import pandas as pd
      import os
      import math
      
      #Load Number Model
      model= tf.keras.models.load_model(
      		"num_model.h5", 
      		custom_objects=None, 
      		compile=True, 
      		options=None)
      		
      #List of classes
      class_list = ['UP','LEFT','RIGHT',]  
      		
      curr_x = -170
      curr_y = 138
      score=0
      ang_f=30
      mov_f=15
      angle=90
      
      sprite.say(("your score is: {}".format(score)))
      sprite.setx(-170)
      sprite.sety(138)
      sprite.setdirection(DIRECTION=90)
      
      def settarget():
        x = random.randrange(-200, 200, 1)
        y = random.randrange(-155, 155, 1)
        time.sleep(0.1)
        sprite1.setx(x)
        sprite1.sety(y)
        return x, y
      
      target_x, target_y = settarget()
      
      def runprediction(diff_x, diff_y, ang):
        inputValue=[diff_x, diff_y, ang]
        #Input Tensor
        inputTensor = tf.expand_dims(inputValue, 0)
        #Predict
        predict = model.predict(inputTensor)
        predict_index = np.argmax(predict[0], axis=0)
        #Output
        predicted_class = class_list[predict_index]
        return predicted_class
      
      while True:
        angle=sprite.direction()
        move = runprediction(curr_x- target_x, curr_y-target_y, angle)
      
        if move == "UP":
          sprite.move(mov_f)
          curr_x=sprite.x()
          curr_y=sprite.y()
      
        if move == "LEFT":
          angle = angle - ang_f
          sprite.setdirection(DIRECTION=angle)
      
        if move == "RIGHT":
          angle = angle + ang_f
          sprite.setdirection(DIRECTION=angle)
       
      
        if abs(curr_x-target_x)<20 and abs(curr_y-target_y)<20:
          score = score + 1
          sprite.say(("your score is: {}".format(score)))
          target_x, target_y = settarget()
      
        time.sleep(0.2) 
      

      Final Result

      Conclusion

      Creating a Machine Learning Model of automated fish feast game can be both complex and time-consuming. Through the steps demonstrated in this project, you can create your own Machine Learning Model of automated game. Once trained, you can export the model into the Python Coding Environment, where you can tweak it further to give you the desired output. Try creating a Machine Learning Model of your own today and explore the possibilities of Number Classifier in PictoBlox!

Read More
Learn how to create a dataset and Machine Learning Model for an automated shark attack game from the user's input. See how to open the ML environment, upload data, label images, train the model, and export the Python script.

Introduction

In this example project, we are going to create a Machine Learning Model where shark run by the user and fish automatically feed on randomly generated food while escaping from sharks.

Data Collection

  • Now, we are going to collect the data of “Shark Attack: Hungry for Fish” game .
  • This data will contain the actions that we have taken to accomplish the game successfully.
  • We will use the data we collect here, to teach our device how to play the “Shark Attack: Hungry for Fish” game , i.e. to perform machine learning.
  • The data that you collect will get saved in your device as a csv (comma separated values) file. If you open this file in Microsoft Excel, it will look as shown below:

Follow the steps below

  1. Open PictoBlox and create a new file.
  2. Select the coding environment as Python Coding Environment.
  3. Now write code in python.

Code for making dataset

  1. Creates a sprite object named “Fish”. A sprite is typically a graphical element that can be animated or displayed on a screen.
  2. Creates three sprites object named “Orange” , “Shark2” and “Button3” and also upload backdrop of “Underwater2” .
  3. Click on the Fish.py file from the Project files section.
    sprite = Sprite('Fish')
  4. Similarly, declare new sprites on the Fish.py file.
    sprite1 = Sprite('Orange')
    sprite2 = Sprite('Shark 2')
    sprite3 = Sprite('Button3')
  5. Then we will import the time, random, os, math, TensorFlow as tf  and Pandas as pd modules using the import keyword for using delay in the program later.
    1. Time – For using delay in the program.
    2. Random – For using random position.
    3. Pandas as pd – For using Data Frame.
    4. Math– For using math functions in code.
    5. Os– For reading files from Program files.
      import random
      import time
      import tensorflow as tf
      import pandas as pd
      import os
      import math
  6. Now, make 3 variables curr_x, curr_y, shark_x, shark_y, score, chance, fish_d, fish_m, shark_m, angle_f and angle_s with initial values 25, 108, -177, 116, 0, 5, 20, 35, 25, 90 and 90 respectively.
    1.  curr_x – To store the initial x – position of fish.
    2. curr_y – To store the initial y – position of fish.
    3. shark_x – To store the initial x – position of shark.
    4. shark_y – To store the initial y – position of shark.
    5. score – To store the score while playing the game.
    6. chance– To store the chance of fish while playing the game.
    7. fish_d– To store increment value in direction of fish on pressing specific key.
    8. fish_m – To store increment value in movement of fish on pressing specific key.
    9. shark_m – To store increment value in movement of shark on pressing specific key.
    10. shark_d – To store increment value in direction of shark on pressing specific key.
    11. angle_f – To store increment value in angle of fish on pressing specific key.
    12. angle_s – To store increment value in angle of shark on pressing specific key.
      curr_x = 25 
      curr_y = 108 
      shark_x=-177 
      shark_y=116 
      score=0 
      chance=5 
      fish_d=20 
      fish_m=25 
      shark_m=4 
      angle_f=90 
      angle_s=90
  7. Now set initial position and angle of fish and shark both.
    sprite.setx(curr_x)
    sprite.sety(curr_y)
    sprite2.setx(shark_x)
    sprite2.sety(shark_y)
    sprite.setdirection(DIRECTION=angle_f)
    sprite2.setdirection(DIRECTION=angle_s)
  8. Now, make a function settarget1() in which we are generating food at a random position. We pass one argument “m” in the function for generating target food in the greed position of the t gap.
    1. x and y – To generate the fish at random position on stage.
    2. x1 and y1 – To generate the food at random position on stage.
    3. x2 and y2 – To generate the shark at random position on stage.
    4. time.sleep – For giving the time delay.
    5. sprite.set()– To Set the position of fish at random position on stage.
    6. sprite1.set()– To Set the position of food at random position on stage.
    7. sprite2.set()– To Set the position of shark at random position on stage.
      def settarget(t):
        x = random.randrange(-200, 200, t)
        y = random.randrange(-155, 155, t)
        x1 = random.randrange(-200, 200, t)
        y1 = random.randrange(-155, 155, t)
        x2 = random.randrange(-200, 200, t)
        y2 = random.randrange(-155, 155, t)
        time.sleep(0.1)
        sprite1.setx(x1)
        sprite1.sety(y1)
        sprite.setx(x)
        sprite.sety(y)
        sprite2.setx(x2)
        sprite2.sety(y2)
        return x, y, x1, y1, x2, y2
  9. Now, make a function settarget1() in which we are generating food at a random position. We pass one argument “m” in the function for generating target food in the greed position of the t gap.
    1. x and y – To generate the food at random position on stage.
    2. time.sleep – For giving the time delay.
    3. sprite1.set()– To Set the position of food at random position on stage.
      def settarget1(m):
      x = random.randrange(-200, 200, m)
      y = random.randrange(-155, 155, m)
      time.sleep(0.1)
      sprite1.setx(x)
      sprite1.sety(y)
      return x, y 
  10. Now set the target (food). In this, fish are chasing the food, and target_x  and  target_y should be equal to the x and y positions of the food.
    target_x, target_y = settarget(40) 
  11. Now create a data frame of name “Chase_Data.csv” to collect the data for machine learning and if this name csv exist then directly add data in it.
    target_x, target_y = settarget1(40)
    if(os.path.isfile('Chase_Data.csv')):
      data=pd.read_csv('Chase_Data.csv')
    else:
      data = pd.DataFrame({"curr_X": curr_x, "curr_Y": curr_y,"shark_X": shark_x, "shark_Y": shark_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "diff_x1":shark_x-curr_x, "diff_y1":shark_y-curr_y, "direction_f": angle_f, "direction_s": angle_s, "Action": "RIGHT"}, index=[0])
  12. After that, we will use the while True loop to run the code indefinitely. Don’t forget to add a colon ‘:’ just after the loop to avoid errors.
    while True:
  13. In a while loop, write code by which sharks follow fish by ‘shark_m’ steps.
    sprite2.spriteRequest.requestCommand("motion_pointtowards", {"TOWARDS": "Fish"})
     sprite2.move(shark_m)
  14. Find the direction of shark and fish using the Python pictoblox function and take the floor value of angle.
    angle_f=sprite.direction()
    angle_s=sprite2.direction()
    anglef=math.floor(angle_f)
    angles=math.floor(angle_s)
  15. Now write the script for moving the Fish in forward direction and change clockwise or anticlockwise direction by fix value with the help of a conditional statement.
    1. If the up arrow key is pressed then fish will move fish_m position in same direction.
    2. After pressing the up arrow key action taken should be stored in the Data frame with data.append command.
      if sprite.iskeypressed("up arrow"):
          data = data.append({"curr_X": curr_x, "curr_Y": curr_y,"shark_X": shark_x, "shark_Y": shark_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "diff_x1":shark_x-curr_x, "diff_y1":shark_y-curr_y, "direction_f": anglef, "direction_s": angles, "Action": "UP"}, ignore_index=True)
          sprite.move(fish_m)
  16. Repeat the process for the set direction in clockwise or anticlockwise.
    if sprite.iskeypressed("left arrow"):
        data = data.append({"curr_X": curr_x, "curr_Y": curr_y,"shark_X": shark_x, "shark_Y": shark_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "diff_x1":shark_x-curr_x, "diff_y1":shark_y-curr_y, "direction_f": anglef, "direction_s": angles, "Action": "LEFT"}, ignore_index=True)
        angle = anglef - fish_d
        sprite.setdirection(DIRECTION=angle)
    if sprite.iskeypressed("right arrow"):
        data = data.append({"curr_X": curr_x, "curr_Y": curr_y,"shark_X": shark_x, "shark_Y": shark_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "diff_x1":shark_x-curr_x, "diff_y1":shark_y-curr_y, "direction_f": anglef, "direction_s": angles, "Action": "RIGHT"}, ignore_index=True)
        angle = anglef + fish_d
        sprite.setdirection(DIRECTION=angle)
  17. Write the conditional statement for the storing data in csv file after few score.
    if(score>0 and score%2==0):
        data.to_csv('Chase_Data.csv',index=False)
  18. Write the conditional statement for the chance variable. If the fish and shark position difference is less than 20, then the chance should be decreased by one.
     if abs(shark_x-curr_x)<20 and abs(shark_y-curr_y)<20:
        chance= chance-1
  19. Update the position of all three sprites, and if chance becomes 0, then data should be printed on Chase Data. csv file, and the positions of all three sprites change randomly by the functions settarget() and update chance value.
      if abs(shark_x-curr_x)<20 and abs(shark_y-curr_y)<20:
        chance= chance-1
        curr_x, curr_y, target_x, target_y, shark_x, shark_y = settarget(40)
        sprite3.say(("score: ",score ," and chance:  ",chance,""))
        if (chance == 0):
          data.to_csv('Chase_Data.csv',index=False)
          curr_x, curr_y, target_x, target_y, shark_x, shark_y = settarget(40)
          chance=5
  20. Again write the conditional statement for the score variable if the fish and food position difference is less then 20 then the score should be increased by one.
    if abs(curr_x-target_x)<20 and abs(curr_y-target_y)<20: 
        score = score + 1 
        sprite.say(("your score is: {}".format(score)))
  21. If the score is equal to or greater than 50 then data should be printed on Chase Data.csv file and food positions change randomly by the function settarget1().
    if (score >= 40):
          print(data)
          data.to_csv('Chase Data.csv')
          break
     target_x, target_y = settarget1()
  22. Now update the curr_x, curr_y, shark_x and shark_y variables by storing the current position of the fish and shark and delaying movement by 0.02 seconds.
      curr_x=math.floor(sprite.x())
      curr_y=math.floor(sprite.y())
      shark_x=math.floor(sprite2.x())
      shark_y=math.floor(sprite2.y())
      time.sleep(0.02)
  23. The final code is as follows:
    sprite = Sprite('Fish')
    sprite1 = Sprite('Orange')
    sprite2 = Sprite('Shark 2')
    sprite3 = Sprite('Button3')
    import random
    import time
    import numpy as np
    import tensorflow as tf
    import pandas as pd
    import os
    import math
    		
    curr_x = 25
    curr_y = 108
    shark_x=-177
    shark_y=116
    score=0
    chance=5
    fish_d=20
    fish_m=25
    shark_m=4
    angle_f=90
    angle_s=90
    sprite3.say(("score: ",score ," and chance:  ",chance,""))
    sprite.setx(curr_x)
    sprite.sety(curr_y)
    sprite2.setx(shark_x)
    sprite2.sety(shark_y)
    sprite.setdirection(DIRECTION=angle_f)
    sprite2.setdirection(DIRECTION=angle_s)
    def settarget(t):
      x = random.randrange(-200, 200, t)
      y = random.randrange(-155, 155, t)
      x1 = random.randrange(-200, 200, t)
      y1 = random.randrange(-155, 155, t)
      x2 = random.randrange(-200, 200, t)
      y2 = random.randrange(-155, 155, t)
      time.sleep(0.1)
      sprite1.setx(x1)
      sprite1.sety(y1)
      sprite.setx(x)
      sprite.sety(y)
      sprite2.setx(x2)
      sprite2.sety(y2)
      return x, y, x1, y1, x2, y2
    def settarget1(m):
      x = random.randrange(-200, 200, m)
      y = random.randrange(-155, 155, m)
      time.sleep(0.1)
      sprite1.setx(x)
      sprite1.sety(y)
      return x, y
    target_x, target_y = settarget1(40)
    if(os.path.isfile('Chase_Data.csv')):
      data=pd.read_csv('Chase_Data.csv')
    else:
      data = pd.DataFrame({"curr_X": curr_x, "curr_Y": curr_y,"shark_X": shark_x, "shark_Y": shark_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "diff_x1":shark_x-curr_x, "diff_y1":shark_y-curr_y, "direction_f": angle_f, "direction_s": angle_s, "Action": "RIGHT"}, index=[0])
    while True:
      # sprite2.pointto()
      sprite2.spriteRequest.requestCommand("motion_pointtowards", {"TOWARDS": "Fish"})
      sprite2.move(shark_m)
      angle_f=sprite.direction()
      angle_s=sprite2.direction()
      anglef=math.floor(angle_f)
      angles=math.floor(angle_s)
      if sprite.iskeypressed("up arrow"):
        data = data.append({"curr_X": curr_x, "curr_Y": curr_y,"shark_X": shark_x, "shark_Y": shark_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "diff_x1":shark_x-curr_x, "diff_y1":shark_y-curr_y, "direction_f": anglef, "direction_s": angles, "Action": "UP"}, ignore_index=True)
        sprite.move(fish_m)
      if sprite.iskeypressed("left arrow"):
        data = data.append({"curr_X": curr_x, "curr_Y": curr_y,"shark_X": shark_x, "shark_Y": shark_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "diff_x1":shark_x-curr_x, "diff_y1":shark_y-curr_y, "direction_f": anglef, "direction_s": angles, "Action": "LEFT"}, ignore_index=True)
        angle = anglef - fish_d
        sprite.setdirection(DIRECTION=angle)
        
         
      if sprite.iskeypressed("right arrow"):
        data = data.append({"curr_X": curr_x, "curr_Y": curr_y,"shark_X": shark_x, "shark_Y": shark_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "diff_x1":shark_x-curr_x, "diff_y1":shark_y-curr_y, "direction_f": anglef, "direction_s": angles, "Action": "RIGHT"}, ignore_index=True)
        angle = anglef + fish_d
        sprite.setdirection(DIRECTION=angle)
        
      if(score>0 and score%2==0):
        data.to_csv('Chase_Data.csv',index=False)
      
      if abs(shark_x-curr_x)<20 and abs(shark_y-curr_y)<20:
        chance= chance-1
        curr_x, curr_y, target_x, target_y, shark_x, shark_y = settarget(40)
        sprite3.say(("score: ",score ," and chance:  ",chance,""))
        if (chance == 0):
          data.to_csv('Chase_Data.csv',index=False)
          curr_x, curr_y, target_x, target_y, shark_x, shark_y = settarget(40)
          chance=5
      if abs(curr_x-target_x)<20 and abs(curr_y-target_y)<20:
        score = score + 1
        sprite3.say(("score: ",score ," and chance:  ",chance,""))
        if (score >= 50):
          data.to_csv('Chase_Data.csv',index=False)
          break
        target_x, target_y = settarget1(40)
      curr_x=math.floor(sprite.x())
      curr_y=math.floor(sprite.y())
      shark_x=math.floor(sprite2.x())
      shark_y=math.floor(sprite2.y())
      time.sleep(0.02)
    
  24. Press the Run button and play fish feast game to collect data.
  25. Store this dataset on your local computer.

    Numbers(C/R) in Machine Learning Environment

    Datasets on the internet are hardly ever fit to directly train on. Programmers often have to take care of unnecessary columns, text data, target columns, correlations, etc. Thankfully, PictoBlox’s ML Environment is packed with features to help us pre-process the data as per our liking.

    Let’s create the ML model.

    Opening Image Classifier Workflow

    Alert: The Machine Learning Environment for model creation is available in the only desktop version of PictoBlox for Windows, macOS, or Linux. It is not available in Web, Android, and iOS versions.

    Follow the steps below:

    1. Open PictoBlox and create a new file.
    2. Select the coding environment as Block Coding Environment.
    3. Select the “Open ML Environment” option under the “Files” tab to access the ML Environment.
    4. You’ll be greeted with the following screen.
      Click on “Create New Project“.
    5. You shall see the Numbers C/R workflow with an option to either “Upload Dataset” or “Create Dataset”.

      Uploading/Creating Dataset

      Datasets can either be uploaded or created on the ML Environment. Lets see how it is done.

      Uploading a dataset
      1. To upload a dataset, click on the Upload Dataset button and the Choose CSV from your files button.
        Note: An uploaded dataset must be a “.csv” file.
      2. Once uploaded the first 50 rows of the uploaded CSV document will show up in the window.

      3. If you look at the output column, all the values are currently “0”. Hence, first we need to create an output column.
        1. In the Dataset table, click on the tick near Select All to de-select all the columns.
        2. click on the tick of Action column to select it. We will make this column the output.
        3. The output column must always be numerical. Hence click on the button Text to Number to convert the data within this column to numerical type.
        4. Now select it again and press the Set as Output button to set this column as Output.
        5. There is also many which is not useful in training our model and needs to be disable. So select it and click the Disable button in the Selected columns section.

          Creating a Dataset
          1. To create a dataset, click on the Create Dataset button.
          2. Select the number of rows and columns that are to be added and click on the Create button. More rows and columns can be added as and when needed.

          Notes:

          1. Each column represents a feature. These are the values used by the model to train itself.
          2. The “Output” column contains the target values. These are the values that we expect the model to return when features are passed.
          3. The window only shows the first 50 rows of the dataset.
          4. Un-check the “Select All” checkbox to un-select all the columns.

          Training the Model

          After data is pre-processed and optimized, it’s fit to be used in model training. To train the model, simply click the “Train Model” button found in the “Training” panel.

          By training the model, meaningful information is extracted from the numbers, and that in turn updates the weights. Once these weights are saved, the model can be used to make predictions on data previously unseen.

          The model’s function is to use the input data and predict the output. The target column must always contain numbers.

          However, before training the model, there are a few hyperparameters that need to be understood. Click on the “Advanced” tab to view them.

          There are three hyperparameters that can be altered in the Numbers(C/R) Extension:

          1. Epochs– The total number of times the data will be fed through the training model. Therefore, in 10 epochs, the dataset will be fed through the training model 10 times. Increasing the number of epochs can often lead to better performance.
          2. Batch Size– The size of the set of samples that will be used in one step. For example, if there are 160 data samples in the dataset, and the batch size is set to 16, each epoch will be completed in 160/16=10 steps. This hyperparameter rarely needs any altering.
          3. Learning Rate– It dictates the speed at which the model updates the weights after iterating through a step. Even small changes in this parameter can have a huge impact on the model performance. The usual range lies between 0.001 and 0.0001.
          Note: Hover the mouse pointer over the question mark next to the hyperparameters to see their description.

          It’s a good idea to train a numeric classification model for a high number of epochs. The model can be trained in both JavaScript and Python. In order to choose between the two, click on the switch on top of the Training panel.

          Alert: Dependencies must be downloaded to train the model in Python, JavaScript will be chosen by default.

          The accuracy of the model should increase over time. The x-axis of the graph shows the epochs, and the y-axis represents the accuracy at the corresponding epoch.

          A window will open. Type in a project name of your choice and select the “Numbers(C/R)” extension. Click the “Create Project” button to open the Numbers(C/R) window.

          Testing the Model

          To test the model, simply enter the input values in the “Testing” panel and click on the “Predict” button.

          The model will return the probability of the input belonging to the classes.

      Export in Python Coding

      Click on the “PictoBlox” button, and PictoBlox will load your model into the Python Coding Environment if you have opened the ML Environment in Python Coding.

    Code

    1. Creates a sprite object named “Fish”. A sprite is typically a graphical element that can be animated or displayed on a screen.
    2. Creates three sprites object named “Orange” , “Shark2” and “Button3” and also upload backdrop of “Underwater2” .
    3. Click on the Fish.py file from the Project files section.
      sprite = Sprite('Fish')
    4. Similarly, declare new sprites on the Fish.py file.
      sprite1 = Sprite('Orange')
      sprite2 = Sprite('Shark 2')
      sprite3 = Sprite('Button3')
    5. Then we will import the time, random, os, math, TensorFlow as tf  and Pandas as pd modules using the import keyword for using delay in the program later.
      1. Time – For using delay in the program.
      2. Random – For using random position.
      3. Pandas as pd – For using Data Frame.
      4. Math– For using math functions in code.
      5. Os– For reading files from Program files.
        import random
        import time
        import tensorflow as tf
        import pandas as pd
        import os
        import math
    6. Now, make 3 variables curr_x, curr_y, ang_f, mov_f and score with initial values 4, 3, 50, and 0 respectively.
      1. curr_x – To store the initial x – position of fish.
      2. curr_y – To store the initial y – position of fish.
      3. shark_x – To store the initial x – position of shark.
      4. shark_y – To store the initial y – position of shark.
      5. score – To store the score while playing the game.
      6. chance– To store the chance of fish while playing the game.
      7. fish_d– To store increment value in direction of fish on pressing specific key.
      8. fish_m – To store increment value in movement of fish on pressing specific key.
      9. shark_m – To store increment value in movement of shark on pressing specific key.
      10. shark_d – To store increment value in direction of shark on pressing specific key.
      11. angle_f – To store increment value in angle of fish on pressing specific key.
      12. angle_s – To store increment value in angle of shark on pressing specific key.
        curr_x = 25 
        curr_y = 108 
        shark_x=-177 
        shark_y=116 
        score=0 
        chance=5 
        fish_d=20 
        fish_m=35 
        shark_m=25 
        shark_d= 20 
        angle_f=90 
        angle_s=90
    7. Now set initial position and angle of fish and shark both.
      sprite.setx(curr_x) 
      sprite.sety(curr_y) 
      sprite2.setx(shark_x) 
      sprite2.sety(shark_y) 
      sprite.setdirection(DIRECTION=angle_f) sprite2.setdirection(DIRECTION=angle_s)
    8. Now, make a function settarget1() in which we are generating food at a random position. We pass one argument “t” in the function for generating target food in the greed position of the t gap.
      1. x and y – To generate the fish at random position on stage.
      2. x1 and y1 – To generate the food at random position on stage.
      3. x2 and y2 – To generate the shark at random position on stage.
      4. time.sleep – For giving the time delay.
      5. sprite.set()– To Set the position of fish at random position on stage.
      6. sprite1.set()– To Set the position of food at random position on stage.
      7. sprite2.set()– To Set the position of shark at random position on stage.
        def settarget1(t):
          x = random.randrange(-200, 200, t)
          y = random.randrange(-155, 155, t)
          x1 = random.randrange(-200, 200, t)
          y1 = random.randrange(-155, 155, t)
          x2 = random.randrange(-200, 200, t)
          y2 = random.randrange(-155, 155, t)
          time.sleep(0.1)
          sprite1.setx(x1)
          sprite1.sety(y1)
          sprite.setx(x)
          sprite.sety(y)
          sprite2.setx(x2)
          sprite2.sety(y2)
          return x, y, x1, y1, x2, y2
    9. Now, make a function settarget() in which we are generating food at a random position. We pass one argument “m” in the function for generating target food in the greed position of the t gap.
      1. x and y – To generate the food at random position on stage.
      2. time.sleep – For giving the time delay.
      3. sprite1.set()– To Set the position of food at random position on stage.
        def settarget(m):
        x = random.randrange(-200, 200, m)
        y = random.randrange(-155, 155, m)
        time.sleep(0.1)
        sprite1.setx(x)
        sprite1.sety(y)
        return x, y 
    10. Now set the target (food). In this, fish are chasing the food, and target_x  and  target_y should be equal to the x and y positions of the food.
      target_x, target_y = settarget(40)
    11. Now, make a function runprediction() in which we are predicting class (Left, Up, right) by taking argument from user . We pass three arguments “diff_x”, “diff-y”, “diff_x1”, “diff_y1”, “ang1”, “ang2” in the function.
      1. inputvalue – To store input parameters of function in array.
      2. model.predict() – For predicting output from trained model.
      3. np.argmax(,)– To find the most probable prediction output.
        def runprediction(diff_x, diff_y, diff_x1, diff_y1, ang1, ang2):
          inputValue=[diff_x, diff_y, diff_x1, diff_y1, ang1, ang2]
          #Input Tensor
          inputTensor = tf.expand_dims(inputValue, 0)
          #Predict
          predict = model.predict(inputTensor)
          predict_index = np.argmax(predict[0], axis=0)
          #Output
          predicted_class = class_list[predict_index]
          return predicted_class
    12. After that, we will use the while True loop to run the code indefinitely. Don’t forget to add a colon ‘:’ just after the loop to avoid errors.
      While True :
    13. Now write the script for moving the Shark in forward direction and change clockwise or anticlockwise direction by fix value with the help of a conditional statement.
      1. If the up arrow key is pressed then fish will move fish_m position in same direction.
      2. After pressing the up arrow key, the shark_x and shark_y variables update by storing the current position of the shark.
        if sprite.iskeypressed("up arrow"):
            sprite2.move(shark_m)
            shark_x=sprite2.x()
            shark_y=sprite2.y() 
    14. Repeat the process for the set direction in clockwise or anticlockwise.
        if sprite.iskeypressed("left arrow"):
          angles = angle_s - shark_d
          sprite2.setdirection(DIRECTION=angles)
          
        if sprite.iskeypressed("right arrow"):
          angles = angle_s + shark_d
          sprite2.setdirection(DIRECTION=angles)
    15. Find the direction of shark and fish using the Python pictoblox function and store prediction value in ‘move’ variable.
      angle_f=sprite.direction()
      angle_s=sprite2.direction()
      move = runprediction(curr_x- target_x, curr_y-target_y, shark_x-curr_x, shark_y-curr_y, angle_f, angle_s)
    16. Now write the script for moving the Fish in forward direction and change clockwise or anticlockwise direction by fix value with the help of a conditional statement.
      1. If the predicted value is “UP” then fish will move fish_m position in same direction.
      2. If the predicted value is “LEFT” then fish will change direction by some constant value in anticlockwise direction.
      3. If the predicted value is “RIGHT” then fish will change direction by some constant value in clockwise direction.
        if move == "UP":
            sprite.move(fish_m)
            curr_x=sprite.x()
            curr_y=sprite.y()
        
        if move == "LEFT":
            angle = angle_f - fish_d
            sprite.setdirection(DIRECTION=angle)
        
        if move == "RIGHT":
            angle = angle_f + fish_d
            sprite.setdirection(DIRECTION=angle)
    17. Write the conditional statement for the chance variable. If the fish and shark position difference is less than 20, then the chance should be decreased by one.
       if abs(shark_x-curr_x)<20 and abs(shark_y-curr_y)<20:
          chance= chance-1
    18. Update the position of all three sprites, and if chance becomes 0, then the positions of all three sprites change randomly by the functions settarget1() and update chance value.
        if abs(shark_x-curr_x)<20 and abs(shark_y-curr_y)<20:
          chance= chance-1
          curr_x, curr_y, target_x, target_y, shark_x, shark_y = settarget1(40)
          sprite3.say(("score: ",score ," and chance:  ",chance,""))
          if (chance == 0):
            chance=5
    19. Again write the conditional statement for the score variable if the fish and food position difference is less then 20 then the score should be increased by one and food positions change randomly by the function settarget().
      if abs(curr_x-target_x)<20 and abs(curr_y-target_y)<20: 
          score = score + 1 
          sprite.say(("your score is: {}".format(score)))
          target_x, target_y = settarget(4)
    20. The final code is as follows:
      sprite = Sprite('Fish')
      sprite1 = Sprite('Orange')
      sprite2 = Sprite('Shark 2')
      sprite3 = Sprite('Button3')
       
      import random
      import time
      import numpy as np
      import tensorflow as tf
      import pandas as pd
      import os
      import math
      #Load Number Model
      model= tf.keras.models.load_model(
      		"num_model.h5", 
      		custom_objects=None, 
      		compile=True, 
      		options=None)
      		
      #List of classes
      class_list = ['UP','RIGHT','LEFT',]  
      		
      curr_x = 25
      curr_y = 108
      shark_x=-177
      shark_y=116
      score=0
      chance=5
      fish_d=20
      fish_m=35
      shark_m=25
      shark_d=20
      angle_f=90
      angle_s=90
      
      sprite3.say(("score: ",score ," and chance:  ",chance,""))
       
      sprite.setx(curr_x) 
      sprite.sety(curr_y) 
      sprite2.setx(shark_x) 
      sprite2.sety(shark_y) 
      sprite.setdirection(DIRECTION=angle_f) 
      sprite2.setdirection(DIRECTION=angle_s)
      
      def settarget1(t):
        x = random.randrange(-200, 200, t)
        y = random.randrange(-155, 155, t)
        x1 = random.randrange(-200, 200, t)
        y1 = random.randrange(-155, 155, t)
        x2 = random.randrange(-200, 200, t)
        y2 = random.randrange(-155, 155, t)
        time.sleep(0.1)
        sprite1.setx(x1)
        sprite1.sety(y1)
        sprite.setx(x)
        sprite.sety(y)
        sprite2.setx(x2)
        sprite2.sety(y2)
        return x, y, x1, y1, x2, y2
        
      def settarget(m):
        x = random.randrange(-200, 200, m)
        y = random.randrange(-155, 155, m)
        time.sleep(0.1)
        sprite1.setx(x)
        sprite1.sety(y)
        return x, y
        
      target_x, target_y = settarget(40)
      def runprediction(diff_x, diff_y, diff_x1, diff_y1, ang1, ang2):
        inputValue=[diff_x, diff_y, diff_x1, diff_y1, ang1, ang2]
        #Input Tensor
        inputTensor = tf.expand_dims(inputValue, 0)
        #Predict
        predict = model.predict(inputTensor)
        predict_index = np.argmax(predict[0], axis=0)
        #Output
        predicted_class = class_list[predict_index]
        return predicted_class
      while True:
        if sprite.iskeypressed("up arrow"):
          sprite2.move(shark_m)
          shark_x=sprite2.x()
          shark_y=sprite2.y()
          
        if sprite.iskeypressed("left arrow"):
          angles = angle_s - shark_d
          sprite2.setdirection(DIRECTION=angles)
        if sprite.iskeypressed("right arrow"):
          angles = angle_s + shark_d
          sprite2.setdirection(DIRECTION=angles)
       
        angle_f=sprite.direction()
        angle_s=sprite2.direction()
        move = runprediction(curr_x- target_x, curr_y-target_y, shark_x-curr_x, shark_y-curr_y, angle_f, angle_s)
        
        if move == "UP":
          sprite.move(fish_m)
          curr_x=sprite.x()
          curr_y=sprite.y()
      
        if move == "LEFT":
          angle = angle_f - fish_d
          sprite.setdirection(DIRECTION=angle)
      
        if move == "RIGHT":
          angle = angle_f + fish_d
          sprite.setdirection(DIRECTION=angle)
       
        if abs(shark_x-curr_x)<20 and abs(shark_y-curr_y)<20:
          chance= chance-1
          curr_x, curr_y, target_x, target_y, shark_x, shark_y = settarget1(40)
          sprite3.say(("score: ",score ," and chance:  ",chance,""))
          if (chance == 0):
            chance=5
        if abs(curr_x-target_x)<35 and abs(curr_y-target_y)<35:
          score = score + 1
          sprite3.say(("score: ",score ," and chance:  ",chance,""))
          target_x, target_y = settarget(4)
      
        time.sleep(0.2) 
      

      Final Result

      Conclusion

      Creating a Machine Learning Model of “Shark Attack: Hungry for Fish” game can be both complex and time-consuming. Through the steps demonstrated in this project, you can create your own Machine Learning Model of automated game. Once trained, you can export the model into the Python Coding Environment, where you can tweak it further to give you the desired output. Try creating a Machine Learning Model of your own today and explore the possibilities of Number Classifier in PictoBlox!

Read More
Learn how to create a dataset and Machine Learning Model for an automated game from the user's input. See how to open the ML environment, upload data, label images, train the model, and export the Python script.

Introduction

In this example project, we are going to create a Machine Learning Model where beetle automatically feed on randomly generated food in space.

Data Collection

  • Now, we are going to collect the data of  “BeetleBot: Autonomous Feeding in Space” game.
  • This data will contain the actions that we have taken to accomplish the game successfully.
  • We will use the data we collect here, to teach our device how to play the “BeetleBot: Autonomous Feeding in Space” game, i.e. to perform machine learning.
  • The data that you collect will get saved in your device as a csv (comma separated values) file. If you open this file in Microsoft Excel, it will look as shown below:

Follow the steps below

  1. Open PictoBlox and create a new file.
  2. Select the coding environment as Python Coding Environment.
  3. Now write code in python.

Code for making dataset

  1. Creates a sprite object named “Beetle”. A sprite is typically a graphical element that can be animated or displayed on a screen.
  2. Creates another sprite object named “Strawberry” and also upload backdrop of “Galaxy”.
  3. Click on the Beetle.py file from the Project files section.
    sprite = Sprite('Beetle')
  4. Similarly, declare new sprite on the Beetle.py file.
    sprite1 = Sprite('Strawberry')
  5. Then we will import the time, random, os, math, TensorFlow as tf  and Pandas as pd modules using the import keyword for using delay in the program later.
    1. Time – For using delay in the program.
    2. Random – For using random position.
    3. Pandas as pd – For using Data Frame.
    4. Os– For reading files from Program files.
      import random
      import time
      import tensorflow as tf
      import pandas as pd
      import os
  6. Now, make 3 variables curr_x, curr_y, ,beetle_m, angle and score with initial values -170, 138, 5,0 and 90 respectively.
    1. curr_x – To store the initial x – position of beetle.
    2. curr_y – To store the initial y – position of beetle.
    3. beetle_m– To store increment value in movement of beetle on pressing specific key.
    4. angle – To store initial angle of beetle.
    5. score – To store the score while playing the game.
      curr_x = -170
      curr_y = 138
      mov_f= 5
      score = 0
      angle = 90
  7. Now set initial position and angle of beetle.
    sprite.setx(curr_x)
    sprite.sety(curr_y)
    sprite.setdirection(DIRECTION=angle)
  8. Now, make a function settarget() in which we are generating food at a random position. We pass one argument “t” in the function for generating target food in the greed position of the t gap.
    1. x and y – To generate the food at random position on stage.
    2. time.sleep – For giving the time delay.
    3. sprite1.set()– To Set the position of food at random position on stage.
      def settarget(t):
      x = random.randrange(-200, 200, t)
      y = random.randrange(-155, 155, t)
      time.sleep(0.1)
      sprite1.setx(x)
      sprite1.sety(y)
      return x, y 
  9. Now set the target (Strawberry). In this, beetle are chasing the target, and target_x  and  target_y should be equal to the x and y positions of the food.
    target_x, target_y = settarget(40) 
  10. Now create a data frame of name “Chase_Data.csv” to collect the data for machine learning and if this name csv exist then directly add data in it.
    if(os.path.isfile('Chase_Data.csv')):
      data=pd.read_csv('Chase_Data.csv')
    else:
      data = pd.DataFrame({"curr_X": curr_x, "curr_Y": curr_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "Action": "RIGHT"}, index=[0])
    
  11. After that, we will use the while True loop to run the code indefinitely. Don’t forget to add a colon ‘:’ just after the loop to avoid errors.
    while True:
  12. Now write the script for moving the Beetle in upward direction, downward direction, left direction and right direction by fix value with the help of a conditional statement.
    1. If the up arrow key is pressed then beetle will move beetle_m position in y direction by adding fix value in beetle’s y position.
    2. After pressing the up arrow key action taken should be stored in the Data frame with data.append command.
       if sprite.iskeypressed("up arrow"):
          data = data.append({"curr_X": curr_x, "curr_Y": curr_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "Action": "UP"}, ignore_index=True)
          curr_y = curr_y + beetle_m
          sprite.setdirection(DIRECTION=0)
          sprite.setx(curr_x)
          sprite.sety(curr_y)
  13. Repeat the process for the set direction and position of beetle for down, left and right movement.
    if sprite.iskeypressed("down arrow"):
        data = data.append({"curr_X": curr_x, "curr_Y": curr_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "Action": "DOWN"}, ignore_index=True)
        curr_y = curr_y - beetle_m
        sprite.setdirection(DIRECTION=-180)
        sprite.setx(curr_x)
        sprite.sety(curr_y) 
        
      if sprite.iskeypressed("left arrow"):
        data = data.append({"curr_X": curr_x, "curr_Y": curr_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "Action": "LEFT"}, ignore_index=True)
        curr_x = curr_x - beetle_m
        sprite.setdirection(DIRECTION=-90)
        sprite.setx(curr_x)
        sprite.sety(curr_y)
         
      if sprite.iskeypressed("right arrow"):
        data = data.append({"curr_X": curr_x, "curr_Y": curr_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "Action": "RIGHT"}, ignore_index=True)
        curr_x = curr_x + beetle_m
        sprite.setdirection(DIRECTION=90)
        sprite.setx(curr_x)
        sprite.sety(curr_y)
  14. Write the conditional statement for the storing data in csv file after few score.
    if(score>0 and score%10==0):
        data.to_csv('Chase_Data.csv',index=False)
  15. Again write the conditional statement for the score variable if the fish and food position difference is less then 20 then the score should be increased by one.
    if abs(curr_x-target_x)<20 and abs(curr_y-target_y)<20: 
        score = score + 1 
        sprite.say(("your score is: {}".format(score)))
  16. If the score is equal to or greater than 20 then data should be printed on Chase Data.csv file.
    if (score >= 20):
          print(data)
          data.to_csv('Chase Data.csv')
          break
    target_x, target_y = settarget()
  17. The final code is as follows:
    sprite = Sprite('Fish')
    sprite1 = Sprite('Orange')
     
    import random
    import time
    import numpy as np
    import tensorflow as tf
    import pandas as pd
    import os
    		
    curr_x = -170
    curr_y = 138
    score=0
    angle=90
    beetle_m=5
    
    sprite.say(("your score is: {}".format(score)))
    sprite.setx(-170)
    sprite.sety(138)
    sprite.setdirection(DIRECTION=angle)
    
    def settarget(t):
      x = random.randrange(-200, 200, t)
      y = random.randrange(-155, 155, t)
    
      time.sleep(0.1)
      sprite1.setx(x)
      sprite1.sety(y)
      return x, y
    
    target_x, target_y = settarget(40)
    if(os.path.isfile('Chase_Data.csv')):
      data=pd.read_csv('Chase_Data.csv')
    else:
      data = pd.DataFrame({"curr_X": curr_x, "curr_Y": curr_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "Action": "RIGHT"}, index=[0])
    
    while True:
      if sprite.iskeypressed("up arrow"):
        data = data.append({"curr_X": curr_x, "curr_Y": curr_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "Action": "UP"}, ignore_index=True)
        curr_y = curr_y + beetle_m
        sprite.setdirection(DIRECTION=0)
        sprite.setx(curr_x)
        sprite.sety(curr_y)
    
      if sprite.iskeypressed("down arrow"):
        data = data.append({"curr_X": curr_x, "curr_Y": curr_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "Action": "DOWN"}, ignore_index=True)
        curr_y = curr_y - beetle_m
        sprite.setdirection(DIRECTION=-180)
        sprite.setx(curr_x)
        sprite.sety(curr_y) 
        
      if sprite.iskeypressed("left arrow"):
        data = data.append({"curr_X": curr_x, "curr_Y": curr_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "Action": "LEFT"}, ignore_index=True)
        curr_x = curr_x - beetle_m
        sprite.setdirection(DIRECTION=-90)
        sprite.setx(curr_x)
        sprite.sety(curr_y)
         
      if sprite.iskeypressed("right arrow"):
        data = data.append({"curr_X": curr_x, "curr_Y": curr_y, "tar_x": target_x, "tar_y": target_y, "diff_x":curr_x-target_x, "diff_y":curr_y-target_y, "Action": "RIGHT"}, ignore_index=True)
        curr_x = curr_x + beetle_m
        sprite.setdirection(DIRECTION=90)
        sprite.setx(curr_x)
        sprite.sety(curr_y)
        
      if(score>0 and score%10==0):
        data.to_csv('Chase_Data.csv',index=False)
        
      if abs(curr_x-target_x)<20 and abs(curr_y-target_y)<20:
        score = score + 1
        sprite.say(("your score is: {}".format(score)))
        if (score >= 20):
          data.to_csv('Chase_Data.csv',index=False)
          break
        target_x, target_y = settarget(40)
  18. Press the Run button and play “BeetleBot: Autonomous Feeding in Space” game to collect data.
  19. Store this dataset on your local computer.

    Numbers(C/R) in Machine Learning Environment

    Datasets on the internet are hardly ever fit to directly train on. Programmers often have to take care of unnecessary columns, text data, target columns, correlations, etc. Thankfully, PictoBlox’s ML Environment is packed with features to help us pre-process the data as per our liking.

    Let’s create the ML model.

    Opening Image Classifier Workflow

    Alert: The Machine Learning Environment for model creation is available in the only desktop version of PictoBlox for Windows, macOS, or Linux. It is not available in Web, Android, and iOS versions.

    Follow the steps below:

    1. Open PictoBlox and create a new file.
    2. Select the coding environment as Block Coding Environment.
    3. Select the “Open ML Environment” option under the “Files” tab to access the ML Environment.
    4. You’ll be greeted with the following screen.
      Click on “Create New Project“.
    5. You shall see the Numbers C/R workflow with an option to either “Upload Dataset” or “Create Dataset”.

      Uploading/Creating Dataset

      Datasets can either be uploaded or created on the ML Environment. Lets see how it is done.

      Uploading a dataset
      1. To upload a dataset, click on the Upload Dataset button and the Choose CSV from your files button.
        Note: An uploaded dataset must be a “.csv” file.
      2. Once uploaded the first 50 rows of the uploaded CSV document will show up in the window.

      3. If you look at the output column, all the values are currently “0”. Hence, first we need to create an output column.
        1. In the Dataset table, click on the tick near Select All to de-select all the columns.
        2. click on the tick of Action column to select it. We will make this column the output.
        3. The output column must always be numerical. Hence click on the button Text to Number to convert the data within this column to numerical type.
        4. Now select it again and press the Set as Output button to set this column as Output.
        5. There is also many which is not useful in training our model and needs to be disable. So select it and click the Disable button in the Selected columns section.

          Creating a Dataset
          1. To create a dataset, click on the Create Dataset button.
          2. Select the number of rows and columns that are to be added and click on the Create button. More rows and columns can be added as and when needed.

          Notes:

          1. Each column represents a feature. These are the values used by the model to train itself.
          2. The “Output” column contains the target values. These are the values that we expect the model to return when features are passed.
          3. The window only shows the first 50 rows of the dataset.
          4. Un-check the “Select All” checkbox to un-select all the columns.

          Training the Model

          After data is pre-processed and optimized, it’s fit to be used in model training. To train the model, simply click the “Train Model” button found in the “Training” panel.

          By training the model, meaningful information is extracted from the numbers, and that in turn updates the weights. Once these weights are saved, the model can be used to make predictions on data previously unseen.

          The model’s function is to use the input data and predict the output. The target column must always contain numbers.

          However, before training the model, there are a few hyperparameters that need to be understood. Click on the “Advanced” tab to view them.

          There are three hyperparameters that can be altered in the Numbers(C/R) Extension:

          1. Epochs– The total number of times the data will be fed through the training model. Therefore, in 10 epochs, the dataset will be fed through the training model 10 times. Increasing the number of epochs can often lead to better performance.
          2. Batch Size– The size of the set of samples that will be used in one step. For example, if there are 160 data samples in the dataset, and the batch size is set to 16, each epoch will be completed in 160/16=10 steps. This hyperparameter rarely needs any altering.
          3. Learning Rate– It dictates the speed at which the model updates the weights after iterating through a step. Even small changes in this parameter can have a huge impact on the model performance. The usual range lies between 0.001 and 0.0001.
          Note: Hover the mouse pointer over the question mark next to the hyperparameters to see their description.

          It’s a good idea to train a numeric classification model for a high number of epochs. The model can be trained in both JavaScript and Python. In order to choose between the two, click on the switch on top of the Training panel.

          Alert: Dependencies must be downloaded to train the model in Python, JavaScript will be chosen by default.

          The accuracy of the model should increase over time. The x-axis of the graph shows the epochs, and the y-axis represents the accuracy at the corresponding epoch.

          A window will open. Type in a project name of your choice and select the “Numbers(C/R)” extension. Click the “Create Project” button to open the Numbers(C/R) window.

          Testing the Model

          To test the model, simply enter the input values in the “Testing” panel and click on the “Predict” button.

          The model will return the probability of the input belonging to the classes.

      Export in Python Coding

      Click on the “PictoBlox” button, and PictoBlox will load your model into the Python Coding Environment if you have opened the ML Environment in Python Coding.

    Code

    1. Creates a sprite object named “Beetle”. A sprite is typically a graphical element that can be animated or displayed on a screen.
    2. Creates another sprite object named “Strawberry” and also upload backdrop of “Space” .
    3. Click on the Beetle.py file from the Project files section.
      sprite = Sprite('Beetle')
    4. Similarly, declare new sprite on the Beetle.py file.
      sprite1 = Sprite('Strawberry')
    5. Then we will import the time, random, TensorFlow as tf  and Pandas as pd modules using the import keyword for using delay in the program later.
      1. Time – For using delay in the program.
      2. Random – For using random position.
      3. Pandas as pd – For using Data Frame.
      4. TensorFlow – For loading model.
        import random
        import time
        import tensorflow as tf
        import pandas as pd
    6. Now, make 3 variables curr_x, curr_y, beetle_m, angle and score with initial values -170, 138, 15, 90 and 0 respectively.
      1. curr_x – To store the initial x – position of beetle.
      2. curr_y – To store the initial y – position of beetle.
      3. beetle_m – To store increment value in movement of beetle on pressing specific key.
      4. angle – To store initial angle of beetle.
      5. score – To store the score while playing the game.
        curr_x = -170
        curr_y = 138
        beetle_m= 15
        angle = 90
        score = 0
    7. Now set initial position and angle of beetle.
      sprite.setx(curr_x)
      sprite.sety(curr_y)
      sprite.setdirection(DIRECTION=90)
    8. Now, make a function settarget() in which we are generating strawberry at a random position. We pass one argument “t” in the function for generating target strawberry in the greed position of the t gap.
      1. x and y – To generate the food(strawberry) at random position on stage.
      2. time.sleep – For giving the time delay.
      3. sprite1.set()– To Set the position of food at random position on stage.
        def settarget(t):
        x = random.randrange(-200, 200, t)
        y = random.randrange(-155, 155, t)
        time.sleep(0.1)
        sprite1.setx(x)
        sprite1.sety(y)
        return x, y 
    9. Now set the position of strawberry. In this, beetle are chasing the strawberry, and target_x  and  target_y should be equal to the x and y positions of the strawberry.
      target_x, target_y = settarget(40) 
    10. Now, make a function runprediction() in which we are predicting class (Left, Up, Right, Down) by taking argument from user . We pass three arguments “diff_x”, “diff_y” in the function.
      1. inputvalue – To store input parameters of function in array.
      2. model.predict() – For predicting output from trained model.
      3. np.argmax(,)– To find the most probable prediction output.
        def runprediction(diff_x, diff_y):
          inputValue=[diff_x, diff_y]
          #Input Tensor
          inputTensor = tf.expand_dims(inputValue, 0)
          #Predict
          predict = model.predict(inputTensor)
          predict_index = np.argmax(predict[0], axis=0)
          #Output
          predicted_class = class_list[predict_index]
          return predicted_class
        
    11. After that, we will use the while True loop to run the code indefinitely. Don’t forget to add a colon ‘:’ just after the loop to avoid errors.
      while True:
    12. In while loop find angle of sprite and call runprediction function by passing arguments in it.
      angle=sprite.direction()
       move = runprediction(curr_x- target_x, curr_y-target_y, angle)
    13. Now write the script for moving the Beetle in forward direction, Upward direction, Left direction and Right direction by fix value with the help of a conditional statement.
      1. If the predicted value is “UP” then beetle will move beetle_m position in direction=0.
      2. If the predicted value is “LEFT” then beetle will move beetle_m position in direction=-90.
      3. If the predicted value is “RIGHT” then beetle will move beetle_m position in direction=90.
      4. If the predicted value is “DOWN” then beetle will move beetle_m position in direction=-180.
        if move == "UP":
            curr_y = curr_y + beetle_m
            sprite.setdirection(DIRECTION=0)
            sprite.setx(curr_x)
            sprite.sety(curr_y)
        
          if move == "LEFT":
            curr_x = curr_x - beetle_m
            sprite.setdirection(DIRECTION=-90)
            sprite.setx(curr_x)
            sprite.sety(curr_y)
        
          if move == "RIGHT":
            curr_x = curr_x + beetle_m
            sprite.setdirection(DIRECTION=90)
            sprite.setx(curr_x)
            sprite.sety(curr_y)
        
         if move == "DOWN":
            curr_y = curr_y - beetle_m
            sprite.setdirection(DIRECTION=-180)
            sprite.setx(curr_x)
            sprite.sety(curr_y) 
    14.  Again write the conditional statement for the score variable if the beetle and strawberry position difference is less then 20 then the score should be increased by one also set new position of target.
      if abs(curr_x-target_x)<20 and abs(curr_y-target_y)<20: 
          score = score + 1 
          sprite.say(("your score is: {}".format(score)))
      target_x, target_y = settarget()
    15.  Now add delay function for delaying movement by 0.02 seconds.
      time.sleep(0.02)
    16. The final code is as follows:
      sprite = Sprite('Beetle')
      sprite1 = Sprite('Strawberry')
       
      import random
      import time
      import numpy as np
      import tensorflow as tf
      
      #Load Number Model
      model= tf.keras.models.load_model(
      		"num_model.h5", 
      		custom_objects=None, 
      		compile=True, 
      		options=None)
      		
      #List of classes
      class_list = ['RIGHT','LEFT','DOWN','UP',]  
      		
      curr_x = -170
      curr_y = 138
      score=0
      beetle_m=30
      angle=90
      
      sprite.say(("your score is: {}".format(score)))
      sprite.setx(-170)
      sprite.sety(138)
      sprite.setdirection(DIRECTION=angle)
      
      def settarget():
        x = random.randrange(-200, 200, 1)
        y = random.randrange(-155, 155, 1)
        time.sleep(0.1)
        sprite1.setx(x)
        sprite1.sety(y)
        return x, y
      
      target_x, target_y = settarget()
      
      def runprediction(diff_x, diff_y):
        inputValue=[diff_x, diff_y]
        #Input Tensor
        inputTensor = tf.expand_dims(inputValue, 0)
        #Predict
        predict = model.predict(inputTensor)
        predict_index = np.argmax(predict[0], axis=0)
        #Output
        predicted_class = class_list[predict_index]
        return predicted_class
      
      while True:
        
        move = runprediction(curr_x- target_x, curr_y-target_y)
      
        if move == "UP":
          curr_y = curr_y + beetle_m
          sprite.setdirection(DIRECTION=0)
          sprite.setx(curr_x)
          sprite.sety(curr_y)
      
        if move == "DOWN":
          curr_y = curr_y - beetle_m
          sprite.setdirection(DIRECTION=-180)
          sprite.setx(curr_x)
          sprite.sety(curr_y) 
      
        if move == "LEFT":
          curr_x = curr_x - beetle_m
          sprite.setdirection(DIRECTION=-90)
          sprite.setx(curr_x)
          sprite.sety(curr_y)
           
      
        if move == "RIGHT":
          curr_x = curr_x + beetle_m
          sprite.setdirection(DIRECTION=90)
          sprite.setx(curr_x)
          sprite.sety(curr_y)
       
      
        if abs(curr_x-target_x)<20 and abs(curr_y-target_y)<20:
          score = score + 1
          sprite.say(("your score is: {}".format(score)))
          target_x, target_y = settarget()
      
        time.sleep(0.2) 
      

      Final Result

      Conclusion

      Creating a Machine Learning Model of “Beetle’s Cosmic Feast: Strawberry Space Adventure” can be both complex and time-consuming. Through the steps demonstrated in this project, you can create your own Machine Learning Model of automated game. Once trained, you can export the model into the Python Coding Environment, where you can tweak it further to give you the desired output. Try creating a Machine Learning Model of your own today and explore the possibilities of Number Classifier in PictoBlox!

Read More
All articles loaded
No more articles to load