# Raspberry Pi + Teachable Machine = Teachable Pi

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/087/626/medium800thumb/camera_output.jpg?1580094745)

In our [previous](https://blog.adafruit.com/2019/09/04/new-guide-running-tensorflow-lite-object-recognition-on-the-raspberry-pi-4-adafruit-adafruitlearningsystem-adafruitio-tensorflow-raspberry_pi-adafruit-makermelissa/) [demos](https://blog.adafruit.com/2019/09/09/machine-learning-monday-mobilenet-v4-ssd-lite-on-raspberry-pi-4-adafruit-raspberry_pi-tensorflow-machinelearning-tinyml-raspberrypi/), we got Google's Object Detection models running on a Raspberry Pi so that you could build your own smart camera! There's just one thing--what if you want to recognize something else besides coffee cups and people? Until recently, this was challenging; either it required a complex Colaboratory notebook or a long series of local commands to set up a local TensorFlow environment.

Google recently released a tool called [Teachable Machine](https://teachablemachine.withgoogle.com/) that automates the whole process for you! You bring some example images, configure a few parameters on their web page, and voila! Google produces a custom model trained against your own objects.

This guide walks you through the process of configuring Teachable Machine, downloading the generated model to Raspberry Pi 4, and running the model on your Pi.

# You will need

You'll need the same equipment as used in our [Running TensorFlow Lite Object Recognition on the Raspberry Pi 4](https://learn.adafruit.com/running-tensorflow-lite-on-the-raspberry-pi-4) guide. 

# Raspberry Pi + Teachable Machine = Teachable Pi

## Visit Teachable Machine



# Setting up Teachable Machine

To start with, visit the [Teachable Machine](https://teachablemachine.withgoogle.com/) page on your **desktop** or **laptop** (not the Pi--it's too slow).

Click **Get Started**, then choose **Image Project** from the first screen, and you'll be presented with this empty project screen:

![](https://cdn-learn.adafruit.com/assets/assets/000/087/325/medium800/camera_teachable-machine-main.png?1579673029)

Now it's ready for you to input samples. Before you start, have a look at the guidelines for choosing and capturing samples below.

# Choosing Samples

Now you need to find example images that include the objects you want to recognize. This can be the both the most important and most time-consuming part of retraining a machine learning model, so choose wisely! 

Here are some tips for choosing example images:

* **Choose realistic images**. Your model won't behave predictably in scenarios it hasn't seen before.
* **Choose varied images**. It's really important that the only thing in common between the images is the thing you want to detect. As an example, if all of the images in one category have a blue background, the model might simply learn that a blue background indicates that category.
* **Choose representative images**. Make sure you include images that show all different angles of the object you'd like to recognize.
* **Include an "empty" category**. Try including a category that the machine can select when it isn't confident in any of the choices. This should have as much background variation as possible, and shouldn't include any objects you want to recognize.

# Raspberry Pi + Teachable Machine = Teachable Pi

## Create Categories



Now you need to create categories in the Teachable Machine UI to match the objects you've chosen. Usually, you should create one category for each distinct type of object you want to recognize--so, if you want to train your model to recognize different clothes, you might create categories for *pants*, *dresses*, *shirts*, and *socks*. 

You can also try to train a smarter model, like one that looks for blue shirts or button-down shirts. But beware, this is advanced and we don't suggest you start with this.

Also, be sure to create a *nothing* category--one that the machine can choose when it doesn't see anything. You'll need to find some example images that don't contain any of your chosen objects.

## Adding Categories to your Teachable Machine project

Each box on the left of your page represents a different category. Change the name of each as follows:

Click the _pencil_ icon, then name the category.

![camera_name-category.png](https://cdn-learn.adafruit.com/assets/assets/000/087/629/medium640/camera_name-category.png?1580096767)



Press **Enter** to accept the name. The box should look like this (e.g. for the _transistor_ category).

![camera_upload.png](https://cdn-learn.adafruit.com/assets/assets/000/086/900/medium640/camera_upload.png?1580096731)

Repeat these steps for each of the other objects you'd like to recognize. When you're finished, your screen should look like this:

![](https://cdn-learn.adafruit.com/assets/assets/000/087/628/medium800/camera_tm-categories-empty.png?1580095931)

# Raspberry Pi + Teachable Machine = Teachable Pi

## Use Computer Webcam

# Capturing Samples

You can capture either from your computer's webcam or from the Pi's camera. When you use your computer's webcam, Teachable Machine captures the samples directly from the browser--super quick and easy! [When you use the Pi camera](https://learn.adafruit.com/teachable-raspberry-pi/use-raspberry-pi-camera), you'll need to run a script to generate the samples, transfer them to your computer, and upload them.

For best results, you should capture samples using the camera you want to use. But, a MacBook Pro webcam worked well enough for us, and it's much easier to use. So, we suggest you first try to use your computer webcam, and [use the Pi camera](https://learn.adafruit.com/teachable-raspberry-pi/use-raspberry-pi-camera) if the model doesn't work well on the Pi.

# Before you start

Be sure you've followed the steps in [Create Categories](https://learn.adafruit.com/teachable-raspberry-pi/create-categories) first.

Though this method should work well enough, be warned that the model might not perform differently on the Pi because it uses a different camera. If this happens, you can try the [next method](https://learn.adafruit.com/admin/guides/2858/editor/18466) to capture samples directly from the Pi camera.

# Capturing the Samples
You should have Teachable Machine up on your laptop or desktop to a screen like this:

![](https://cdn-learn.adafruit.com/assets/assets/000/087/630/medium800/camera_tm-categories-empty.png?1580096227)

Now, click the Webcam button. You might need to allow the page access to your computer camera.

![camera_click-webcam-button.png](https://cdn-learn.adafruit.com/assets/assets/000/087/327/medium640/camera_click-webcam-button.png?1580096272)

Click and hold the **Hold to Record** button to capture samples!

![camera_webcam-record-cropped.png](https://cdn-learn.adafruit.com/assets/assets/000/087/647/medium640/camera_webcam-record-cropped.png?1580105849)

Samples will appear next to the webcam image.

![camera_webcam-record-done-cropped.png](https://cdn-learn.adafruit.com/assets/assets/000/087/648/medium640/camera_webcam-record-done-cropped.png?1580105949)

 Try to capture as many unique samples as you can. Remember:
  - Try to capture around 200 samples per category.
  - Capture different angles and light levels
  - Make sure the samples are as representative of the object as they can be
  - Try to move the camera around so that the only thing in common between all of the sample images for a given object is the object itself.


# Raspberry Pi + Teachable Machine = Teachable Pi

## Use Raspberry Pi Camera

# Before you start

Be sure you've followed the steps in [Create Categories](https://learn.adafruit.com/teachable-raspberry-pi/create-categories) first.

# Setting up the Raspberry Pi

First, follow the instructions in [Raspberry Pi Setup](https://learn.adafruit.com/teachable-raspberry-pi/raspberry-pi-setup). This will load the software onto the Pi.

# Launch the capture script

SSH to the pi, or open a Terminal. Then, run these commands to set up the shell:

```shell
$ cd rpi-vision
$ sudo bash
# source .venv/bin/activate
```
    
Get ready to capture the first samples. Then, at the same shell, type:

```shell
# python3 tests/pitft_capture.py 
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html

Ready. Press ENTER to toggle capture/standby. Press Ctrl+C to quit.    
```

Info: 

At this point, the Raspberry Pi display should look like this.

![camera_idle-capture-screen.png](https://cdn-learn.adafruit.com/assets/assets/000/087/329/medium640/camera_idle-capture-screen.png?1579674425)

## Record some data

At this point, the **STANDBY** text indicates that the Pi is not recording data. Make sure the camera is pointed at your object, then press **ENTER** to toggle between **STANDBY** and **RECORD** modes.

In **RECORD** mode, the script is capturing frames and saving them to the Pi's SD card. The display looks like this.

Press **ENTER** to toggle between **STANDBY** and **RECORD** modes.

![camera_capture.png](https://cdn-learn.adafruit.com/assets/assets/000/087/330/medium640/camera_capture.png?1579674556)

Warning: 

When finished, press **CTRL + C** to stop the process.

# Copy images to your desktop/laptop computer

Here we show you how to use either `samba` or `scp` to copy the generated tar to your desktop/laptop for use with Teachable Machine.

## Using Samba

The easiest way is to setup your Raspberry Pi as a file server using **Samba**. Follow [these steps](https://magpi.raspberrypi.org/articles/raspberry-pi-samba-file-server).

Now, copy the generated tarball into your samba share:
```
$ cp ~/rpi_vision/&lt;&lt;OBJECT_NAME&gt;&gt;.tar /share
```

On your local desktop, look for the `RASPBERRYPI` machine on your network. Connect to the share and copy the tar to your local machine.

![camera_rpi-network.png](https://cdn-learn.adafruit.com/assets/assets/000/087/641/medium640/camera_rpi-network.png?1580098660)

## Using scp

On your local machine (i.e. not the raspberry pi), run:

    $ scp pi@raspberrypi.local:~/rpi_vision/&lt;&lt;OBJECT_NAME&gt;&gt;.tar .


Be sure to change ``&lt;&lt;OBJECT_NAME&gt;&gt;.tar`` to the actual path you specified to your `pitft_capture.py` command above.

# Upload images to Teachable Machine

Extract the tar by double-clicking it. On Windows, you may need a utility like [7-zip](https://www.7-zip.org/).

Now, in the *Teachable Machine* UI, follow these steps:

click the **Upload** button next to the class box these images belong to.

![camera_click-upload-button.png](https://cdn-learn.adafruit.com/assets/assets/000/087/638/medium640/camera_click-upload-button.png?1580097731)

Now **drag-n-drop** the images from your file browser onto the dialog.

You can also click on the folder to find them from within the browser.

![camera_upload-drag-n-drop.png](https://cdn-learn.adafruit.com/assets/assets/000/087/639/medium640/camera_upload-drag-n-drop.png?1580097798)

When done, the box looks like this.

![camera_upload-added-images.png](https://cdn-learn.adafruit.com/assets/assets/000/087/640/medium640/camera_upload-added-images.png?1580097955)

Repeat this process for all categories in your project.

# Raspberry Pi + Teachable Machine = Teachable Pi

## Training

When you've finished the last page (either [Use Computer Webcam](https://learn.adafruit.com/teachable-raspberry-pi/use-computer-webcam) or [Use Raspberry Pi Camera](https://learn.adafruit.com/teachable-raspberry-pi/use-raspberry-pi-camera), your screen should look like the image below. Now you're ready to start training. 

![](https://cdn-learn.adafruit.com/assets/assets/000/087/643/medium800/camera_train.png?1580105011)

# Run Training

Follow these steps to run the training process.

Now click **Train** and _leave the tab open and visible_. The training process happens in Javascript on your own machine, and will be slow or stalled if the browser window is left in the background.

![camera_click-train-button.png](https://cdn-learn.adafruit.com/assets/assets/000/087/646/medium640/camera_click-train-button.png?1580105190)

Warning: 

While training is in progress, you'll see a progress bar. Training should take around **5 minutes** or less using the default **50** epochs.

![camera_training-in-progress.png](https://cdn-learn.adafruit.com/assets/assets/000/087/645/medium640/camera_training-in-progress.png?1580105115)

## Previewing

After training, you can use your computer's webcam to try out the model. You can also upload images taken from the Pi.	

![](https://cdn-learn.adafruit.com/assets/assets/000/086/904/medium800/camera_preview.png?1579025810)

## Tweaking advanced settings

Sometimes your model may not perform as well as you like on the first run. There are a couple of things you can do in this situation:

* **Run for more epochs**. This is the easiest thing--just click the *Advanced* drop-down in the *Training* box, and change the number of epochs. Your training time will increase more-or-less linearly with the number of epochs. Try 100 or 200 to start.
* **Use different samples**. This will produce the most change in your results, but takes more time.

# Raspberry Pi + Teachable Machine = Teachable Pi

## Raspberry Pi Setup

Info: 

![](https://cdn-learn.adafruit.com/assets/assets/000/087/323/medium800/camera_setup.png?1579672496)

# Hardware

The camera and touchscreen display are attached to the Raspberry Pi. Plug in the camera first, then the touchscreen. Be sure the Pi is not powered while assembling hardware.

Here are the links to instructions for the hardware we use in this guide:

* [**Camera Install Instructions**](https://projects.raspberrypi.org/en/projects/getting-started-with-picamera/3)
* **Touchscreen Installation**: The [PiTFT](https://www.adafruit.com/product/1601) just needs to be attached to the 2x20 header so that the board is stacked directly on top of the Pi. The PiTFT module should fit snugly enough in the header port that you shouldn't need to attach any spacers. 

## Stabilizing the camera

Attach the camera to a larger, more stable object so that it doesn't bounce around on the cable. I taped the camera to the box it shipped in, like so:

![](https://cdn-learn.adafruit.com/assets/assets/000/087/333/medium800/camera_setup.png?1579675059)

# Software

Follow the [Initial Setup](https://learn.adafruit.com/running-tensorflow-lite-on-the-raspberry-pi-4/initial-setup) and [TensorFlow Lite 2.0 Setup](https://learn.adafruit.com/running-tensorflow-lite-on-the-raspberry-pi-4/tensorflow-lite-2-0-setup) steps. You can stop when you reach the **Running the Graphic Labelling Demo** section.	

# Raspberry Pi + Teachable Machine = Teachable Pi

## Running on the Pi

## Exporting the SavedModel

The first step is to export your shiny new model in **SavedModel** format. This makes it easy to transfer all of the files needed to the Pi, since it comes in a zip file.

Click **Export Model** in the Preview box.

![camera_export-model.png](https://cdn-learn.adafruit.com/assets/assets/000/086/905/medium640/camera_export-model.png?1579025972)

In the dialog that appears, choose **TensorFlow** and be sure the **SavedModel** button is selected, as shown below. 

Then, click **Download my model**. After a minute or two, your browser will save a ``converted_savedmodel.zip`` file.

![](https://cdn-learn.adafruit.com/assets/assets/000/086/906/medium800/camera_export-dialog.png?1579026120)

## Running the model on the Pi

When you clicked the **Download my model** button in the last step, your browser should have saved a file called ``converted_savedmodel.zip``. Now you need to place this on your Raspberry Pi.

You can transfer the model to the Raspberry Pi using ``scp``:

    $ scp &lt;&lt;PATH_TO_DOWNLOADS&gt;&gt;/converted_savedmodel.zip pi@raspberrypi.local:~

**NOTE**: You might need to adjust the first path in the command above to point to your Downloads folder.

Now, SSH to your Pi and run:

    $ cd rpi-vision
    $ sudo bash
    # source .venv/bin/activate
    # python3 tests/pitft_teachablemachine.py ../converted_savedmodel.zip
    
It will take some time to load the model into memory. During this time, you'll see the BrainCraft logo appear on the display. Afterwards, point your rpi camera and try it out!	

Warning: Be sure to follow these steps to get a file called **pitft_teachablemachine.py**. This file is not in the project GitHub repo, it is made from the training and part of the process noted above.

Info: If you get an error that a module like ```pygame``` or ```rpi_vision``` isn't found, please use pip or pip3 to load the module for Python use.

![](https://cdn-learn.adafruit.com/assets/assets/000/086/908/medium800thumb/camera_output.jpg?1579028491)


## Featured Products

### Raspberry Pi 4 Model B - 4 GB RAM

[Raspberry Pi 4 Model B - 4 GB RAM](https://www.adafruit.com/product/4296)
The Raspberry Pi 4 Model B is the newest Raspberry Pi computer made, and the Pi Foundation knows you can always make a good thing _better_! And what could make the Pi 4 better than the 3? How about a&nbsp;_faster_ processor, USB 3.0 ports, and updated Gigabit Ethernet chip with...

In Stock
[Buy Now](https://www.adafruit.com/product/4296)
[Related Guides to the Product](https://learn.adafruit.com/products/4296/guides)
### Aluminum Metal Heatsink Raspberry Pi 4 Case with Dual Fans

[Aluminum Metal Heatsink Raspberry Pi 4 Case with Dual Fans](https://www.adafruit.com/product/4340)
Looking for a way&nbsp;to keep your&nbsp;[Raspberry Pi 4](https://www.adafruit.com/category/1003)&nbsp;running cool _ **and also** _ look cool? We've got a two-birds-one-stone solution for you. This enclosure kit is absolutely perfect, is&nbsp;extra durable,...

In Stock
[Buy Now](https://www.adafruit.com/product/4340)
[Related Guides to the Product](https://learn.adafruit.com/products/4340/guides)
### Adafruit BrainCraft HAT - Machine Learning for Raspberry Pi 4

[Adafruit BrainCraft HAT - Machine Learning for Raspberry Pi 4](https://www.adafruit.com/product/4374)
The idea behind the BrainCraft HAT is that you’d be able to “craft brains” for Machine Learning on the EDGE, with Microcontrollers & Microcomputers. On&nbsp;ASK AN ENGINEER, our founder & engineer chatted with&nbsp;Pete Warden, the technical lead of the mobile,...

In Stock
[Buy Now](https://www.adafruit.com/product/4374)
[Related Guides to the Product](https://learn.adafruit.com/products/4374/guides)
### PiTFT Plus Assembled 320x240 2.8" TFT + Resistive Touchscreen

[PiTFT Plus Assembled 320x240 2.8" TFT + Resistive Touchscreen](https://www.adafruit.com/product/2298)
Is this not the cutest little display for the Raspberry Pi? It features a 2.8" display with 320x240 16-bit color pixels and a resistive&nbsp;touch overlay. The plate uses the high speed SPI interface on the Pi and can use the mini display as a console, X window port, displaying images or...

Out of Stock
[Buy Now](https://www.adafruit.com/product/2298)
[Related Guides to the Product](https://learn.adafruit.com/products/2298/guides)
### PiTFT Plus 320x240 3.2" TFT + Resistive Touchscreen

[PiTFT Plus 320x240 3.2" TFT + Resistive Touchscreen](https://www.adafruit.com/product/2616)
Is this not the cutest little display for the Raspberry Pi? It features a 3.2" display with 320x240 16-bit color pixels and a resistive&nbsp;touch overlay. The plate uses the high speed SPI interface on the Pi and can use the mini display as a console, X window port, displaying images or...

Out of Stock
[Buy Now](https://www.adafruit.com/product/2616)
[Related Guides to the Product](https://learn.adafruit.com/products/2616/guides)
### Raspberry Pi Camera Board v2 - 8 Megapixels

[Raspberry Pi Camera Board v2 - 8 Megapixels](https://www.adafruit.com/product/3099)
Snap, snap! The Camera v2 is the new official camera board released by the Raspberry Pi Foundation!

The **Raspberry Pi Camera Board v2** is a high quality 8 megapixel Sony IMX219 image sensor custom designed add-on board for Raspberry Pi, featuring a fixed focus lens....

In Stock
[Buy Now](https://www.adafruit.com/product/3099)
[Related Guides to the Product](https://learn.adafruit.com/products/3099/guides)
### Raspberry Pi Camera Board

[Raspberry Pi Camera Board](https://www.adafruit.com/product/1367)
The Raspberry Pi Camera Module is a custom designed add-on for Raspberry Pi. It attaches to Raspberry Pi by way of one of the two small sockets on the board upper surface. This interface uses the dedicated CSI interface, which was designed especially for interfacing to cameras. The CSI bus is...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/1367)
[Related Guides to the Product](https://learn.adafruit.com/products/1367/guides)
### Flex Cable for Raspberry Pi Camera or Display - 24" / 610mm

[Flex Cable for Raspberry Pi Camera or Display - 24" / 610mm](https://www.adafruit.com/product/1731)
This cable will let you swap out the stock 150mm long flex cable from a Raspberry Pi Camera (either 'classic' or 'NoIR' type)&nbsp;or Raspberry Pi Display for a different size. Works great, just carefully open the connector on the Pi and slip this one in.  
  
This cable...

In Stock
[Buy Now](https://www.adafruit.com/product/1731)
[Related Guides to the Product](https://learn.adafruit.com/products/1731/guides)

## Related Guides

- [Raspberry Pi Care and Troubleshooting](https://learn.adafruit.com/raspberry-pi-care-and-troubleshooting.md)
- [Adafruit BrainCraft HAT - Easy Machine Learning for Raspberry Pi](https://learn.adafruit.com/adafruit-braincraft-hat-easy-machine-learning-for-raspberry-pi.md)
- [DIY WiFi Raspberry Pi Touchscreen Camera](https://learn.adafruit.com/diy-wifi-raspberry-pi-touch-cam.md)
- [Running TensorFlow Lite Object Recognition on the Raspberry Pi 4 or Pi 5](https://learn.adafruit.com/running-tensorflow-lite-on-the-raspberry-pi-4.md)
- [Raspberry Pi YouTube Boombox](https://learn.adafruit.com/youtube-radio.md)
- [Kali Linux on the Raspberry Pi with the PiTFT](https://learn.adafruit.com/kali-linux-on-the-raspberry-pi-with-the-pitft.md)
- [Adafruit PiTFT - 2.8" Touchscreen Display for Raspberry Pi](https://learn.adafruit.com/adafruit-pitft-28-inch-resistive-touchscreen-display-raspberry-pi.md)
- [Processing on the Raspberry Pi & PiTFT](https://learn.adafruit.com/processing-on-the-raspberry-pi-and-pitft.md)
- [Monitor PiCam and temperature on a PiTFT via adafruit.io](https://learn.adafruit.com/monitor-picam-and-temperature-on-a-pitft-via-adafruit-dot-io.md)
- [Using Google Assistant on the BrainCraft HAT or Voice Bonnet](https://learn.adafruit.com/using-google-assistant-on-the-braincraft-hat.md)
- [Build an ML Package Detector with Lobe](https://learn.adafruit.com/build-an-ml-package-detector.md)
- [RasPipe: A Raspberry Pi Pipeline Viewer, Part 2](https://learn.adafruit.com/raspipe-a-raspberry-pi-pipeline-viewer-part-2.md)
- [Running PyPortal Code on Blinka with Displayio](https://learn.adafruit.com/running-pyportal-code-on-blinka-with-displayio.md)
- [Articulated Pi Display V2 Mount](https://learn.adafruit.com/pi-wall-mount.md)
- [SnapPiCam Raspberry Pi Camera](https://learn.adafruit.com/snappicam-raspberry-pi-camera.md)
- [PiGlass v2](https://learn.adafruit.com/piglass-v2-wearable-raspberry-pi-computer.md)
