The Startup
Published in

The Startup

How to Run Object Detection With Tensorflow 2 on the Raspberry PI Using Docker

In this article, I am going to show you how you can try object detection on the Raspberry PI using a PI Camera, the easy way, with docker!

These are the main steps you need to complete:

  1. Install and configure the PI Camera if you haven’t yet. See this separate guide I created to install it: or this guide or
  2. Install Docker
  3. Download github repository at and follow setup
  4. Setup X11 forwarding to camera
  5. Download model from model Zoo
  6. Run example

Installing Docker

Installing Docker in the Raspberry PI is very easy. But before you do that, it is always best that you get all the latest updates on your Raspberry PI. So let’s do that first:

Update all your packages in the PI:

$ sudo apt update ... $ sudo apt upgrade

Get a coffee, because this is going to take a while. When it is all finished, do a reboot:

$ sudo reboot

And now we are ready to install docker:

$ curl -fsSL -o && sh

Add your user to the Docker Group

sudo usermod -aG docker $(whoami)

And let’s reboot again!

$ sudo reboot

After reboot, double check that docker is really installed:

$ docker version Client: Docker Engine - Community Version: 19.03.13 API version: 1.40 Go version: go1.13.15 Git commit: 4484c46 Built: Wed Sep 16 17:07:02 2020 OS/Arch: linux/arm Experimental: false Server: Docker Engine - Community Engine: Version: 19.03.13 API version: 1.40 (minimum version 1.12) ...

And that’s it. You have docker installed.

Raspberry PI Camera Setup

In order to try Tensorflow object detection in real-time on the Raspberry PI we need to have a camera installed on the PI. I will assume that you have already done that. If you haven’t, don’t worry I have created a nice guide on how you can install your Raspberry PI Camera:

Raspberry PI Camera Tutorial — How to install a Raspberry PI Camera

Downloading Github Repository

Let’s clone the raspberrypi-docker-tensorflow-opencv repository from inside your home directory:

git clone

The project we have just downloaded contains all the files needed to run Object Detection with Tensorflow 2 using the Raspberry PI Camera.

Starting the camera docker container

To start a docker container containing all the necessary dependencies to run object detection with Tensorflow 2 and also with access to the Raspberry PI camera execute the following:

$ cd raspberrypi-docker-tensorflow-opencv $ docker-compose up -d .... Creating camera ... done

It might take a while as it will download the docker container from docker hub. Get a cup of coffee and it will finish downloading before you finish your coffee.

Enabling access to X11 Server

The camera docker container needs to connect to the X11 server running on the Raspberry PI. We need to allow it to do so. Open a terminal window and type:

$ xhost +:local

Testing the Raspberry PI Camera

To test that the docker container is able to open a window with a view of the camera let’s try python

$ python

If your camera is working then you should be able to see a window similar to the one below:

Testing Object Detection

Now that we know that the camera is working we can test object detection.

To start object detection on the raspberry pi open a terminal again.

$ docker exec -it camera bash $[email protected]:/tensorflow/models/research# cd /app && python3 Preparing to download tensorflow model ssd_mobilenet_v2_320x320_coco17_tpu-8.tar.tar.gz Is file downloaded? False downloadModel(MODEL_URL) Elapsed time: 172.93311309814453s

If all goes well you should see a window popup with a view of the camera and object detection should be working.



Originally published at



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store