Setup OpenCV on Raspberry Pi 3 B

Raspberry Pi is a hobbyist’s dream.  It is a small compact computer base for under $40.  Yes!  Under $40.  Below is a professional product image:

For the clover project, I setup four Raspberries.  I am a DIY type of person and I love doing a lot with a little (money-wise).  Thus, my setup is pictured below.  Please note the wonderful, high-quality case work!

These are the Raspberry PI 3 B, which has built-in wireless connectivity at 100MB, super nice for under $40. IKR!

If you want to set up virtual Python and OpenCV on Raspberry Jessie, the best instructions I have seen are here.  Adrian Rosebrock has some excellent Python and OpenCV resources for the Raspberry.  His site is worth marking, especially if you are a beginner.  If you do use Adrian’s instructions (and they are nice, he sets up a nice virtual development environment).  If you wish to run Python Idle in the virtual environment a handy command prompt to open it is:

python -m idlelib

This command must be run in the virtual environment.  This opens Python Idle with all the configurations of the virtual development environment.  If you wish to use OpenCV with Python on Raspberry, I suggest Adrian’s post is your best direction.  My purpose was to use the Raspberry for training and some testing.  My walk through below may not meet all your needs if you’re a heavy Python user.

My approach is simple.  Ubuntu and Jessie (the Raspberry flavored Linux) have much in common (Debian).  Therefore I used the same setup steps I used in setting up OpenCV on Ubuntu.  I will walk you through them again here with added emphasis and a few changes for the Raspberry.

If you plan to use the Raspberry for any cascade training, you will discover the 1GIG limit is, well… limiting.  Especially for cascade training.  I used a work around by using zRam.  The GitHub link for it is here.  I have added the information below.  It doesn’t exactly add RAM to the Raspberry, but it does give you extra swap space via a processor.  The end result is 2 GIG of available RAM for a program instead of 1GIG.  This makes all the difference in the world for cascade training.  You will not be able to do HPC type of training, but you will be able to use a $40 piece of hardware to do training!  Who cares if it takes two to five times as long, this is development, research stage, and hobby, not Wall Street.

Script to enable zram for raspberry pi

Download the script and copy to /usr/bin/ folder

sudo wget -O /usr/bin/zram.sh https://raw.githubusercontent.com/novaspirit/rpi_zram/master/zram.sh

make file executable

sudo chmod +x /usr/bin/zram.sh

edit /etc/rc.local file to run script on boot

sudo nano /etc/rc.local

add line before exit 0

/usr/bin/zram.sh &

When you reboot your Raspberry, you can use top or htop to see the newly available memory in swap.  Very nice for times when the 1GIG limitation does not allow a program to run.

Setting Up OpenCV on Raspberry PI

Login to your raspberry.  Open a terminal and run

sudo apt-get update

sudo apt-get upgrade

This will update your server to the latest state.  On Raspberry, these steps can take a bit longer.  The total process took me about 2 hours with the largest amount of time waiting for OpenCV to compile.  If you are using SSH or VNC, you can work through the process while doing other things (multi-task)… this concept makes your current or future employers very happy!

Next, create a directory called MyWorkspace.  I created mine in the /home/pi directory.

sudo mkdir MyWorkspace

Change directory into the MyWorkspace directory and run the following commands. If you do not have git installed, you can install by typing in “sudo apt-get install git” and pressing return.  Don’t forget use sudo, Raspberry is a stickler about rights and permissions.  You will notice in the screen capture below I left the command out in the walk through.

sudo git clone https://github.com/Itseez/opencv.git

My current screen shot:

 

Next, install the necessary libraries and compilers by running the following commands.

sudo apt-get install build-essential

sudo apt-get install cmake libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

sudo apt-get install python-dev python-numpy libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

sudo apt-get install libopencv-dev

That’s it… you have successfully installed OpenCV on your Raspberry PI!  If you want to verify the installation, type in opencv_createsamples at the command prompt.  You should see a list of options when using opencv_createsamples as shown below.

BUT, OpenCV has not been installed to use with Python yet.  That requires a little more effort and a decent amount of processing time.

In the MyWorkspace/opencv directory, make a directory named build and cd into it.   Then run the following the command:

cmake -D CMAKE_BUILD_TYPE=RELEASE \

-D CMAKE_INSTALL_PREFIX=/usr/local ..

This will take a couple of minutes to run.  Note, there are other parts one can install for opencv, I only needed the base for what I was doing, so I used the minimum.  Again, if you need a full installation, I suggest Adrian’s instructions I mentioned before.

Next, we build.  This will take a long time, probably over an hour.  Adrian’s post does give us information on how to use all 4 processors for the build.  This option has the possibility (though never happen with me) to fail.  If it does, simply don’t use the 4 compiler option of (-j4 ).  The make command:

sudo make -j4

After a long compile, you will need to run

sudo make install

to place the files in their proper location.  Then run

sudo ldconfig

Lastly, you will need to change to the following directory:

cd /usr/local/lib/python3.4/dist-packages/

and change the name of the cv2.cpython-34m.so file to cv2.so, this is done with the following command.

sudo mv cv2.cpython-34m.so cv2.so

At this point, you can import cv2 into Python as shown in the screen capture below:

Lastly, let me re-emphasize, this was to meet my needs for this project.  If you need more bells and whistles with OpenCV for Python on Raspberry, I strongly suggest you follow Adrian’s post.