Image Classification with Tensorflow Lite

Jun 21, 2023
Can Durmus
Raspberry Pi
Pi Camera
Image classification on Raspberry Pi with Tensorflow Lite.
Jul 1, 2023 06:16 PM
In this post, we are going to use tflite_runtime to run one of the official examples from Tensorflow. I am using a Raspberry Pi 4 Model B 4 GB RAM model with a Pi Camera v1.3 attached. Classifications and performance are quite acceptable when the objects are close and lighted sufficiently.

1. Clone the Tensorflow examples repository

Connect your Raspberry Pi and clone the Tensorflow examples repository to your local file system.
git clone --depth 1
notion image

2. Create a new virtual environment

The installer in the repository will install various Python packages to make tflite_runtime able to run, so we should prefer to keep these packages in an isolated Python virtual environment.
mkvirtualenv tfenv
notion image

3. Install the required Python packages

Go to the example location that we are going to use and run the installer script as shown below.
cd examples/lite/examples/image_classification/raspberry_pish
notion image

4. Connect to the Raspberry Pi with VNC

The script is going to use cv2.imshow to show the classification results which means we need to connect to the Raspberry Pi. We can use the VNC viewer to do this or simply connect our Raspberry Pi to an external monitor.

5. Run the example

When you run the Python script, you should be able to see a stream from the Raspberry Pi camera, and there should be classification labels on the left top of the video.
The script runs between 6-12 FPS (Usually around 8-9 FPS) on a Raspberry Pi 4 Model B 4 GB RAM model.
notion image