Raspberry Pi Tensorflow Lite: Image classification and Object detection – Easy guide

This is an easy and fast guide about how to use image classification and object detection using Raspberry Pi and Tensorflow lite.

The goal of this tutorial about Raspberry Pi Tensorflow Lite is to create an easy guide to run Tensorflow Lite on Raspberry Pi without having a deep knowledge about Tensorflow and Machine Learning. In more detail, we will explore how to use image classification and object detection with Raspberry Pi.

There are several great tutorials covering different features of Tensorflow and how to use them to build Machine Learning projects. Anyway, almost all these tutorials require a deep knowledge of Machine Learning algorithms. This tutorial wants to be an easy guide to explore how to use Tensorflow with Raspberry Pi without coding or knowing Machine Learning algorithms. Therefore, you can start using Machine Learning with Raspberry Pi easily. Maybe later, you can start studying Machine Learning in-depth if this topic interests you.

In this easy guide, we will explore how to use image classification and object detection with Raspberry Pi. To do it, you will need :

  • a Raspberry Pi 3 or 4
  • Raspberry Pi camera

Preparing Raspberry Pi for Tensorflow

Before installing Tensorflow on your Raspberry, we need to update it:

  • Connect to your Raspberry using ssh or vnc
  • open a shell and write the following commands:
sudo apt-get update sudo apt-get upgrade sudo reboot
Code language: Bash (bash)

Now your Raspberry Pi is up to date.

Installing Tensorflow on Raspberry Pi

Now we can install TensorFlow on Raspberry Pi following this guide. Before going on, check your python version:

python3 --version

This is my python version:

Python3 version

According to your python version select in the list the right Tensorflow version. Now you can install Tensorflow using:

sudo pip3 install https://github.com/google-coral/pycoral/releases/download/release-frogfish/tflite_runtime-2.5.0-cp35-cp35m-linux_armv7l.whl
Code language: Bash (bash)

the installation details are shown below:

How to install Tensorflow Lite on Raspberry Pi

Wait until the process completes.

How to add Tensorflow lite examples

Once Tensorflow is installed on your Raspberry Pi, we can download the Tensorflow lite example that we will use to experiment with image classification and object detection. From here on, we suppose you will use the default directory /home/pi. If you use another directory, change the commands shown below. To install the Tensorflow lite examples, you have to clone the git repository:

git clone https://github.com/tensorflow/examples --depth 1
Code language: Bash (bash)

If you are curious, you can explore the cloned repository. There are several examples covering how to use Machine Learning we will focus our attention on:

  • image classification with Raspberry Pi example (under the directory /home/pi/examples/lite/examples/image_classification/raspberry_pi)
  • object detection with Raspberry Pi example (under the directory /home/pi/examples/lite/examples/object_detection/raspberry_pi)

There other examples about how to use Tensorflow Lite with Android or iOS but they are out of the scope of this tutorial.

You might be interested in:
How to use Tensorflow lite with ESP32
Arduino Nano 33 BLE Machine Learning

If you want to exaplore how to use Machine Learning, you can read these tutorials:

Image classification with Raspberry Pi and Tensorflow lite

The first example, we will cover is how to use image classification with Raspberry pi. In simple words, image classification in Deep learning is where a computer, using a camera, analyses an image and selects the class this image belongs to. In image classification, the computer selects the class but also the probability that the image analyzed belongs to this class. Therefore, we can have several classes with different probabilities. A class is a label (such as car, bottle and so on). Let us see how we can use image classification with Raspberry.

The first thing to do is running this script:

./download.sh /home/pi/model
Code language: Bash (bash)

This script download downloads all the required libraries and the model that will be used in the image classification. The /home/pi/model is the directory where the script will download the model. In the end, there will be two files in this directory:

  • mobilenet_v1_1.0_224_quant.tflite (the quantitazed Tensorflow lite model)
  • labels_mobilenet_quant_v1_224.txt (the labels related to this model)

Running image classification on Raspberry Pi

Before running the image classification on Raspberry Pi, it is necessary to do a last step: modify a little bit the example downloaded before so that it uses this lite model. Go to the directory /home/pi/examples/lite/examples/image_classification/raspberry_pi and open the file named classify_picamera.py.

Replace the following line:

from tflite_runtime.interpreter import Interpreter
Code language: Python (python)

with:

import tflite_runtime.interpreter as tflite
Code language: Python (python)

Next, look for:

interpreter = Interpreter(args.model)
Code language: Python (python)

and replace it with:

interpreter = tflite.Interpreter(model_path=args.model)
Code language: Python (python)

That’s all. Now you can run the image classification on Raspberry Pi using:

python3 examples/lite/examples/image_classification/raspberry_pi/classify_picamera.py --model ./model/mobilenet_v1_1.0_224_quant.tflite --labels ./model/labels_mobilenet_quant_v1_224.txt
Code language: Bash (bash)

This is the result:

Raspberry Pi image classification using Tensorflow lite

It can happen that when you run the image classification on your Raspberry Pi, you get an error about missing library. In this case, use the following command:

sudo apt-get install libatlas-base-dev
Code language: Bash (bash)

How to use object detection in Raspberry Pi with Tensorflow

In this example, we will run object detection in Raspberry Pi using Tensorflow Lite. Object detection is the capability to locate presence of an object an indicate it using a box that sourrounds the object.

As we did in the previous example, to use object detection with Raspberry Pi, it is necessary to download the necessary libraries:

  • go to /home/pi/examples/lite/examples/object_detection/raspberry_pi
  • run the following script:
./download.sh /home/pi/model

This script downloads a different Tensorflow lite model that we will use to detect objects:

  • detect.tflite (the Tensorflow lite model)
  • coco_labels.txt (the labels to assign to the objects detected)

Running Tensorflow object detection on Raspberry Pi

Before executing the object detection, it is necessary to modify the python code.

Go to /home/pi/examples/lite/examples/object_detection/raspberry_pi and open the file detect_picamera.py. Now, replace the following line:

from tflite_runtime.interpreter import Interpreter
Code language: Python (python)

with

import tflite_runtime.interpreter as tflite
Code language: Python (python)

Next, replace the line:

interpreter = Interpreter(args.model)
Code language: Python (python)

with

interpreter = tflite.Interpreter(model_path = args.model)
Code language: Python (python)

That’s all. The pyhton code is ready. Now to experiment how object detection works on Raspberry Pi use this command:

python3 detect_picamera.py --model /home/pi/model/detect.tflite --labels /home/pi/model/coco_labels.txt
Code language: Python (python)

The image below shows how it works:

Tensorflow lite object detection running on Raspberry Pi

If you want to experiment with image classification or object detection on your Raspberry Pi and you have to connect the Raspberry to your monitor using HDMI. Alternatively, you can use a VNC client. Next, click on the VNC icon and select Options->Troubleshooting, and then enable direct capture mode.

Wrapping up

In the end of this post, we have covered how to use Tensroflow lite with Raspberry Pi. We have covered how we can use Raspberry Pi to run a Tensorflow Image classification system or a object detection. Without writing complex code, but reusing the Tensorflow examples, we experimented how Machine Learning works on Raspberry Pi.