Getting Started with Raspberry Pi + Installation Guide for Tensorflow 2.3.1 and OpenCV 4.5.1

Nitin
6 min readFeb 18, 2021

--

What is a Raspberry Pi?

Raspberry Pi is a credit card-sized mini-computer that allows you to do computing on the go. It has all the features of a desktop/laptop i.e Wifi, Bluetooth, USB ports, HDMI ports, Stereo Audio jack, Camera interface, ethernet port etc. The only drawback of the Raspberry Pi is that it doesn't support CUDA cores(no GPU) and for the price it comes at I think we can ignore that one drawback.

Raspberry Pi 4

Setting Up Raspberry Pi OS in Three Quick Steps

Things that you will need:

  1. SD Card 16 GB(or more)
  2. SD Card Reader
  3. Raspberry Pi 4 or any version
  4. Power Adapter 5V 3 Amps Output and Power Cable
  5. HDMI to Micro HDMI Cable
  6. Monitor(just for the initial setup)

Step 1:

Download the Raspberry Pi Imager from the official Raspberry Pi Website. https://www.raspberrypi.org/software/

Step 2:

Insert your SD card and run the Raspberry Pi Imager. Click on Select OS and go ahead and choose Raspberry Pi OS(32-Bit). Click on Select SD Card and choose your destination SD card and click on Write.

Step 3:

Remove the SD Card from your laptop and insert it into the Raspberry Pi Device. Boot your device and follow setup instructions. Once updates are finished installing reboot your raspberry pi from the terminal using the command: sudo reboot

After reboot, take some time to explore the OS and get a feel for it. Once comfortable we need to setup SSH and Remote Desktop(xrdp). SSH stands for Secure Shell and allows two computers to talk to each other over a network like WIFI.

Enable SSH:

  1. Click on the Raspberry Pi logo on the top left corner a dropbox will appear. Select Preferences >>> Raspberry Pi Configuration >>> Interfaces
  2. Enable SSH and Camera Interfaces and reboot your device.
  3. Open Terminal and enter the command: ifconfig and note down your inet address.

4. On your Windows/Mac system download Putty software. https://www.putty.org/ and install it using default options.

5. Open Putty and enter your inet address in the Host Name(or IP address) text field and click Open. Click “YES” for the Putty Security alert.

6. You will land on a Linux like terminal on your windows laptop. If you followed my instructions until now your username should be “pi” and password is what you provided during the initial setup.

You can now give commands to your raspberry pi using your Windows Computer. But we still lack a display to work more productively. Our next step is to setup Remote Desktop Connection between Raspberry Pi and windows.

Enable Remote Desktop Connection:

  1. Since we already have enabled SSH in your Pi we can use the terminal to communicate with the Pi.
  2. After login via Putty, we now need to install xrdp package which will allow windows to connect to Pi and give us a graphical interface to work with. In the Putty terminal enter command: sudo apt-get update
  3. sudo apt-get upgrade
  4. Install the xrdp package using the command: sudo apt-get install xrdp
  5. Reboot your Pi device using sudo reboot
  6. In your windows system open Remote Desktop Connection.
  7. In the Computer, text field enter your Pi device inet address followed by login credentials.

Now you have a graphical interface to work with, which is much better than SSH and we don't need a monitor for display.

The Pi device by default comes with the Python programming language. In fact, there are two python versions available Python 2 and Python 3. To access python 2 open terminal and type python and to access python 3 type python3.

Our next step is to install Tensorflow 2.3.1 on the Pi device. There are a lot of tutorials on how to do this and many of them might not work for you due to version issues. After a lot of searching, I came across this Github link which provides clear instructions for installing Tensorflow on Pi.

Install Tensorflow:

  1. Install pip “sudo apt-get install python-pip
  2. Install Tensorflow dependencies
  • sudo apt-get install -y libhdf5-dev libc-ares-dev libeigen3-dev gcc gfortran python-dev libgfortran5 \ libatlas3-base libatlas-base-dev libopenblas-dev libopenblas-base libblas-dev \ liblapack-dev cython libatlas-base-dev openmpi-bin libopenmpi-dev python3-dev

3. Install Keras Application

  • sudo pip3 install keras_applications==1.0.8 — no-deps

4. Install Keras Preprocessing

  • sudo pip3 install keras_preprocessing==1.1.0 — no-deps

5. Install h5py

  • sudo pip3 install h5py==2.9.0

6. Install pybind11

  • sudo pip3 install pybind11

7. Install wheel

  • pip3 install -U — user six wheel mock

8. Download Tensorflow (.sh) executable file

9. Go into the directory where the above (.sh) file is located (mostly in the downloads section)

10. Rename the tensorflow_xxxxx.sh file to tensorflow.sh and execute the sh file using the command

  • chmod +x tensorflow.sh

11.You will now see a new (.sh) file in the directory named as tensorflow-2.4.0-cp37-none-linux_armv7l_download.sh. We need to execute this file using the command

  • ./tensorflow-2.4.0-cp37-none-linux_armv7l_download.sh

12. Uninstall if any previous version of TensorFlow is present

  • sudo pip3 uninstall tensorflow

13. Remove file wrpat-1.10.11.egg-info(very important)

  • sudo rm /usr/lib/python3/dist-packages/wrapt-1.10.11.egg-info

14. Install Tensorflow 2.3.1(Note: The below command may crash on the first try, go ahead and run it a couple more times)

  • sudo -H pip3 install tensorflow-2.3.1-cp37-none-linux_armv7l.whl

15. Check for successful TensorFlow build. Open Terminal and enter the following

  • python3
  • import tensorflow as tf
  • tf.__version__

You should see version 2.3.1 printed in the terminal. You have now successfully install Tensorflow on the Pi device. If you run into any error during this installation please remain calm and try to follow the steps again.

Our final step in this tutorial is to build OpenCV using Cmake on our Pi device. Note at the time of this article you can use “pip” command to install and use OpenCV, but there will be some missing methods if you decide to go that way. The other method takes much more time approx(4 hours or less) but provides all methods. I would also like to mention that this part of the setup is available on Adrian’s PyImageSearch Website and I have borrowed it from his tutorial. I would like to thank Adrian for coming up with this setup.

Install OpenCV:(https://github.com/opencv/opencv)

  1. Install Cmake “sudo apt-get install cmake
  2. Install Nano Editor “sudo apt-get install nano
  3. Navigate to the root directory
  • cd ~

3. Download OpenCV

4. Download OpenCV contrib

5. Unzip OpenCV and OpenCV Contrib zip files

  • unzip opencv.zip
  • unzip opencv_controb.zip

6. Move OpenCV file contents into a new folder opencv

  • mv opencv-4.1.1 opencv

7. Move OpenCV Contrib file contents into a new folder opencv_contrib

  • mv opencv_contrib-4.1.1 opencv_contrib

8. Increase swap file to stop Pi device from hanging(very important)

  • sudo nano /etc/dphys-swapfile
  • Change CONF_SWAPSIZE to 2048
  • Save file and exit
  • sudo /etc/init.d/dphys-swapfile stop
  • sudo /etc/init.d/dphys-swapfile start

9. Navigate to OpenCV folder “cd ~/opencv

10. Create a new folder called build and navigate into that folder

  • mkdir build
  • cd build

11. Specify the build Instructions(make sure to copy the .. as well)

cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D ENABLE_NEON=ON \
-D ENABLE_VFPV3=ON \
-D BUILD_TESTS=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D CMAKE_SHARED_LINKER_FLAGS=-latomic \
-D BUILD_EXAMPLES=OFF ..

12. Run make command(this part will take a very long time)

  • make -j4
  • sudo make install
  • sudo ldconfig

13. After Installation change the swap file size back to100

  • sudo nano /etc/dphys-swapfile
  • Change CONF_SWAPSIZE to 100
  • Save file and exit
  • sudo /etc/init.d/dphys-swapfile stop
  • sudo /etc/init.d/dphys-swapfile start

13. Check for OpenCV Installation in the terminal

  • python3
  • import cv2
  • cv2.__version__

Following the above steps, you should now have build Opencv-4.1.1 using CMake on the Pi device.

--

--