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
Step 1: Update Raspberry Pi
Your Raspberry Pi needs to be updated
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==22.214.171.124
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,
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.
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
protoc object_detection/protos/*.proto --python_out=.
This command changes all the
"name".proto files to "name_pb2".py files.
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.
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
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.
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
Participated in the
Raspberry Pi Contest 2020