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.

In this guide we'll be showing you the steps you need to follow to get TensorFlow 2.0 and TensorFlow Lite running on your Raspberry Pi 4 and along with an object detection demo.

Raspberry Pi 4 Computer & Camera

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

Raspberry Pi 4 Model B - 4G RAM

PRODUCT ID: 4296
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.

Raspberry Pi Camera Board v2 - 8 Megapixels

PRODUCT ID: 3099
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...
$29.95
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.

PiTFT - Assembled 480x320 3.5" TFT+Touchscreen for Raspberry Pi

PRODUCT ID: 2097
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...
$44.95
IN STOCK

But our other PiTFT's will also work just fine

PiTFT Plus 320x240 3.2" TFT + Resistive Touchscreen

PRODUCT ID: 2616
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...
$39.95
IN STOCK

PiTFT Plus Assembled 320x240 2.8" TFT + Resistive Touchscreen

PRODUCT ID: 2298
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...
$34.95
IN STOCK

Adafruit PiTFT 2.4" HAT Mini Kit - 320x240 TFT Touchscreen

PRODUCT ID: 2455
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

Adafruit PiTFT 2.2" HAT Mini Kit - 320x240 2.2" TFT - No Touch

PRODUCT ID: 2315
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...
$24.95
IN STOCK

Cooling It Down

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

Pimoroni Fan SHIM for Raspberry Pi

PRODUCT ID: 4316
Looking for another way to keep your Raspberry Pi cool? Hook up Pimoroni's miniature...
$11.95
IN STOCK

Or this mini 5V fan

Miniature 5V Cooling Fan for Raspberry Pi (and Other Computers)

PRODUCT ID: 3368
Looking for another way to keep your Raspberry Pi cool? Hook up this 5V Mini Cooling...
OUT OF STOCK

Or tall heatsink

Aluminum Heat Sink for Raspberry Pi 3 or 4 - 15 x 15 x 15mm

PRODUCT ID: 3082
Looking for the best heat sink for your Raspberry Pi? Check out this nice and tall 15x15x15mm Heat...
OUT OF STOCK

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

GPIO Stacking Header for Pi A+/B+/Pi 2/Pi 3

PRODUCT ID: 2223
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...
OUT OF 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.

Flex Cable for Raspberry Pi Camera or Display - 24" / 610mm

PRODUCT ID: 1731
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...
$2.95
IN STOCK

Flex Cable for Raspberry Pi Camera or Display - 18" / 457mm

PRODUCT ID: 1730
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...
$2.50
IN STOCK

Flex Cable for Raspberry Pi Camera or Display - 300mm / 12"

PRODUCT ID: 1648
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...
$1.95
IN STOCK

Initial Setup

We're going to get started using the latest Raspbian Lite, we don't need the graphical system and it takes up precious cycles. Start by downloading the latest 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.

passwd

Open a terminal and run

sudo raspi-config

Permanently Enable SSH through Interfacing Options → SSH
Enable the camera through Interfacing Options → Camera

After that, it will ask you to reboot. Go ahead and do that now.

Update the Raspberry Pi

Update the Pi

sudo apt update
sudo apt-get update
sudo apt-get upgrade

and

sudo apt-get install -y python3-pip

and

pip3 install --upgrade setuptools

PiTFT Drivers

Next we want to install the PiTFT Drivers. You can find directions in the Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi guide in the Installer Script section.

When prompted, choose 90 degrees for rotation, No when asked if you would like the console to appear on the display and Yes when asked about HDMI Mirroring. This will cause it to install the FBCP drivers, which we will need.

Go ahead and Reboot when asked.

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

Install Requirement Packages

There are a few more packages 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

Initial Camera Test

Now to do an initial test with the camera. This should display what the camera sees on the display.

raspistill -t 0

TensorFlow Lite 2.0 Setup

Install requirements

For TensorFlow, there are a few dependency requirements to install in the Python Environment:

pip3 install virtualenv Pillow numpy pygame

Install rpi-vision

Now to install our 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.0

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.0 RC0

wget https://github.com/PINTO0309/Tensorflow-bin/raw/master/tensorflow-2.0.0rc0-cp37-cp37m-linux_armv7l.whl

pip3 install --upgrade setuptools

pip3 install tensorflow-2.0.0rc0-cp37-cp37m-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.

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.