Install OpenCV 4 on the Raspberry Pi

· 7 min read
Install OpenCV 4 on the Raspberry Pi

OpenCV (Open Source Computer Vision Library) is a library for Computer Vision and Machine Learning with an open source license. OpenCV was built to provide infrastructure commonly used in Computer Vision. According to the source website, OpenCV was built with 2500 more comprehensive algorithms for Computer Vision and Machine Learning.

Then what is the use of OpenCV for? Maybe you have uploaded photos on Facebook social media. Then suddenly, intelligently, Facebook recognizes every face of your friend to be tagged. Or, if you are traveling in Jakarta, and you are committing a traffic violation. Suddenly you will get a ticket automatically. This is the work of Computer Vision because the computer will see and detect objects that are adjusted to the program.

In this tutorial, we will try to install OpenCV on a Raspberry Pi device. The next one can be developed into smart camera applications such as Facial Recognition, Object Recognition, or just for smart security cameras.

Preparation

Prepare 1 Unit of Raspberry Pi that has fresh Stretch Raspbian installed. For how to install Raspberry Pi, please see the article Install Raspberry Pi in Headless or How to Install Raspberry Pi with NOOBS. Also prepare an adequate internet connection, because we will download a relatively large software.

If you do not use the display, you must activate the SSH server and VNC Server on your Raspberry Pi. The method can be found in the Headless Installation article.

Install OpenCV 4

Please follow the installation steps below. When this article was written, we used the Raspberry Pi 3 Model B+ (Buy Here) model with 1GB RAM and 4 Core Processors. The installation time is approximately 2 to 5 hours, depending on the internet speed and your SD Card type. Follow the steps carefully so that this OpenCV installation is successful.

Expand Filesystem

When your Raspberry Pi is installed, the file system used is the file system from the original image. We will use all quota that is on our Micro SD. Type the following commandment.

sudo raspi-config

After the Raspberry Pi display config appears, select it in Advanced Option then press the Enter key. Next, select the A1 Expanded Filesystem option. You will be warned that the new filesystem will be active after your Raspberry Pi is rebooted. Select OK and Finish, if there is a request to reboot, please select Reboot.

Raspi Config Display. Select in Advanced Option
Raspi Config Display. Select in Advanced Option
Select A1 in Expand Filesystem
Select A1 in Expand Filesystem

Remove Unnecessary Applications

Next, we will try to delete the application that is not needed to save SD Card capacity. If your SD Card is large enough, and you don't want to delete applications that aren't needed, you can ignore this step.

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

Install the required software

Before we compile OpenCV, there is some software needed for installation. Type the following command on your Raspberry Pi to install supporting software.

It's a good idea to change the Repository to the Indonesian Repository so that the installation process is faster. You can follow the steps in the article Change the Repository on the Raspberry Pi.

sudo apt-get update && sudo apt-get upgrade -y
sudo apt-get install build-essential cmake unzip pkg-config -y
sudo apt-get install libjpeg-dev libpng-dev libtiff-dev -y
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev -y
sudo apt-get install libxvidcore-dev libx264-dev -y
sudo apt-get install libgtk-3-dev -y
sudo apt-get install libcanberra-gtk* -y
sudo apt-get install libatlas-base-dev gfortran -y
sudo apt-get install python3-dev -y

Download OpenCV

Next, we will try to download the latest OpenCV source. You can see the latest version via the official OpenCV page at https://opencv.org/releases/. When this article was created, the latest version is version 4.1.0. We will install the latest version. There are 2 files that we will download, namely opencv and opencv_contrib. Type the following command to download both files in the main directory.

cd ~
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

The command will download the OpenCV file and save it to two files with the name opencv.zip and opencv_contrib.zip. Next, extract the both files.

unzip opencv.zip
unzip opencv_contrib.zip

The extraction results above are 2 folders, namely opencv-4.1.0 and opencv_contrib-4.1.0. We will rename the two folders to make the installation easier to become opencv and opencv_contrib.

mv opencv-4.1.0 opencv
mv opencv_contrib-4.1.0 opencv_contrib

Delete the opencv.zip and opencv_contrib.zip files to save space.

rm -f opencv.zip opencv_contrib.zip

Configure the Virtual Environment for Python3

The Virtual Environment is useful so that all installations performed only work on a virtual system. This will make it easier for the installation to not mix with the primary system. Also, the virtual environment will run what version of the default Python will be run. With the Virtual Environment, it will isolate the primary system from error configuration errors. We will install virtualenvwrapper with the pip command.

sudo pip install virtualenv virtualenvwrapper
sudo pip3 install virtualenv virtualenvwrapper
sudo rm -rf ~/get-pip.py ~/.cache/pip

To complete the installation, we will update the ~/.profile file. Open and modify ~/.profile with the nano command.

nano ~/.profile

Then paste the following code.

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

Save the ~/.profile file by pressing the CTRL+X button followed by confirmation Y.

Contoh hasil akhir file ~/.profile
Contoh hasil akhir file ~/.profile

Next, use the ~/.profile file as source.

source ~/.profile

The configuration is complete. Then we will try to create a new Virtual Environment, for example with the name cv with the Python 3 version as default.

mkvirtualenv cv -p python3

Next, we will work on the cv environment.

workon cv

Now we are in the Virtual Environment cv, in your terminal, it will be marked with a sign (cv) as shown below.

Virtual Environment cv
Virtual Environment cv

Install NumPy

Next, we will install numpy in our environment. NumPy is a Python library for Scientific Computing. Type the following code, make sure you are in the Virtual Environment cv.

pip install numpy

Next, we will compile OpenCV in environment cv.

Compile OpenCV

OpenCV CMake Configuration

Before the compilation process, we will configure OpenCV. All configurations will be saved in the build folder in the main folder of OpenCV. Type the following command.

cd ~/opencv
mkdir build
cd build

Then run the cmake command with the following configuration.

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 OPENCV_ENABLE_NONFREE=ON \
    -D INSTALL_PYTHON_EXAMPLES=OFF \
    -D BUILD_EXAMPLES=OFF ..

Wait until the CMake process is complete. At this stage, the process is quite long. You have to wait patiently.

Increase Swap

Swap Space will help main Raspberry pi memory. We will try to increase the swap space to 2GB. The trick is to edit the file /etc/dphys-swapfile, and configure it in the CONF_SWAPSIZE variable. By default, the Raspberry pi swap size is 100.

sudo nano /etc/dphys-swapfile

Edit in the CONF_SWAPSIZE section to:

CONF_SWAPSIZE=2048

The final file results, like the example in the image below. Press CTRL+X and confirm Y to save the file.

Increase SWAP space limit
Increase SWAP space limit

Next, apply the new swap size then start the swap service with the following command.

sudo dphys-swapfile setup
sudo dphys-swapfile swapon

You can check the new swap configuration with the command:

free -m

The output can look like the image below. It can be seen that the new total swap is 2047MB (or mean 2GB).

Check New Swap Configuration
Check New Swap Configuration

Now you are ready to compile OpenCV.

Compile OpenCV

We will compile OpenCV using all Raspberry Pi processor cores. This will use all processor resources and will heat up your Raspberry Pi. Make sure your Raspberry Pi uses a heatsink or fan cooler, and store your Raspberry Pi in a quite cold room.

During the make process, it will take a long time, so you must be patient enough. Type the following command to start the process.

make -j4

The -j4 option will use all 4 of your processor cores. You can use 2 cores or 3 cores. Consequently, the process will be longer.

After the compilation process is complete, install with the following command.

sudo make install
sudo ldconfig

After the compilation and install process for OpenCV is complete, then we will create the OpenCV symlink module so that it can work in the Virtual Environment. Type the following command to create a symlink.

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

OpenCV Test

Next, we will do an OpenCV test, whether it can work in our Virtual Environment. Type the following command to execute python in the cv environment.

workon cv
python
>>> import cv2
>>> cv2.__version__
>>> exit()

Alternatively, by:

python -c 'import cv2; print(cv2.__version__)'

In the above command, we will run a python program that displays a version of OpenCV. If you get a response like below:

'4.1.0'
OpenCV Test Results
OpenCV Test Results

That means OpenCV is ready to use. Until this stage, the OpenCV Installation process is complete. In the next article, we will make a simple project of Computer Vision with OpenCV.

Conclusion

Installing OpenCV on the Raspberry Pi is quite complicated and quite time-consuming. You must be careful step by step without missing anything. However, the complexity of installing OpenCV will pay off when you make the first project later. We will try to make a human detection security camera, a face detection camera, and deep learning object detection with TensorFlow.

Follow TeknoTut to get articles about technology.