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

Armindo Cachada
Jan 28 · 4 min read

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: https://spltech.co.uk/how-to-create-a-time-lapse-with-the-raspberry-pi-hq-camera/ or this guide or https://spltech.co.uk/creating-a-wildlife-camera-with-a-raspberry-pi-python-opencv-and-tensorflow/
  2. Install Docker
  3. Download github repository at https://github.com/armindocachada/raspberrypi-docker-tensorflow-opencv 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 get.docker.com -o get-docker.sh && sh get-docker.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 https://github.com/armindocachada/raspberrypi-docker-tensorflow-opencv

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 example3.py:

$ python example3.py

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 object_detection_camera.py Preparing to download tensorflow model ssd_mobilenet_v2_320x320_coco17_tpu-8.tar.tar.gz Is file downloaded? False object_detection_camera.py:156: 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.

Enjoy!

References:

https://github.com/armindocachada/raspberrypi-docker-tensorflow-opencv

Originally published at https://spltech.co.uk.

The Startup

Get smarter at building your thing. Join The Startup’s +786K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Armindo Cachada

Written by

Founding Director at Spltech

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +786K followers.

Armindo Cachada

Written by

Founding Director at Spltech

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +786K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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