Object Detection on Raspberry Pi

Introduction: Object Detection on Raspberry Pi

This instructable provides step by step instructions for how to set up object detection on Raspberry Pi. By following the steps you will be able to use your Raspberry Pi to perform object detection and recognition on live video feed from Pi camera. This can also be trained with our own neural network to identify specific objects using Pi camera, for example red color cars on heavy traffic roads. I have attached object detection python file at the end of this instructable. Raspberry Pi can be connected to PC using hotspot and VNC viewer.

Supplies

  • Raspberry Pi 3B+
  • Pi camera

Step 1: Updating the Raspberry Pi

  • Firstly, the Raspberry Pi need to be fully updated and upgraded to latest version which usually takes about 10 mins.
  • To update the Pi issue the command : sudo apt-get update
  • This command updates the Pi to the latest version of Raspbian OS.
  • I would like to mention that I used Raspbian OS and this same steps with slight modifications can be used for NOOBs
  • To upgrade the Pi issue the command: sudo apt-get upgrade
  • This command will download latest libraries and dependencies for our Raspberry Pi.

Step 2: Installing TensorFlow

  • To install tensorFlow which is a large file(100Mb) will take a while, issue the command : pip3 install tensorflow
  • TensorFlow needs the Libatlas package which is a dependency used by tensor flow.
  • Issue the command : sudo apt-get install libatlas-base-dev
  • Now we need to install other dependencies that will be used for object detection.
  • Issue the command : sudo pip3 install pillow lxml jupyter matplotlib cython
  • Then issue : sudo apt-get install python-tk
  • Now tensorflow has been successfully installed in our Raspberry Pi

Step 3: Installing OpenCV

  • Object detection is usually done using matplotlib to display images but I prefer OpenCV because it is easier to work with and probability of getting error is very less.
  • Before installing OpenCV we need to install some dependencies.
  • Issue the command: sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev
  • libjpeg is used for reading and writing JPEG images
  • libtiff is used for storing image data in TIFF format
  • libjasper is used for coding and manipulation of images
  • libpng12 is used for reading and writing images in png format
  • Then issue : sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
  • libavcodec is used for encoding and decoding video and audio data
  • libavformat is used to recoerd, convert and stream audio and videos
  • libswscale is used for image scaling
  • libv4l is used to support videos without writing seperate code for variety of devices
  • After this issue the command: sudo apt-get install libxvidcore-dev libx264-dev
  • libxvidcore is used for compression and storage of videos
  • libx264 is used for reading MPEG and AVC formats
  • and atlast issue : sudo apt-get install qt4-dev-tools libatlas-base-dev
  • qt4 is used for developing graphical user interface
  • And finally now we can install openCV by issuing the command: sudo pip3 install opencv-python
  • Now openCV is installed in our Raspberry Pi

Step 4: Installing Protobuf

  • Object detection uses Protobuf which implements Google's Protocol Buffer data format.
  • It can be installed easily by issuing the command : sudo apt-get install protobuf-compiler
  • Once the installation is complete issue the command : protoc --version to verify the installation and you will get response as libprotoc 3.8.0

Step 5: Setting Up TensorFlow Directory and Pythonpath Variable

  • As we have installed all the packages, now we need to set up the directory which you can name anything and I named it as "tensorflow1"
  • Issue the command: mkdir tensorflow1
  • followed by : cd tensorflow1
  • Now we need to install tensorflow repository which is available on github.
  • Simply issue : git clone --depth 1 https://github.com/tensorflow/models.git
  • Now we need to modify the pythonpath variable to point at some directories inside the tensorflow repository which we have just downloaded.
  • Open it by issuing : sudo nano ~/.bashrc
  • Then issue the command as follows to modify .bashrc file : export PYTHONPATH=$PYTHONPATH:/home/pi/tensorflow1/models/research:/home/pi/tensorflow1/models/research/slim
  • We need to use Protoc to compile the protocol Buffer files.
  • Execute the command from the research directrory. Issue: cd home/pi/tensorflow1/models/research
  • Followed by : protoc object_detection/protos/*.proto --python_out=.
  • Finally download the SSDlite MobileNet model and unpack it by issuing: wget http://download.tensorflow.org/models/object_dete...
  • Unpack the filw by issuing the command : tar -xzvf ssdlite_mobilenet_v2_coco_2018_05_09.tar.gz
  • Now the model is in object_detection directory and it is ready to be used.

Step 6: Detecting and Recognizing Objects

  • Make sure that Picamera is enabled in Raspberry Pi configuration menu.
  • Dowload my python file which is posted in the instructable into the object_detection directory
  • Run the script by issuing : python3 object_detection.py
  • The object detection window will open and can be used to detect and recognize object as shown in the video.

Be the First to Share

    Recommendations

    • Micro:bit Contest

      Micro:bit Contest
    • Soup & Stew Speed Challenge

      Soup & Stew Speed Challenge
    • Space Contest

      Space Contest

    3 Comments

    0
    emojica06
    emojica06

    Question 3 months ago

    I made it to the step that asks us to run this sudo nano ~/.bashrc and I copied the next line onto the end of the .bashrc file but when running the next line, I am getting a "bash: cd: home/pi/tensorflow1/models/research: No such file or directory" error. Do you have any tips on how to fix this? thank you

    0
    turkseveraltan34
    turkseveraltan34

    9 months ago

    I take error of this.
    od_graph_def = tf.GraphDef()
    AttributeError: module 'tensorflow' has no attribute 'GraphDef'


    could you help me?