Skip to content

Latest commit

 

History

History

raspberry_pi

TensorFlow Lite Python video classification example with Raspberry Pi.

This example uses TensorFlow Lite with Python on a Raspberry Pi to perform real-time video classification using images streamed from the camera.

Set up your hardware

Before you begin, you need to set up your Raspberry Pi with Raspberry Pi OS (preferably updated to Buster).

You also need to connect and configure the Pi Camera if you use the Pi Camera. This code also works with USB camera connect to the Raspberry Pi.

And to see the results from the camera, you need a monitor connected to the Raspberry Pi. It's okay if you're using SSH to access the Pi shell (you don't need to use a keyboard connected to the Pi)—you only need a monitor attached to the Pi to see the camera stream.

Install the TensorFlow Lite runtime

In this project, all you need from the TensorFlow Lite API is the Interpreter class. So instead of installing the large tensorflow package, we're using the much smaller tflite_runtime package.

To install this on your Raspberry Pi, follow the instructions in the Python quickstart.

You can install the TFLite runtime using this script.

sh setup.sh

Download the example files

First, clone this Git repo onto your Raspberry Pi like this:

git clone https://github.com/tensorflow/examples --depth 1

Then use our script to install a couple Python packages:

cd examples/lite/examples/video_classification/raspberry_pi

# The script install the required dependencies and download the TFLite models.
sh setup.sh

Run the example

python3 classify.py
  • You can optionally specify the model parameter to set the TensorFlow Lite model to be used:
    • The default value is movinet_a0_int8.tflite
    • This sample currently uses MoviNet-A0, but it supports all TensorFlow Lite MoviNet video classification models that are available on TensorFlow Hub. You can use the larger variant of MoviNet if you need higher accuracy.
  • You can optionally specify the maxResults parameter to limit the list of classification results:
    • Supported value: A positive integer.
    • Default value: 3.
  • Example usage: python3 classify.py \ --model movinet_a0_int8.tflite \ --maxResults 5

For more information about executing inferences with TensorFlow Lite, read TensorFlow Lite inference.