Setting up Raspberry Pi for Computer Vision (installing OpenCV)
By: Tanuj Vishnoi
Tags: Opencv, Raspberry Pi, IoT, Computer Science, Computer Vision

Photo by Harrison Broadbent on Unsplash

Setting up Rasbian Noobs

Noobs is an easy to install version of Raspbian. Raspbian is the Foundation’s official supported operating system. Raspbian comes pre-installed with plenty of software for education, programming and general use. It has Python, Scratch, Sonic Pi, Java and more.

Note: Rasbian is a Linux based OS.

Note: The process may seem lengthy and boring but its a one-time thing. You can also just copy the commands in quotes.

Download the zip file from https://www.raspberrypi.org/downloads/noobs/ Make sure you unzip the folder and copy all the contents to an SD Card (recommended >4GB). Enable SSH by placing a file named “ssh” (without any extension) onto the boot partition (inside the boot folder) of the SD card. Power up the raspberry pi using your phone charger cable(not C-type) and connect it through ethernet to your laptop.

For the first time, I recommend going up with an external monitor, keyboard, and mouse for initial setup. Note the IP of pi through ifconfig and then proceed through wifi/ethernet essentials. However, if you don't have initial external peripherals go through this video: How to Setup a Raspberry Pi Without a Monitor or Keyboard

Getting IP of Raspberry Pi Connected on Ethernet (Ubuntu)

Power up the Raspberry Pi and connect the ethernet to pi and laptop. On your laptop. Go the edit connection setting. Navigate to ipv4 option. Select method: “shared to other computer”. Connect to the wired connection. Then open command prompt and type command

nano /var/lib/misc/dnsmasq.leases”.

You will get raspberry pi Ip from that.

5. then open command prompt and type: ssh pi@<ip of raspberry pi>

Setting up OpenCV 3 on Pi

Can’t SSH? If you see your Pi on your network, but can’t ssh to it, you may need to enable SSH. This can easily be done via the Raspberry Pi desktop preferences menu (you’ll need an HDMI cable and a keyboard/mouse) or running sudo service ssh start from the command line of your Pi.

The first step will be of expanding the file system to include all available space on your micro-SD card:

Step 1: sudo raspi-config Step 2: Followed by selecting “Expand filesystem”: Step 3: Once prompted, you should select the first option, “A1. Expand File System”, hit Enter and then Finish Step 4: Go to the terminal and type “sudo reboot”

(Optional) The second is to delete all unnecessary files to clear up space

sudo apt-get purge wolfram-engine sudo apt-get purge libreoffice* sudo apt-get clean sudo apt-get autoremove

Now, in step 3 we shall install all the dependencies required

sudo apt-get update && sudo apt-get upgrade sudo apt-get install build-essential cmake pkg-config 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 libgtk2.0-dev libgtk-3-dev sudo apt-get install libatlas-base-dev gfortran

Installing OpenCV for python 3 and python 2

sudo apt-get install python2.7-dev python3-dev

Now for step four, we shall be downloading the opencv source code and then building it for the pi.

cd ~ && wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.3.0.zip && unzip opencv.zip

(optional) Install the opencv-contrib library

wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.3.0.zip && unzip opencv_contrib.zip

Now, in step 5, we will install pip for both the python

wget https://bootstrap.pypa.io/get-pip.py && sudo python get-pip.py && sudo python3 get-pip.py

Mostly, you came through this blog to setup your deep learning modules on single board computers like Raspberry Pi. So we will make opencv installtion public, to be accessible across all virtual environments. If you want you build your own virtual environment using envwrapper or likewise, activate and proceed.

Install numpy

pip install numpy

Now, we will compile and install opencv. This process will take time, depending on the pi model. It took around 1.5 hours in my B+ plus model. So grab a coffee!!

cd ~/opencv-3.3.0/ && mkdir build && cd build cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.3.0/modules \ -D BUILD_EXAMPLES=ON ..

If you are using python 3 make sure your python interpreter and libraries are set in python3 or python 3.5 folders (or python2.7 folder for python 2)

Before you start the compile process, you should increase your swap space size. This enables OpenCV to compile with all four cores of the Raspberry PI without the compile hanging due to memory problems. Type in terminal

sudo nano /etc/dphys-swapfile

then edit the CONF_SWAPSIZE as

CONF_SWAPSIZE=1024

Now, we will restart the swap space.

sudo /etc/init.d/dphys-swapfile stop sudo /etc/init.d/dphys-swapfile start

Now, we are jumping to conclusion phases of installing opencv. We will compile opencv now.

make -j4

Now, all you need to do is install OpenCV 3 on your Raspberry Pi 3

sudo make install sudo ldconfig

Bug Fix: I honestly don’t know why, perhaps it’s a bug in the CMake script, but when compiling OpenCV 3 bindings for Python 3+, the output .so file is named cv2.cpython-35m-arm-linux-gnueabihf.so (or some variant of) rather than simply cv2.so (like in the Python 2.7 bindings). Again, I’m not sure exactly why this happens, but it’s an easy fix. All we need to do is rename the file:

For python3

cd /usr/local/lib/python3.5/site-packages/ sudo mv cv2.cpython-35m-arm-linux-gnueabihf.so cv2.so cd ~/.virtualenvs/cv/lib/python3.5/site-packages/ ln -s /usr/local/lib/python3.5/site-packages/cv2.so cv2.so

TEST the installation by opening python bash and

import cv2

Now, we will just free up the space by deleting installation files and reverting the pi back to its original swap space.

rm -rf opencv-3.3.0 opencv_contrib-3.3.0 sudo nano /etc/dphys-swapfile

then edit the CONF_SWAPSIZE as

CONF_SWAPSIZE=1024

Now, we will restart the swap space.

sudo /etc/init.d/dphys-swapfile stop sudo /etc/init.d/dphys-swapfile start

Congratulations, you are DONE now. Enjoy Computer Vision!!