Want to up your robotics game and give it the ability to detect objects? Maybe implement a security camera that can see and identify certain items? Now that the Raspberry Pi is fast enough to do machine learning, adding these features is fairly straightforward.

This guide will show you the steps to get TensorFlow 2 and TensorFlow Lite running on your Raspberry Pi 4 along with an object detection demo.

There are two main setup paths to choose from. The first option is with a PiTFT if you want to have a larger display. The second option is with the BrainCraft HAT, which has a built-in display and audio along several other components such as DotStar LEDs, a Joystick, and ports.

Raspberry Pi 4 Computer & Camera

To start with, you will need a Raspberry Pi 4. Since TensorFlow object detection is processing intensive, we recommend at least the 4GB model.

You really need a Pi 4 or better, TensorFlow vision recognition will not run on anything slower!
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...
Out of Stock

You will need a camera for the Raspberry Pi to see with.

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...
Out of Stock

All-in One BrainCraft HAT

If you want to get a HAT that has everything you need besides the camera including display, sound, and cooling, you'll want to pick up the BrainCraft HAT.

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 ASK...
In Stock

Display Output

You will also need a display so you can see what it's detecting. You can use any of our displays with the Raspberry Pi, but the 3.5" display is our biggest.

Is this not the cutest, little display for the Raspberry Pi? It features a 3.5" display with 480x320 16-bit color pixels and a resistive touch overlay so is...
Out of Stock

But our other PiTFT's will also work just fine

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 touch overlay. The plate uses the high...
Out of Stock
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 touch overlay. The plate uses the high...
Out of Stock
Is this not the cutest little display for the Raspberry Pi? It features a 2.4" display with 320x240 16-bit color pixels and a resistive touch overlay. The HAT uses the high speed...
Out of Stock
The cute PiTFT got even more adorable with this little primary display for Raspberry Pi in HAT form! It features a 2.2" display with 320x240 16-bit color pixels. The HAT uses the...
Out of Stock

Cooling It Down

Since the Raspberry Pi 4 tends to run a little hot, we recommend the Pimoroni Fan SHIM.

Looking for another way to keep your Raspberry Pi cool? Hook up Pimoroni's miniature...
In Stock

Or this mini 5V fan

Looking for another way to keep your Raspberry Pi cool? Hook up this 5V Mini Cooling...
In Stock

Or tall heatsink

Looking for the best heat sink for your Raspberry Pi? Check out this nice and tall 15x15x15mm Heat...
In Stock

In order to fit the fan/heatsink along with the display, you will need a GPIO stacking header.

Connect your own PCB to a Raspberry Pi B+ and stack on top with this normal-height female header with extra long pins.  The female header part is about 8.5mm tall, good for small...
In Stock

Camera Extension Cable

The flex cable that comes with the camera is a bit on the short side, so you may want a longer cable as well.

This cable will let you swap out the stock 150mm long flex cable from a Raspberry Pi Camera (either 'classic' or 'NoIR' type) or Raspberry Pi Display for a...
In Stock
This cable will let you swap out the stock 150mm long flex cable from a Raspberry Pi Camera (either 'classic' or 'NoIR' type) or Raspberry Pi Display for a different...
In Stock
The latest Raspberry Pi Bullseye release does not support Picamera anymore, which is needed to run the demos. Until support is added, you will need to use the Buster release.

We're going to get started using the latest Raspbian Lite, we don't need the graphical system which takes up precious CPU cycles. Start by downloading the latest Raspberry Pi OS (formerly Raspbian) Lite operating system image.

Burn the operating system image to a Micro SD Card. Directions are available on the Raspberry Pi site.

Enable Networking

For networking, you can use either a cable or WiFi. You can find more information about this in our Raspberry Pi Network Setup guide.

Enable Interfaces

After properly unmounting ("ejecting") your card for you computer, go ahead and place the microSD card in the Pi and supply power to boot it up. It may take a few minutes before it's available.

On your computer, SSH into the card using the following:

ssh [email protected]

This is a great time to change your password.


Update the Raspberry Pi

Update the Pi

sudo apt-get update
sudo apt-get upgrade


sudo apt-get install -y python3-pip


pip3 install --upgrade setuptools

Upgrade Script

We put together a script to easily make sure your Pi is correctly configured and install Blinka. Although Blinka isn't required for this to work, it's nice to have and this script also enables several other interfaces that are required for this project to work. It requires just a few commands to run. Most of it is installing the dependencies.

cd ~
sudo pip3 install --upgrade adafruit-python-shell
wget https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/raspi-blinka.py
sudo python3 raspi-blinka.py

If your system default Python is Python 2 (which is likely on a first install), it will ask to confirm that you want to proceed. Choose yes.

It may take a few minutes to run. When it finishes, it will ask you if you would like to reboot. Choose yes.

Option 1. BrainCraft HAT Setup

If you have the BrainCraft HAT, you'll first want to head over to the Adafruit BrainCraft HAT - Easy Machine Learning for Raspberry Pi guide and go through the setup process there. Specifically, you'll want to follow the Audio Setup, Fan Service Setup, and Display Module Install pages. This will guide you through all the pieces needed to prepare the Pi.

Option 2. PiTFT Setup

If you have just a bare PiTFT, you'll want to install the PiTFT Drivers. There is a new installer script now, so it can be installed with just a few commands. First, start by installing a few dependencies and downloading the repo:

cd ~
sudo pip3 install --upgrade adafruit-python-shell click==7.0
sudo apt-get install -y git
git clone https://github.com/adafruit/Raspberry-Pi-Installer-Scripts.git
cd Raspberry-Pi-Installer-Scripts

Next, choose the install command based on your display. For the 2.4", 2.8", or 3.2" Resistive touchscreens, use the following command:

sudo python3 adafruit-pitft.py --display=28r --rotation=90 --install-type=fbcp

For the 3.5" Resistive touchscreen, use the following command:

sudo python3 adafruit-pitft.py --display=35r --rotation=90 --install-type=fbcp

For the 2.8" Capacitive touchscreen, use the following command:

sudo python3 adafruit-pitft.py --display=28c --rotation=90 --install-type=fbcp

When you get asked to reboot, reboot!

After it reboots, you should now see text on the display.

Now to do an initial test with the camera to make sure the hardware is working before we dive into TensorFlow. This should display what the camera sees on the display.

raspistill -t 0

Here's what it looks like on the BrainCraft HAT:

Here's what it looks like on the PiTFT:

If you don't see anything on your camera or get an error message, be sure you have the camera interface enabled in raspi-config. If it is still having issues, you will need to get that working before continuing. Unfortunately troubleshooting a camera setup can be complex and is beyond the scope of this guide.

There's a LOT of software to install, this can take up to an hour

Install requirements

There are a few packages that TensorFlow requires that need to be installed:

sudo apt-get install -y libatlas-base-dev libhdf5-dev libc-ares-dev libeigen3-dev build-essential libsdl-ttf2.0-0 python-pygame festival python3-h5py

Virtual Environment

There are a few dependency requirements to install TensorFlow inside the Python Environment:

pip3 install virtualenv Pillow numpy pygame

Install rpi-vision

Now to install an Adafruit fork of a program originally written by Leigh Johnson that uses the MobileNet V2 model to detect objects. This part will take a few minutes to complete.

cd ~
git clone --depth 1 https://github.com/adafruit/rpi-vision.git
cd rpi-vision
python3 -m virtualenv -p $(which python3) .venv
source .venv/bin/activate

Install TensorFlow 2.x

You should now be inside a virtual environment. You can tell by the (.venv) on the left side of the command prompt. While in the virtual environment, you may download and install Tensorflow 2.3.1

wget https://raw.githubusercontent.com/PINTO0309/Tensorflow-bin/main/previous_versions/download_tensorflow-2.3.1-cp37-none-linux_armv7l.sh

chmod a+x ./download_tensorflow-2.3.1-cp37-none-linux_armv7l.sh


pip3 install --upgrade setuptools

pip3 install ./tensorflow-2.3.1-cp37-none-linux_armv7l.whl

pip3 install -e .

After this, go ahead and reboot the Pi.

sudo reboot

Running the Graphic Labeling Demo

Finally you are ready to run the detection software. First you want to run as root so that Python can access the Frame Buffer of the display.

sudo bash

Then activate the virtual environment again:

cd rpi-vision && . .venv/bin/activate

To run a program that will display the object it sees on screen type in the following:

python3 tests/pitft_labeled_output.py --tflite

You should see a bunch of text scrolling in your SSH window.

Now start holding up various items in front of the camera and it should display what it thinks it sees, which isn't actually what the item may be. Some items that it's pretty good about identifying are coffee mugs and animals.

Speech Output

As an added bonus, you can hook up a pair of headphones or a speaker to the Raspberry Pi and it will actually tell you what it is detecting. Make sure you don't have any HDMI cords plugged in though or the audio will go through the monitor.

This guide was first published on Sep 04, 2019. It was last updated on Sep 04, 2019.