# Installing OpenCV 3.3.0 on Raspbian

In this tutorial we are going no only install opencv algo we are going to optimizing so we could have a great performance on terms of IA.

**1. Expand filesystem**

Remember that first we need to download and install [Raspbian Strecth](https://www.raspberrypi.org/downloads/raspbian/) on our raspberry pi.

The first step is to run, _raspi-config_ and expand your filesystem:

```
$ sudo raspi-config
```

After finish we need to reboot the Raspberry pi. From there we are goint to eliminate package that we are not goint to use, such as Wolfram Engine and LibreOffice so we can get \~ 1GB of space.

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

**2. Install dependecies**

This next command allow us to update and upgrade any existing packages, followed by installing dependecies, I/O libraries, and optimization packages for OpenCV

```
$ 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 libcanberra-gtk*
$ sudo apt-get install libatlas-base-dev gfortran
$ sudo apt-get install python2.7-dev python3-dev
```

**3. Download OpenCV source code**

We need to get the OpenCV source code from opencv and opencv\_contrib repositories. Following by unarchiving them:

```
$ cd ~
$ git clone https://github.com/opencv/opencv.git
$ cd ~
$ git clone https://github.com/opencv/opencv_contrib.git
```

\* This tutorial we are mention the version 3.3.0 but with this command we are going to get the latest version of openCV.

**4. Create Python virtual enviroment and install NumPY**

We need to instal pip, virtualenv and virtualenvwrapper using the next commands:

```
$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python get-pip.py
$ sudo python3 get-pip.py
$ sudo pip install virtualenv virtualenvwrapper
$ sudo rm -rf ~/.cache/pip
```

Once we have virtualenv and virtualenvwrapper installed, we need to open up our \~/.profile and append the following lines to the bottom of the file.

```
nano ~/.profile
```

```
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
```

After that we need to reload \~/.profile so the change can be apply to the current bash session.

Remember that is necessary to run _source \~/.profile_ each time that you open a new terminal in your Pi so we can ensure that the system variable have been set properly.

Next we are going to create our Python3 virtual enviroment, for that:

```
$ mkvirtualenv cv -p python3
```

In this line we define that the virtual enviroment it will be call cv and we are going to use python3.

Finally, we need to install NumPy into the Python virtual enviroment

```
$ pip install numpy
```

**5. Compile and install the optimized OpenCV library for Raspberry Pi**

Remember that we are in the cv virtual enviroment, for that we use:

```
$ workon cv
```

And from there configure your build:

```
$ cd ~/opencv-3.3.0/
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.3.0/modules \
    -D ENABLE_NEON=ON \
    -D ENABLE_VFPV3=ON \
    -D BUILD_TESTS=OFF \
    -D INSTALL_PYTHON_EXAMPLES=OFF \
    -D BUILD_EXAMPLES=OFF ..
```

In this part we need to incres the swap spaces. This will enable to compile OpenCV with all four cores of the Raspberry Pi without the compile hanging due to memory exhausting. For that wee need to open \_dphys-swapfile \_and edit the \_CONF\_SWAPSIZE \_variable:

```
$ nano /etc/dphys-swapfile
```

```
# set size to absolute value, leaving empty (default) then uses computed value
#   you most likely don't want this, unless you have an special disk situation
# CONF_SWAPSIZE=100
CONF_SWAPSIZE=1024
```

Notice that you will swap from 100MB to 1024MB. If you do not perform this step it's very likely that Rasp will hang.

From there, restart the swap service:

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

After updated the swap size, kick off the optimized OpenCV compile using all four cores:

```
$ make -j4
```

Assuming OpenCV compiled without error, we can install your optimized version of OpenCV:

```
$ sudo make install
$ sudo ldconfig
```

\*\*\*\*Don't forget to go back and restore the value of _/dphys-swapfile_

1. Reset CONF\_SWAPSIZE to 100MB.

**6. Finish installing OpenCV on the Raspberry Pi**

To sym-link cv2.so file into the cv virtual enviroment, for that we need to run the following commands:

```
$ cd ~/.virtualenvs/cv/lib/python2.7/site-packages/
$ ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so
```

**7. Test OpenCV on Raspberry Pi**

Access the cv virutal enviromen, fire up a Python shell and try to import the OpenCV library:

```
$ source ~/.profile
$ workon cv
$ python
>>> import cv2
>>> cv2.__version__
'3.3.0'
```

**CONGRATULATION NOW YOU HAVE OPEN CV**
