Get Started With Image Recognition Using TensorFlow and Raspberry Pi

Ian Buckley Updated 25-11-2019

TensorFlow is Google’s Neural Network library. Given that machine learning is the hottest thing around currently, it is no surprise that Google are among the leaders in this new technology.


In this article, you will learn how to install TensorFlow on the Raspberry Pi, and run simple image classification on a pre-trained neural network.

Getting Started

To get started with image recognition, you will need a Raspberry Pi (any model will work) and an SD card with the Raspbian Stretch (9.0+) operating system (if you’re new to the Raspberry Pi, use our installation guide).

Boot up the Pi and open a terminal window. Make sure your Pi is up to date, and check your Python version.

sudo apt-get update
python --version
python3 --version

You can use both Python 2.7 or Python 3.4+ for this tutorial. This example is for Python 3. For Python 2.7, replace Python3 with Python, and pip3 with pip throughout this tutorial.

Pip is a package manager for Python, usually installed as standard on Linux distributions.


If you find you don’t have it, follow the install for Linux instructions How to Install Python PIP on Windows, Mac, and Linux Many Python developers rely on a tool called PIP for Python to streamline development. Here's how to install Python PIP. Read More in this article to install it.

Installing TensorFlow

Installing TensorFlow used to be quite a frustrating process, but a recent update makes it incredibly simple. While you can follow this tutorial without any prior knowledge, it might be worth understanding the basics of machine learning before trying it out.

Before installing TensorFlow, install the Atlas library.

sudo apt install libatlas-base-dev

Once that is finished install TensorFlow via pip3

pip3 install --user tensorflow

This will install TensorFlow for the logged in user. If you prefer to use a virtual environment Learn How to Use the Python Virtual Environment Whether you are an experienced Python developer, or you are just getting started, learning how to setup a virtual environment is essential for any Python project. Read More , modify your code here to reflect this.

Testing TensorFlow

Once it has installed, you can test whether it is working with the TensorFlow equivalent of a Hello, world!

From the command line create a new Python script using nano or vim (If you aren’t sure which one to use, they both have advantages) and name it something easy to remember.

sudo nano

Enter this code, provided by Google for testing TensorFlow:

import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()

If you are using nano, exit by pressing Ctrl + X and save your file by typing Y when prompted.

Run the code from the terminal:


You should see “Hello, TensorFlow” printed.

If you are running Python 3.5, you will get several runtime warnings. The official TensorFlow tutorials acknowledge that this happens, and recommend you to ignore it.


TensorFlow and Python3.5 - Ignorable error

It works! Now to do something interesting with TensorFlow.

Installing the Image Classifier

In the terminal, create a directory for the project in your home directory and navigate into it.

mkdir tf1
cd tf1

TensorFlow has a git repository with example models to try out. Clone the repository into the new directory:

git clone

You want to use the image classification example, which can be found at models/tutorials/image/imagenet. Navigate to that folder now:

cd models/tutorials/image/imagenet

The standard image classifying script runs with a provided image of a panda:

Tiny TensorFlow Panda

To run the standard image classifier with the panda image provided enter:


This feeds an image of a panda to the neural network, which returns guesses as to what the image is with a value for its level of certainty.

TensorFlow Panda Classifying Output

As the output image shows, the neural net guessed correctly, with an almost 90 percent certainty. It also thought the image might contain an custard apple, but it was not very confident with that answer.

Using a Custom Image

The panda image proves that TensorFlow works, but that is perhaps unsurprising given it is the example the project provides. For a better test, you can give your own image to the neural net for classification.

In this instance, you’ll be seeing if the TensorFlow neural net can identify George.

George the Dinosaur

Meet George. George is a dinosaur. To feed this image (available in cropped form here) into the neural net, add arguments when running the script.

python3 --image_file=/home/pi/george.jpg

The image_file= following the script name allows the addition of any image by path. Lets see how this neural net did.

TensorFlow Dinosaur Classification Output

Not bad! While George is not a triceratops, the neural net classified the image as a dinosaur with a high degree of certainty when compared to the other options.

TensorFlow and Raspberry Pi, Ready to Go

This basic implementation of TensorFlow already has potential. This object recognition is happening on the Pi, and needs no internet connection to function. This means that with the addition of a Raspberry Pi camera module and a Raspberry Pi-suitable battery unit, the whole project could go portable.

Most tutorials only scratch the surface of a subject, but it has never been truer than in this case. Machine learning is an incredibly dense subject.

One way to take your knowledge further would be by taking a dedicated course These Machine Learning Courses Will Prepare a Career Path for You These excellent online machine learning courses will help you understand the skills needed to start a career in machine learning and artificial intelligence. Read More . In the meantime, get hands on with machine learning and the Raspberry Pi with these TensorFlow projects you can try yourself.

Related topics: Google TensorFlow, Image Recognition, Raspberry Pi.

Affiliate Disclosure: By buying the products we recommend, you help keep the site alive. Read more.

Whatsapp Pinterest

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. vinothkumar
    August 20, 2019 at 6:17 am

    i am using oython 3.7.4. i am not able to install tensor flow. i need help on this

  2. Xaver Stenliz
    April 5, 2019 at 10:11 pm

    hello this make no sense for me, cd models/tutorials/image/imagenet orcd tf1/models/tutorials/image/imagenet, why to paths?? in the picture your in the tf1 folder. thx

  3. Reijo Korhonen
    April 4, 2019 at 6:52 pm

    Seems that Raspberry pi 2 is not enough even with Raspbian Stretch (9.0+) . When trying to install TensorFlow with pip3, I get

    File "/usr/lib/python3.5/", line 59, in b64encode
    encoded = binascii.b2a_base64(s)[:-1]