Fangyijie Wang

Ubuntu Install OpenCV 3.0 And Python 3

A few days ago, I was playing around with some interesting object detection projects using OpenCV and Python3. In this blog, I’d like to share some issues I found during installation and development environment setup.

Details of my dev environment:

  • ubuntu 16.04 LTS
  • Python 3.5.2
  • OpenCV 3.1.0

Now, I’ll show you how to install them and set up a dev environment probably.

Install dependencies

First there are lots of libs and dependencies before you start installing Python or OpenCV. Please see below lists of commands. It’s better to follow them step and step to install all of them.

Upgrade any pre-installed packages:

1
2
sudo apt-get update
sudo apt-get upgrade

Install tools to compile OpenCV 3.1.0:

1
sudo apt-get install build-essential cmake git pkg-config

If you are going to develop object detection functionalities on images, then below libraries and packages are needed:

1
sudo apt-get install libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev

Also, if you need to work on videos, below libraries are needed as well:

1
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev

Install packages that are used to optimize various functions inside OpenCV, such as matrix operations:

1
sudo apt-get install libatlas-base-dev gfortran

After you run above commands to install all libraries and packages without errors, you are ready to go next step - setting up Python environment.

Set up virtual environment for Python development

NOTE: It’s highly recomended to always use virtual environment for Python development. But if you still don’t want to use virtual environment, you could skip this part.

Here I’m going to use virtualenv and virtualenvwrapper. They will help us create separate and independent Python environments, so that we don’t junk up our system Python install. Moreover we can have a separate Python environment for each of our projects).

Get pip using below commands: (You need to indicate Python3 inside the commands)

1
2
wget https://bootstrap.pypa.io/get-pip.py
sudo python3 get-pip.py

Install and setup virtualenv and virtualenvwrapper:

1
sudo pip3 install virtualenv virtualenvwrapper

Update ~/.bashrc file. Below commands need to be appended to the file:

1
2
3
4
# virtualenv and virtualenvwrapper
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

It’s pointing VIRTUALENVWRAPPER_PYTHON to where Python3.5 binary exists on Ubuntu system. Next is to reload ~/.bashrc file:

1
source ~/.bashrc

Create cv virtual environment where you could work on object detection projects using OpenCV and Python.

1
mkvirtualenv cv

To test if the virtual environment is created successfully, open another terminal, type commands:

1
workon cv

If you see cv at the begining of our terminal, it means you have set up virtual environment to use OpenCV and Python successfully.
virtual env setup

Install Python3.5

Now we have a virtual environment available for Python development. Let’s install Python 3.5 in this environment. Below command is used to install the Python 3.5 headers and development files:

1
sudo apt-get install python3.5-dev

Install necessary libraries:

1
2
pip install numpy
pip install matplotlib

You could other libraries you need.

Build and install OpenCV 3.1.0

Make sure you are still in virtual environment. Pull OpenCV projects from github and checkout the 3.1.0 version:

1
2
3
4
cd ~
git clone https://github.com/opencv/opencv.git
cd opencv
git checkout 3.1.0

Meanwhile you need to grab the opencv_contrib repo as well. Without this repository, you will be missing some of OpenCV 3.0 features like text detection in natural images:

1
2
3
4
cd ~
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout 3.1.0

Make sure that you checkout the same version for opencv_contrib that you did for OpenCV above, otherwise you could run into compilation errors.

Next step is to build OpenCV: (I’m sharing my cmake commands. You could find different commands from other website. If mine is not working on your local machine, please find others’ build commands. There must be a list of cmake commands working in your situation)

1
2
3
4
5
6
7
8
9
10
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python3.5 \
-D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so \
-D PYTHON_LIBRARIES=/usr/lib/x86_64-linux-gnu/libpython3.5m.so \
-D PYTHON3_NUMPY_INCLUDE_DIRS=~/.virtualenvs/cv/lib/python3.5/site-packages \
-D BUILD_EXAMPLES=ON ..

In order to build OpenCV 3.1.0 , you need to set -D INSTALL_C_EXAMPLES=OFF (rather than ON ) in the cmake command.

If your build output looks like below, then your build mostly is done successfully.
build OpenCV

CMake is now using the Python 3.5. It means OpenCV 3.1.0 will be compiled with Python 3.5. Let’s start the OpenCV compile process:

1
make -j4

Assuming OpenCV 3.1.0 compiled without errors, you can install it on your system:

1
2
sudo make install
sudo ldconfig

If you’ve reached this step, OpenCV 3.1.0 should now be installed in /usr/local/lib/python3.5/site-packages/. In this folder you can see OpenCV bindings are stored under the name cv2.cpython-35m.so. To use OpenCV 3.1.0 within cv virtual environment, you need to sym-link OpenCV into site-packages:

1
2
cd ~/.virtualenvs/cv/lib/python3.5/site-packages/
ln -s /usr/local/lib/python3.5/site-packages/cv2.cpython-35m.so cv2.so

After this, Python can import our OpenCV bindings using name cv2

Test OpenCV 3.1.0 and Python 3.5

Again, make sure you are in the cv virtual environment, then call Pyhthon3 and try to import cv2:

1
2
3
4
5
$ workon cv
$ python
>>> import cv2
>>> cv2.__version__
'3.1.0'

final vertification

Congratulations! You are able to use Python and OpenCV on computer vision projects!