Table of Contents

Use an image classification model from Teachable Machine to recognise images and make Marty respond in Blocks. The model uses your computer or tablet's camera; Marty needs to be connected when you want the robot to perform an action.

New to Teachable Machine? Start with its videos on gathering samples and training a model. For a model built directly in Blocks, see Machine Learning in Blocks.

Step 1: Create an image classification model on Teachable Machine

  1. Open Teachable Machine and select Get Started.
  2. Choose Image Project, then Standard image model.
  3. Name your classes, collect a variety of image samples for each class, and select Train Model.
  4. Test the model with new images before exporting it.

Step 2: Export your model from Teachable Machine

  1. Select Export Model in the Preview panel.
  2. Keep Tensorflow.js selected and choose Upload (shareable link).
  3. Select Upload my model. When the upload finishes, copy Your sharable link.

Teachable Machine export options with Upload shareable link selected

Copy the shareable model link after uploading

Copy the model link, which has the form https://teachablemachine.withgoogle.com/models/…/. You do not need to copy the JavaScript code shown below it.

Step 3: Open Machine Learning in Blocks

  1. Open Blocks.
  2. Add and select your Marty device. Connect it if you want to use robot actions.
  3. Open the Machine Learning tab.
  4. Open the Add New Model menu at the bottom left and choose Load TM Model.

Step 4: Import your Teachable Machine model

Paste the shareable link into Paste TM model URL here, then select Load.

Load Teachable Machine Model dialog in current Blocks

Wait for the imported model to appear in the Models list. It is already trained, so you do not need to train it again in Blocks. Keep the same Marty device selected when you return to Code: the imported model belongs to that device in your project.

Step 5: Make classifications in Blocks

In Code, choose Machine Learning. Drag Load Machine Learning model into your program and select the imported model from its dropdown. Place it below when green flag clicked, then click the green flag to load it before using the prediction blocks.

Load an imported image model when the green flag is clicked

Allow Blocks to use your camera when prompted. Loading an image model starts continuous classification. Use turn classification off/on to pause or resume it; classify image requests an image classification.

The examples below use a model named Image example with a Rock class. Choose your own model and class names. These are alternative ways to respond to a prediction:

  • Event: attach an action below when received image label: [class].
  • Boolean: use image [class] detected as the condition of an if block. An if block checks once; put it inside a forever loop if you want to keep checking.
  • Label reporter: compare image label with a class name using the = operator, then use that comparison as a condition.

Alternative image prediction patterns using an event, an if condition, and an image label comparison

Tick the checkbox beside image label in the palette to display the latest label. It may be empty when no prediction meets the confidence threshold.

Step 6: Show video from camera (optional)

Use turn video on to show the camera preview on the stage. Select Switch to large stage beside the green flag and stop controls for a larger view. turn video off hides the stage preview; use turn classification off separately when you want to pause predictions.

Step 7: Changing the classification rate and confidence threshold (optional)

  • Label once every [seconds] seconds changes the interval between image classifications.
  • set confidence threshold sets the minimum confidence needed to report a label. The default is 0.5, or 50%. Increase it to require a more confident prediction.

Blocks to pause classification, set confidence, change the image classification interval, and hide video

To keep your imported model and code, save the project from Save/Load.