Introduction: Object Detection Using Raspberry Pi 4

Following instructable provides step-by-step instruction on the setup of Object detection using Raspberry Pi 4 Model B. At last, you will be able to develop an object detector by recognizing a live video via the Pi -camera. This a basic project by which we can only detect certain items mentioned in the upcoming codes. We can make our project in an advance by implementing neural networking, and machine learning (For example: *Detection of type of vehicles in the traffic signals, *Can also be used in the military base to detect the tank and other offensive materials). I have attached the python code, and the related files working with the code.

Supplies

  • Raspberry Pi 4 Model B
  • Pi camera

Step 1: Updating the Raspberry Pi 4

  • Initially, you need to keep the Raspberry Pi OS to the upgraded version, this will take a time of 15 to 20 mins
  • Open the command window of the Raspberry pi using VNC viewer or by using the putty.exe
  • To get the latest update of the OS you need to issue the command: sudo apt-get update
  • Now you will get the recent updates on the OS and libraries
  • To be upgraded with the latest OS and the libraries available issue the command: sudo apt-get upgrade
  • Now your pi will be upgraded to the latest version.

Step 2: Mentioning the Swap File Size

  • Swap file used when all the Raspbian RAM get exhausted it can start using the swap file for memory
  • The Raspbian distribution comes with a 100MB swapfile.
  • This is a bit on the small side
  • A general rule of thumb is swapfile size should be about twice as much as the available RAM on the machine
  • You must mention the Raspbian swap file size by issuing the command: sudo nano /etc/dphys-swapfile CONF_SWAPSIZE=2048

Step 3: Installing TensorFlow

  • But for future projects, the installation of TensorFlow is a must
  • To install TensorFlow you need to issue the command: pip3 install TensorFlow
  • To access TensorFlow, you need to download some libraries like ATLAS(Automatically Tuned Linear Algebra Software) by issuing the command:sudo apt-get install libatlas-base-dev
  • Next, you should install the remaining libraries which comes under the TensorFlow by issuing the command:sudo pip3 install pillow lxml jupyter matplotlib cython
  • Finally to complete the installation issue the command:sudo apt-get install python-tk
  • For our Python code, we don't need to install TensorFlow


    Step 4: Installing OpenCV

    • In this project, you are going to work on the OpenCV environment
    • To install the complete package of OpenCV you need to issue the command: sudo apt-get install build-essential cmake pkg-config
    • To work under OpenCV we need to install the required libraries
    • To install the libraries working on the image formats you need to issue the command: sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev
    • jpeg, tiff, jasper, png refers to the image format in which, you are going to store and manipulate images
    • Now issue the command:sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
    • libavcodec is to encode and decode the videos
    • libavformat is used to convert audio and video formats
    • libswscale is used in the scaling of images
    • lib4l is used to read any format of videos without any separate code
    • Now issue the command: sudo apt-get install libxvidcore-dev libx264-dev
    • libxvidcore used to open MPEG videos
    • libx264- is a free software library and application for encoding video streams into the H.264/MPEG-4 AVC compression format
    • Install the dependent package by issuing the command:sudo apt-get install libgtk2.0-dev libgtk-3-dev
    • Issue the command to have a graphical interface with the user : sudo apt-get install libatlas-base-dev gfortran
    • Then you have to download the full package of OpenCV by issuing the command:wget -Oopencv.zip https://github.com/opencv/opencv/archive/4.4.0.zi...
    • You have to download the contribution files of OpenCV by issuing the command:wget -O opencv_contrib.zip https://github.com/opencv/opencv/archive/4.4.0.zi...
    • You have to unzip the OpenCV ziped file by issuing the command:unzip opencv.zip
    • You must also unzip the contribution file by issuing the command:unzip opencv_contrib.zip
    • Finally issue the command:sudo pip3 install numpy

    Step 5: Creating a Directory in OpenCV

    • Open the folder OpenCV by issuing the command:cd ~/opencv-4.4.0/
    • Create a directory in the OpenCV by issuing the command:mkdir build
    • Issue the command:cd build
    • Issue the command:cmake-D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-4.4.0/modules \ -D BUILD_EXAMPLES=ON .. 18. make -j4
    • Issue the command:sudo make install &&;sudo ldconfig
    • Finally issue the command:sudo reboot

    Step 6: Additional TensorFlow Directories

    • You don't need to install these directories but for future purposes its good enough to do the following steps
    • Issue the command: mkdir tensorflow1
    • Followed by : cd tensorflow1
    • Now you need to install Tensorflow repository which is available on github
    • Simply issue : git clone --depth 1 https://github.com/tensorflow/models.git
    • Now you need to modify the python path 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 directory.
    • Issue: cd home/pi/tensorflow1/models/researchFollowed by : protoc object_detection/protos/*.proto --python_out=.
    • Finally, download the SSDlite MobileNet model and unpack it by issuing: wget https://github.com/tensorflow/models.git
    • Now you need to get the inbuilt names 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 7: Raspberry Code Setup

    Step 8: Open the Python File

    • Using VNC viewer connect your desktop with Raspberry Pi
    • Open the python IDE which you use in the Raspberry Pi
    • Open the python file

    Step 9: File Path

    • Copy the file location where you have saved the file and paste it in configpath(ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt) and widthpath(frozen_inference_graph.pb)
    • You can see this process in the above image
    • Finally, run the program.