How to Install OpenCV 3.4.0 with Python 3 on Raspberry Pi 3

Introduction

OpenCV (Open Source Computer Vision) is a library of programming functions mainly aimed at real-time computer vision. It is released under a BSD license and hence it’s free for both academic and commercial use. It has C++, Python and Java interfaces and supports Windows, Linux, Mac OS, iOS and Android. OpenCV was designed for computational efficiency and with a strong focus on real-time applications.

In this post, I will show you how to install OpenCV 3.4.0 with Python 3.5 on Raspberry Pi 3. First I will show you how to install OpenCV3 without any virtual environment and how you can also use a virtual environment to work with that build file as well.

Objectives:

Instruction to install OpenCV on Raspberry Pi.

OpenCV version: 3.4.0

Target platform: Raspberry Pi 3 B

OS: Raspbian Stretch

Language: Python 3

Video Youtube:

Steps:

There are some steps to install OpenCV properly on Raspberry Pi 3 with Python 3. I will show all the steps to get it working properly.

Step 1: Expand filesystem

Type the following command to expand the Raspberry Pi3 file system

1

sudo raspi-config

Then select the following

Advanced Options > A1 Expand filesystem > Press “Enter”

It will show a message “The root partition has been resized”.

Then you need to reboot your pi using the following command.

1

sudo shutdown-rnow

Step 2: Free Up Some Space

The default OS will take around 15% if you are using 32GB card. But if you are using a 8GB memory card it might take 50% of all your space. So, it is better to remove some unused packages like LibreOffice and Wolfram engine to free up some space on your pi.

You can do it simply typing the following command on the terminal window.

# you most likely don't want this, unless you have an special disk situation

# CONF_SWAPSIZE=100

CONF_SWAPSIZE=1024

Then save the changes you’ve made, press Ctrl + O. To exit nano, type Ctrl + X. If you ask nano to exit from a modified file, it will ask you if you want to save it. Just press N in case you don’t, or Y in case you do. It will then ask you for a filename. Just type it in and press Enter.

Then type the following lines to take it into effect

1

2

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

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

Step 7: Finally Ready to be Compile

Type the following command to compile it using 4 cores of pi

1

make-j4

Step Optional: Compile with a single core of Pi

If you face any error while compiling due to memory issue you can start the compilation again with only one core using the following command

1

2

make clean

make

Step 8: Install the buildon raspberry pi

After the successful build install the build using the following command

1

2

sudo make install

sudo ldconfig

Step 9: Verify the OpenCV build

After running make install, OpenCV + Python bindings should be installed in usr/local/lib/python3.5/dist-packages or usr/local/lib/python3.5/site-packages.

You need to use the site-packages or dist-packages. Look where it has been created and use that site-packages or dist-packages. In my case it is in dist-packages.

Again, you can verify this with the ls command:

1

ls-l/usr/local/lib/python3.5/dist-packages/

Look for a name like cv2.so and if it is not there then look for a name like cv2.cpython-35m-arm-linux-gnueabihf.so (name starting with cv2. and ending with .so). It might happen due to some bugs in Python binding library for Python 3.

We need to rename cv2.cpython-35m-arm-linux-gnueabihf.so to cv2.so using the following command:

# you most likely don't want this, unless you have an special disk situation

CONF_SWAPSIZE=100

# CONF_SWAPSIZE=1024

Then save the changes you’ve made, press Ctrl + O. To exit nano, type Ctrl + X. If you ask nano to exit from a modified file, it will ask you if you want to save it. Just press N in case you don’t, or Y in case you do. It will then ask you for a filename. Just type it in and press Enter.