Introduction: Raspberry Pi Object Detection

This guide provides step-by-step instructions for how to set up TensorFlow’s Object Detection API on the Raspberry Pi. By following the steps in this guide, you will be able to use your Raspberry Pi to perform object detection on live video from a Picamera or USB webcam. Manual machine learning is not required as used in the online database for object detection. You can detect most of the objects which are commonly used worldwide.

Please refer to my above picture, we used a mouse, Apple and Scissors and detected the object perfectly.

The guide walks through the following steps:

Update the Raspberry Pi

Install TensorFlowInstall OpenCV

Compile and install Protobuf

Set up TensorFlow directory structure

Detect objects!

Step 1: Update Raspberry Pi

Start by updating your Raspberry Pi system. Open the terminal and execute the following commands, one after another:

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

Be prepared, as this may take some time depending on the speed of your internet connection and the performance of your Raspberry Pi. Once completed, your Raspberry Pi will be fully updated—ready for the next steps!

Step 2: Install TensorFlow

Proceed with installing TensorFlow. Run the command:

pip3 install tensorflow

Ensure the LibAtlas package is also installed by executing:

sudo apt-get install libatlas-base-dev

Next, enter the following command to install additional required Python libraries:

sudo pip3 install pillow lxml jupyter matplotlib cython

Don't forget to install python-tk as well:

sudo apt-get install python-tk

You are now done with TensorFlow installation—move on to the next setup!

Step 3: Install OpenCV

The Object Detection examples for TensorFlow utilize matplotlib to display images. However, we will use OpenCV due to its simplicity and reduced error likelihood. Begin by installing several dependencies:

sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install qt4-dev-tools libatlas-base-dev

Afterward, install OpenCV with the following command:

pip3 install opencv-python==3.4.6.27

Now OpenCV is installed, allowing you to utilize it efficiently!

Step 4: Install Protobuf

TensorFlow’s Object Detection API relies on Protobuf, a library for working with Google’s Protocol Buffer format. Install the protobuf-compiler by executing:

sudo apt-get install protobuf-compiler

Verify the installation by checking the version:

protoc --version

Step 5: Set Up TensorFlow Directory Structure

Create a new directory for TensorFlow models and set the Python environment path appropriately. From your home directory, type:

mkdir tensorflow1
cd tensorflow1
git clone --depth 1 https://github.com/tensorflow/models.git

Add the TensorFlow research directories to your PYTHONPATH by editing the .bashrc file:

sudo nano ~/.bashrc

Add this line at the end:

export PYTHONPATH=$PYTHONPATH:/home/pi/tensorflow1/models/research:/home/pi/tensorflow1/models/research/slim

Run protoc to compile Protocol Buffer files:

cd /home/pi/tensorflow1/models/research
protoc object_detection/protos/*.proto --python_out=.


Step 6: Detect Object

Now that everything is set up, execute object detection on live video streams. If using a PiCamera, update your Raspberry Pi configurations and run:

wget https://raw.githubusercontent.com/EdjeElectronics/TensorFlow-Object-Detection-on-the-Raspberry-Pi/master/Object_detection_picamera.py
python3 Object_detection_picamera.py

For USB cameras, simply append --usbcam to the command.

Step 7: Issues and Thank You

Please let me know if you have any questions

Email: rithij_gobinath@gmail.com

Thank you,

Rithik