Installing opencv on Raspberry pi

Musaddiq Al Ali (Ph.D.)
3 min readAug 14, 2020

At the beginning of our deep learning series on Raspberry pi , we installed Raspbian OS on micro SD , then prepared required OS settings , after that we installed tensorflow . In this part of our series, we will talk about Installing opencv 4. Integrating opencv 4 with python needs a lot of dependencies. Here we will Install it successfully, by installing the dependencies, downloading opencv and install it in a build directory. yet it will take a long time.

We will start our installation process by expanding swap file from 100 to 2048. we do this by typing this command

sudo nano /etc/dphys-swapfile

The the this change the value in the yellow rectangle in this picture as shown. Save the file using ctrl and S, and exit to return to the terminal again.

Now we will install the following dependencies one after another. It will take around 20 minutes according to the internet speed and the condition of the single board we are using

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

Now we will download the required libraries and extracting them with the following lines.

wget -O opencv.zip https://github.com/opencv/opencv/archive/4.1.0.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.1.0.zip
unzip opencv.zip
unzip opencv_contrib.zip

After finishing the extraction process, we need to make the custom directory for installing opencv4. we do this using the following commands

cd ~/opencv-4.1.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-4.1.0/modules -D BUILD_EXAMPLES=ON ..

When this process ended successfully it will be like this

After making the directories which will take time we will stat installing by the following command

make -j4

This line will take very long time, from almost 17 hours on Raspberri 2, to 4 hours on Raspberry pi 4 B+. you might need to keep the Raspberry pi cool till the end of this process.

Here I got this kind of progress after 26 hours on Raspberry pi 2 and still not finished.

At the end you will finish the installation process with the following command

sudo make install && sudo ldconfig

It will take also a long time yet it is less than for the previous step.

It is recommend to reboot after that.

With the end of this part, we can say Congratulations.. you have a single board computer able to perform deep learning.

Next lesson we will discuss deep learning for more wide range computing and how to design deep learning network first on PC then apply it for our single board computers such as Raspberry pi

--

--

Musaddiq Al Ali (Ph.D.)
0 Followers

Specialized in structural optimization and laser physics