Raspberry Pi Object Detection

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

Your Raspberry Pi needs to be updated

Step 1:

Type in the Command terminal,

sudo apt-get update

And then Type

sudo apt-get dist-upgrade

This Might Take a long time depends on your Internet and Raspberry pi

That's all you need, you have finished Updating your Raspberry pi

Step 2: Install TensorFlow

Now, we are going to install Tensorflow.

Type this following command,

pip3 install TensorFlow

TensorFlow also needs the LibAtlas package, Type this following command

sudo apt-get install libatlas-base-dev

And type this following command also,

sudo pip3 install pillow lxml jupyter matplotlib cython
sudo apt-get install python-tk

Now, We have finished Installing Tensorflow.

Step 3: Install OpenCV

Now we are working to Install OpenCV library because TensorFlow’s object detection examples use matplotlib to show images, but I choose to practice OpenCV since it’s easier to work with and fewer errors. So, we need to install OpenCV. Now OpenCV is not supporting RPI, so we are going to install older Verision.

Now we are working to install a few dependences that need to be installed via apt-get

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

Finally, Now we can install OpenCV by typing,

pip3 install opencv-python==3.4.6.27


That's all, we have now installed OpenCV

Step 4: Install Protobuf

The TensorFlow object detection API uses Protobuf, a package that outfits Google’s Protocol Buffer data format. You need to compile from source, now you can install easily.

sudo apt-get install protobuf-compiler

Run protoc --version once that's done. You should get a response of libprotoc 3.6.1 or similar.

Step 5: Set Up TensorFlow Directory Structure

We have installed all the packages, we want to set up a directory for the TensorFlow. From home directory, create a directory name called “tensorflow1”,

Type the following,

mkdir tensorflow1
cd tensorflow1

Now download TensorFlow by typing,

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

We want to modify the PYTHONPATH environment variable to direct at some directories inside the TensorFlow repository. We need PYTHONPATH to be set every time. We have to adjust the .bashrc file. We have to open it by Typing

sudo nano ~/.bashrc

At the end of the file, and the last line add the command, like in the top image which is marked on the red colour box.

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

Now save and exit. We need to use Protoc to compile the Protocol Buffer
(.proto) files used by the Object Detection API. The .proto files are situated in /research/object_detection/protos, we want to execute the command from the /research directory. Type following command

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

This command changes all the
"name".proto files to "name_pb2".py files.

cd /home/pi/tensorflow1/models/research/object_detection

We need to download the SSD_Lite model from the TensorFlow
detection model zoo. For this, we want use SSDLite-MobileNet, which is the fastest model existing for the RPI.

Google is endlessly releasing models with enhanced speed and performance, so check often if there are any improved models.

Type the following command to download the SSDLite-MobileNet model.

wget http://download.tensorflow.org/models/object_detection/ssdlite_mobilenet_v2_coco_2018_05_09.tar.gz

tar -xzvf ssdlite_mobilenet_v2_coco_2018_05_09.tar.gz

Now we can able to practice the Object_Detction models!

We are nearly done!

Step 6: Detect Object

Now the whole thing is set up for execution object detection on
the Pi!

Object_detection_picamera.py detects objects in live from a Picamera or USB webcam.

If you are using a Picamera, make change the Raspberry Pi configuration a menu like in the above picture marked in red colour box.

Type the following command to download the Object_detection_picamera.py file into the object_detection directory.

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

python3 Object_detection_picamera.py

Type following command for USB camera

python3 Object_detection_picamera.py --usbcam

One's command is executed, after 1 minute a new window open which will start detecting the objects!!!

Step 7: Issues and Thank You

Please let me know if you have any questions

Email: rithikthegr8@gmail.com

Thank you,

Rithik

Raspberry Pi Contest 2020

Participated in the
Raspberry Pi Contest 2020

1 Person Made This Project!

Recommendations

  • Micro:bit Contest

    Micro:bit Contest
  • Halloween Contest

    Halloween Contest
  • Tinkercad Student Design Contest

    Tinkercad Student Design Contest

9 Comments

0
andyyeo
andyyeo

7 months ago

Thank you very much for this useful project.
Can you help me on the following:
Once the camera detects an object, how can I output a High signal on one of the Raspberry GP I/O pins? I need this HIGH detection signal to light up a LED.

Thank you very much for your kind help

0
divyaraji09
divyaraji09

1 year ago

Good and useful project for everyone
You did a great job

0
palanivel.marimuthu

Great Rithik , All the best ..I will try and get back to you if any help required.

0
rajevnb1
rajevnb1

1 year ago

Good job. Keep doing.

0
vmgobinath
vmgobinath

1 year ago

Good Job. Best of luck Rithik

0
gokulraja850
gokulraja850

1 year ago

Good ... but still work more with what is need full now so in that way you will be seen separately

0
dammasai
dammasai

1 year ago

Very creative and innovative project by Rithik. Congratulations- Dan Ammasai

0
Bantman
Bantman

1 year ago

I have already Voted! Nice instructable. I'll try this out for sure once my Camera arrives.

0
A Rakesh Kumar
A Rakesh Kumar

1 year ago

Really inspiring to see the project at a young age! Very good project!!