[Maker’s Note] Teachable Machine AI Model Applied with Raspberry Pi

by Yun-Han Lin

Teachable Machine

Teachable Machine is a service by Google that makes AI experience and application accessible to even people without any knowledge in programming. The following is a brief sharing on how to apply a model trained by Teachable Machine to Raspberry Pi, coupled with Tensorflow Lite.

Hardware accessories and testing

The hardware we use here is Raspberry Pi 4, with Raspberry Pi Camera Module. After the Raspberry is installed and plugged in with the camera module, users can test whether the camera functions well by entering libcamera-still commands at Terminal.

Preparing Raspberry Pi

We prepared packages and sample programs required for the Raspberry Pi to run deep learning, where Tensorflow Lite, a simple framework, was used. The following commands were entered in sequence at Terminal.

  1. git clone https://github.com/tensorflow/examples --depth 1
  2. cd examples/lite/examples/image_classification/raspberry_pi
  3. sh setup.sh

Sample programs were downloaded along the process, and the required packages will be installed at the same time. Users can run the following code to make sure everything works fine:

python3 classify.py

Next, users should be able to activate EfficientNet, a built-in sample program, to start image classification.

Preparing Teachable Machine

After the model training is completed on Teachable Machine, select Export Model >> TensorFlow Lite >> Quantized >> Download my model. It takes time for the processing and export of files, before there is a zip file saved to the computer. After unzipping, there will be two files: labels.txt and model.tflite.


Usually, users only need to put the model built on Teachable Machine into the Raspberry Pi and use a designated model when running the classify.py file, so that the model with users’ self-modification using EfficientNet can work without any problem. However, sometimes, the files may not be compatible. Since December 2021, the sample program provided by Tensorflow requires that the Tflite model be coded with metadata, including the model name and label information. Yet, the models exported over Teachable Machine have not been updated, as the model.tflite files mentioned above do not contain metadata. For this problem, there are two solutions.

Option 1: Add in metadata

It is more troublesome to add metadata to tflite files.

1: Install tensorflow on your computer, and run the code “git clone https://github.com/tensorflow/examples –depth 1” at the terminal.

2: Find the following sample file first:

cd examples/image_classification/metadata/metadata_writer_for_image_classifier.py

Change the “_MODEL_INFO”, including changing file name, name, and num_class.

3: Put your models into the folder titled “examples/image_classification/metadata”

4: Run the following code:

python3 ./metadata_writer_for_image_classifier.py \

--model_file=./model.tflite \

--label_file=./labels.txt \

--export_directory= ./

After the model is exported, use classify.py, the new version of the sample program, to run the customized model.

cd ~/examples/lite/examples/image_classification

python3 raspberry_pi/classify.py \

--model = metadata/model_with_metadata.tflite

Option 2: Use the old version of sample programs

Older versions of sample programs include a file titled “classify_picamera.py”, which can be used with models exported over Teachable Machine. Users can find the files at this link, or visit the history of tensorflow github to look for any Commits established before December 2021. Users should be able to find the file they need under the following path: example/lite/examples/image_classification/raspberry_pi.

After downloading the file in the Raspberry Pi, it should be put into the original path:examples/lite/examples/image_classification/raspberry_pi, along with the models from Teachable Machine.

After that, users can use the following code to run the program.

python3 raspberry_pi/classify_picamera.py \

--model = model.tflite\

--labels = labels.txt

It is hoped that the above tutorial and sharing can help users apply their deep learning model to the Raspberry Pi!



The share account of vmaker editors. Send your work to us:contact@vmaker.tw