Python OpenCV Tutorials – World of OpenCV, AI, Computer Vision and Robotics Examples and Tutorials http://pythonopencv.com Learn OpenCV, AI & Robotics with Python and C++ Wed, 30 Aug 2017 15:49:41 +0000 en-US hourly 1 https://wordpress.org/?v=4.8.1 World’s simplest Facial Recognition API for Python(Ubuntu Only) /worlds-simplest-facial-recognition-api-for-pythonubuntu-only/ /worlds-simplest-facial-recognition-api-for-pythonubuntu-only/#respond Sun, 27 Aug 2017 12:27:51 +0000 /?p=183 Hey Guys,

Welcome Back, in this post, we are going to discover the easiest way to discover faces in any image using OpenCV.
The Api uses three main imports :

  1. Numpy
  2. scipy.misc
  3. dlib

It’s a ready made program using which, you can Recognize and manipulate faces from Python or from the command line with the world’s simplest face recognition library.

Built using dlib‘s state-of-the-art face recognition built with deep learning. The model has an accuracy of 99.38% on the Labeled Faces in the Wild benchmark.

This also provides a simple 

1
face_recognition

 command line tool that lets you do face recognition on a folder of images from the command line!

Features

Find faces in pictures

Find all the faces that appear in a picture:

 

Find and manipulate facial features in pictures

Get the locations and outlines of each person’s eyes, nose, mouth and chin.

 

Identify faces in pictures

Recognize who appears in each photo.

 

You can even use this library with other Python libraries to do real-time face recognition:

See this example for the code.

Installation

Requirements:

Install this module from pypi using 

1
pip3

 (or 

1
pip2

 for Python 2):

pip3 install face_recognition

IMPORTANT NOTE: It’s very likely that you will run into problems when pip tries to compile the 

1
dlib

 dependency. If that happens, check out this guide to installing dlib from source (instead of from pip) to fix the error:

How to install dlib from source

After manually installing 

1
dlib

, try running 

1
pip3 install face_recognition

 again to complete your installation.

If you are still having trouble installing this, you can also try out this pre-configured VM.

Usage

Command-Line Interface

When you install 

1
face_recognition

, you get a simple command-line program called 

1
face_recognition

 that you can use to recognize faces in a photograph or folder full for photographs.

First, you need to provide a folder with one picture of each person you already know. There should be one image file for each person with the files named according to who is in the picture:

known

Next, you need a second folder with the files you want to identify:

unknown

Then in you simply run the command 

1
face_recognition

, passing in the folder of known people and the folder (or single image) with unknown people and it tells you who is in each image:

$ face_recognition ./pictures_of_people_i_know/ ./unknown_pictures/ /unknown_pictures/unknown.jpg,Barack Obama /face_recognition_test/unknown_pictures/unknown.jpg,unknown_person

There’s one line in the output for each face. The data is comma-separated with the filename and the name of the person found.

An 

1
unknown_person

 is a face in the image that didn’t match anyone in your folder of known people.

Adjusting Tolerance / Sensitivity

If you are getting multiple matches for the same person, it might be that the people in your photos look very similar and a lower tolerance value is needed to make face comparisons more strict.

You can do that with the 

1
--tolerance

 parameter. The default tolerance value is 0.6 and lower numbers make face comparisons more strict:

$ face_recognition --tolerance 0.54 ./pictures_of_people_i_know/ ./unknown_pictures/ /unknown_pictures/unknown.jpg,Barack Obama /face_recognition_test/unknown_pictures/unknown.jpg,unknown_person

If you want to see the face distance calculated for each match in order to adjust the tolerance setting, you can use 

1
--show-distance true

:

$ face_recognition --show-distance true ./pictures_of_people_i_know/ ./unknown_pictures/ /unknown_pictures/unknown.jpg,Barack Obama,0.378542298956785 /face_recognition_test/unknown_pictures/unknown.jpg,unknown_person,None
More Examples

If you simply want to know the names of the people in each photograph but don’t care about file names, you could do this:

$ face_recognition ./pictures_of_people_i_know/ ./unknown_pictures/ | cut -d ',' -f2 Barack Obama unknown_person
Speeding up Face Recognition

Face recognition can be done in parallel if you have a computer with multiple CPU cores. For example if your system has 4 CPU cores, you can process about 4 times as many images in the same amount of time by using all your CPU cores in parallel.

If you are using Python 3.4 or newer, pass in a 

1
--cpus <number_of_cpu_cores_to_use>

 parameter:

$ face_recognition --cpus 4 ./pictures_of_people_i_know/ ./unknown_pictures/

You can also pass in 

1
--cpus -1

 to use all CPU cores in your system.

Python Module

You can import the 

1
face_recognition

 module and then easily manipulate faces with just a couple of lines of code. It’s super easy!

API Docs: https://face-recognition.readthedocs.io.

Automatically find all the faces in an image
import face_recognition image = face_recognition.load_image_file("my_picture.jpg") face_locations = face_recognition.face_locations(image) # face_locations is now an array listing the co-ordinates of each face!

See this example to try it out.

Automatically locate the facial features of a person in an image
import face_recognition image = face_recognition.load_image_file("my_picture.jpg") face_landmarks_list = face_recognition.face_landmarks(image) # face_landmarks_list is now an array with the locations of each facial feature in each face. # face_landmarks_list[0]['left_eye'] would be the location and outline of the first person's left eye.

See this example to try it out.

Recognize faces in images and identify who they are
import face_recognition picture_of_me = face_recognition.load_image_file("me.jpg") my_face_encoding = face_recognition.face_encodings(picture_of_me)[0] # my_face_encoding now contains a universal 'encoding' of my facial features that can be compared to any other picture of a face! unknown_picture = face_recognition.load_image_file("unknown.jpg") unknown_face_encoding = face_recognition.face_encodings(unknown_picture)[0] # Now we can see the two face encodings are of the same person with `compare_faces`! results = face_recognition.compare_faces([my_face_encoding], unknown_face_encoding) if results[0] == True: print("It's a picture of me!") else: print("It's not a picture of me!")

See this example to try it out.

Python Code Examples

All the examples are available here.

How Face Recognition Works

If you want to learn how face location and recognition work instead of depending on a black box library, read my article.

Caveats

  • The face recognition model is trained on adults and does not work very well on children. It tends to mix up children quite easy using the default comparison threshold of 0.6.

 

Common Issues

Issue: 

1
Illegal instruction (core dumped)

 when using face_recognition or running examples.

Solution: 

1
dlib

 is compiled with SSE4 or AVX support, but your CPU is too old and doesn’t support that. You’ll need to recompile 

1
dlib

 after making the code change outlined here.

Issue: 

1
RuntimeError: Unsupported image type, must be 8bit gray or RGB image.

 when running the webcam examples.

Solution: Your webcam probably isn’t set up correctly with OpenCV. Look here for more.

Issue: 

1
MemoryError

 when running 

1
pip2 install face_recognition

Solution: The face_recognition_models file is too big for your available pip cache memory. Instead, try 

1
pip2 --no-cache-dir install face_recognition

 to avoid the issue.

Issue: 

1
AttributeError: 'module' object has no attribute 'face_recognition_model_v1'

Solution: The version of 

1
dlib

 you have installed is too old. You need version 19.4 or newer. Upgrade 

1
dlib

.

Issue: 

1
TypeError: imread() got an unexpected keyword argument 'mode'

Solution: The version of 

1
scipy

 you have installed is too old. You need version 0.17 or newer. Upgrade 

1
scipy

.

Thanks

  • Thank to Adam Geitgey @https://github.com/ageitgey for this awesome tutorial. All credits for this post goes to him. I won’t take even a penny!!!
  • Many, many thanks to Davis King (@nulhom) for creating dlib and for providing the trained facial feature detection and face encoding models used in this library. For more information on the ResNet that powers the face encodings, check out his blog post.
  • Thanks to everyone who works on all the awesome Python data science libraries like numpy, scipy, scikit-image, pillow, etc, etc that makes this kind of stuff so easy and fun in Python.
  • Thanks to Cookiecutter and the audreyr/cookiecutter-pypackage project template for making Python project packaging way more tolerable.
]]>
/worlds-simplest-facial-recognition-api-for-pythonubuntu-only/feed/ 0
Installation of OpenCV 3.3/3.2/3.0 and Python 2.7 on Windows 10 (64-bit) /installation-of-opencv-3-33-23-0-and-python-2-7-on-windows-10-64-bit/ /installation-of-opencv-3-33-23-0-and-python-2-7-on-windows-10-64-bit/#respond Sun, 27 Aug 2017 09:42:57 +0000 /?p=175 Hello all,

Today, we’ll setup our environment with OpenCV 3 with Python on Windows 10. If you have noticed, we have already covered the Step by Step for Windows 10 OpenCV 3.3 for C++ builds.
Follow that tutorial if you would like to configure a C++ build instead of Python.

Alright, let’s get started.

Note: Bold indicates something that will change depending on your version of OpenCV, Python, or NumPy

1) If you have a version of both Python 2.X and Python 3.X installed on your computer currently it would be recommended to uninstall Python 3.X, remove any references to Python 3.X in your PATH variable, then reboot before continuing. This guide will not cover installation/configuration with OpenCV in the case of concurrent Python 2.X and Python 3.X installs.

2) Download the latest version of OpenCV, ex. OpenCV 3.3

3) Make a folder “C:\OpenCV-X.X.X” for your version of OpenCV, for example. “C:\OpenCV-3.3.0” and extract OpenCV there.

4a) Download and install the latest Python 2.X (NOT Python 3.X), for example 2.7.10 for x64 if you are on Windows 64-Bit.

4b) Some users have experienced Python hanging on Windows 10 when attempting to install before the installation screen appears. If this happens to you, it is possible that not running as an administrator may be the cause, see Appendix A.

4c) During the install, on the screen “Customize Python 2.7.10“, scroll down to “Add python.exe to Path“, click on the drop down and choose “Will be installed on the local hard drive”, this will add Python to your PATH (for all other install options the defaults are ok)

5) Reboot and make sure “C:\Python27\” is in your path variable, if not, add it (also remove any other Python paths) then reboot again

6a) Download and install the latest NumPy matching your version of Python 2.X, for example “numpy-1.9.2-win32-superpack-python2.7.exe
Note that the version of NumPy has to match within 2.X, i.e. if you have Python 2.7.X, the NumPy for Python 2.5.X or 2.6.X will not work.

 

 

6b) If the NumPy install hangs before the installation screen appears, reboot, then rather than double-clicking on the NumPy download, instead right click the NumPy download and choose “Run as administrator”

7) If you do not want to use IDLE (the editor that ships with Python), download and install your editor of choice. PyCharm Community Edition by JetBrains (approx. 150 MB) is highly recommended (yes, it’s free, and has good auto code completion)

8a) Copy “cv2.pyd”
from: C:\OpenCV-X.X.X\opencv\build\python\2.7\x64\cv2.pyd
(note that starting OpenCV3.2, you won’t see an x86 directory for cv2.pyd)
To:
C:\Python27\Lib\site-packages

8b) Reboot (might not be required though)

9) Download either of the following tutorials:
Image_Still.py (uses a still image)
My_Webcam.py (uses a webcam)
BallTracker.py (tracks a red ball, uses a webcam)

10) If you are using an example with a still image (i.e. Image_Still.py), copy any JPEG image into the project directory and rename it “image.jpg”(if you are using a webcam example then this step does not apply).

11) Run the program, for those of you that are new to Python, this can be done in one of at least 3 ways:
a) choose run in your chosen Python editor
b) double click on the .py file in Windows Explorer : Easiest
c) run from the operating system command prompt, i.e. Locate to the folder where you have stored your file and then run “python <file_name>” without quotes. If you have setup the environment variable correctly, it will work fine.

That’s the end of this easy tutorial, happy coding.

Appendix A –

How to install Python as an Administrator in Windows 10
If downloading Python and double-clicking on the downloaded .msi file as usual results in the Python install hanging before it begins, try the following steps to install Python as an administrator. With executables you can simply right-click on the downloaded file and choose “Run as administrator”, but the Python .msi file is not an executable and therefore does not present this option. The following is a work around to install Python as an administrator in Windows 10:
A1) In the “Search the web and Windows” box at the lower left, type “cmd”, an option “Command Prompt” should appear.
A2) Right-click on “Command Prompt” and choose “Run as administrator”
A3) cd to the directory where you have downloaded the Python .msi install file.
A4) Type the name of the python install file, ex. python-2.7.10.msi

Error and Troubleshooting:

Error#1: ImportError: DLL load failed: %1 is not a valid Win32 application.
But you have successfully copied the cv2.pyd to the python directory? what to do?
Answer: if this is the case, it means that you are using a 32-bit python but you have copied a 64-bit cv.pyd into the site-packages.
You have two options here, either install 64-bit python from the following link or you can downgrade the openCV version to an earlier version that 3.0 which supports x86 file python. I would recommend going with former if you are on windows 10 x64-bit.

Error#2: ImportError: numpy.core.multiarray failed to import
Answer: If you get this message even after installing the numpy correctly, it simply means that there are some configuration issues with numpy. Nothing to worry.
Open command prompt and cd/navigate to : C:\Python27\Scripts
run the below command:
pip install -U numpy
And you should be good to go.

/////DEMO//////

]]>
/installation-of-opencv-3-33-23-0-and-python-2-7-on-windows-10-64-bit/feed/ 0
Swap colors in an image using OpenCV and Python /swap-colors-in-an-image-using-opencv-and-python/ /swap-colors-in-an-image-using-opencv-and-python/#respond Sun, 20 Aug 2017 19:26:49 +0000 /?p=149 Hello,

In today’s post, we’ll discuss how to swap colors in an image using OpenCV and Python. Often, while writing OpenCV program, you’ll have a need to change the source color to a different color for maybe better understanding or computation. Here’s how you can easily achieve it.
There is a pre-requisite to have numpy installed. NumPy is awesome, because it allows you to manipulate tabular data, like tables and images, in a vectorized manner. In our example, we’ll use numpy so that we don’t have to write a for-loop to change all the pixels by iterating over them but instead, we can use a declarative selector which selects all black pixels in the image and then assign them a desired value which in our case is RED.

 

1
2
3
4
5
6
import numpy as np
import cv2

im = cv2.imread('your_input_image.png')
im[np.where((im == [0,0,0]).all(axis = 2))] = [0,33,166]
cv2.imwrite('your_output_image.png', im)

Result: 

 

]]>
/swap-colors-in-an-image-using-opencv-and-python/feed/ 0
Install OpenCV 3.3 and Python(2.7 & 3.5) bindings on Ubuntu 16.04 /install-opencv-3-3-and-python2-7-3-5-bindings-on-ubuntu-16-04/ /install-opencv-3-3-and-python2-7-3-5-bindings-on-ubuntu-16-04/#respond Sat, 12 Aug 2017 15:36:41 +0000 http://139.59.69.46/?p=131 Hello,

In this post, we are going to cover, how to install OpenCV 3.3 and Python 2.7 or 3.5 bindings on latest and greatest Ubuntu 16.04.

For windows users, please follow this blog post Install OpenCV 3.3 on Windows with Visual Studio 2015

Even if you already have installed Python on Ubuntu, you can still follow this guide to configure OpenCV on Ubuntu.

Install OpenCV and Python on Ubuntu 16.04

It is important to note that Ubuntu 16.04 actually ships out-of-the-box with both Python 2.7 and Python 3.5 installed. The actual versions are:

  • Python 2.7.12 (used by default when you type python in your terminal).
  • Python 3.5.2 (can be accessed via the python3 command).

In either case, this tutorial will support both Python 2.7 and Python 3. I have tried my best to highlighted the steps that will require you to make a decision regarding which version of Python you would like to use. Make sure you are consistent with your decision, otherwise you will inevitably run into compile, linking, and import errors and a huge time loss.

Regarding which Python version is the best and which one you should use…we can’t cover that in this post. If you have prior experience with any of them, use that otherwise python3 is the future. Long story short: the bindings will work just the same.

Enough talk, let’s get to work.

Step #1 : Install OpenCV dependencies on Ubuntu 16.04

Launch your Ubuntu Terminal either using console session or using Putty and update the aptget package manager to refresh and upgrade and pre-installed packages/libraries:

1
2
3
#Install OpenCV with Python on Ubuntu 16.04
sudo apt-get update
sudo apt-get upgrade

Next, install developer tools:
The pkg-config package should already be installed on your system, but be sure to include it in the above apt-get command just in case.
The cmake program is used to automatically configure our OpenCV build.

1
2
#Install OpenCV with Python on Ubuntu 16.04
sudo apt-get install build-essential cmake pkg-config

Since, OpenCV is an image processing and computer vision library, therefore, OpenCV needs to be able to load various image file formats from disk such as JPEG, PNG, TIFF, etc. In order to load these images from disk, OpenCV actually depends upon other image I/O libraries which actually facilitate the loading and decoding process. We install the necessary ones below:

1
2
#Install OpenCV with Python on Ubuntu 16.04
sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev

Good Job, we now have the libraries to load the images from disk but since, we are most likely to use our webcam’s or external camera’s to taste the full flavor of opencv, we need to install additional packages to process video streams from cameras :

1
2
3
#Install OpenCV with Python on Ubuntu 16.04
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev

Default OpenCV setup comprises of very limited GUI tools. These assist us to display an image, track mouse events, mouse sliders/trackbars etc. highgui is the module that contains all these which relies on GTK library, which can be installed using the following command.

1
2
#Install OpenCV with Python on Ubuntu 16.04
sudo apt-get install libgtk-3-dev

Next, we install libraries that are used to optimize various functionalities inside OpenCV, such as matrix operations:

1
2
#Install OpenCV with Python on Ubuntu 16.04
sudo apt-get install libatlas-base-dev gfortran

Finally, to conclude Step #1, we’ll install Python development headers and libraries for both Python 2.7 and Python 3.5 (that way you have both):

1
2
#Install OpenCV with Python on Ubuntu 16.04
sudo apt-get install python2.7-dev python3.5-dev

Note: If you do not install the Python development headers and static library, you’ll run into issues during Step #4 where we run cmake to configure our build. If these headers are not installed, then the cmake command will be unable to automatically determine the proper values of the Python interpreter and Python libraries. In short, the output of this section will look “empty” and you will not be able to build the Python bindings. When you get to Step #4, take the time to compare your output of the command to mine.

Step #2: Download the OpenCV source code
At the time of this blog post, the most recent version of OpenCV is 3.3.0 , which we download as a .zip and unzip it using the following command:

1
2
3
4
#Install OpenCV with Python on Ubuntu 16.04
cd ~
wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.3.0.zip
unzip opencv.zip

We also need the opencv_contrib repository as well:

1
2
3
#Install OpenCV with Python on Ubuntu 16.04
wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.3.0.zip
unzip opencv_contrib.zip

Why download the contrib repo?
Because, we want the full blown install of OpenCV 3.3 to have access to special features as well such as SIFT and SURF. In OpenCV 2.4, SIFT and SURF were par of the default installation of OpenCV. However, with the release of OpenCV 3+, these packages have been moved to contrib.

Also, make sure that both opencv and opencv_contrib versions should be the same (in this case, 3.3.0 ). If the versions numbers do not matchup, you could very easily run into compile time errors (or worse, runtime errors that are near impossible to debug).

Step #3: Setup your Python 2.7 or Python 3 Bindings and Environment

Okay, so up till here, we have downloaded the required libraries for OpenCV to work and also downloaded OpenCV Source and contrib. Now, we are ready to configure our Python Development Environment for the build. First step is to install PIP which is a Python Package Manager:

1
2
3
4
#Install OpenCV with Python on Ubuntu 16.04
cd ~
wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py

Okay so before moving on, we need to talk a bit about Virtual Environments and how they can help you in segregating your environments.
Virtualenv is a tool to create isolated Python environments. Long story short, using these packages, you can solve the “Project A depends on version 1.x, but Project Z needs 4.x dilemma. A fantastic side effect of using Python virtual environments is that you can keep your system Python neat, tidy, and free from clutter. I am a fan of virtual environment and would recommend you to use it. We’ll use them by doing the following:

1
2
3
#Install OpenCV 3.3 with Python on Ubuntu 16.04
sudo pip install virtualenv virtualenvwrapper
sudo rm -rf ~/get-pip.py ~/.cache/pip

Once we have virtualenv and virtualenvwrapper installed, we need to update our ~/.bashrc file to include the following lines at the bottom of the file:

1
2
3
#Install OpenCV 3.3 with Python on Ubuntu 16.04
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

The ~/.bashrc file is simply a shell script that Bash runs whenever you launch a new terminal window. You normally use this file to set various configurations. In this case, we are setting an environment variable called WORKON_HOME to point to the directory where our Python virtual environments live. We then load any necessary configurations from virtualenvwrapper.

1
2
3
4
#Install OpenCV 3.3 with Python on Ubuntu 16.04
echo -e "\n# virtualenv and virtualenvwrapper" &gt;&gt; ~/.bashrc
echo "export WORKON_HOME=$HOME/.virtualenvs" &gt;&gt; ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" &gt;&gt; ~/.bashrc

After editing our ~/.bashrc file, we need to reload the changes:

1
2
#Install OpenCV 3.3 with Python on Ubuntu 16.04
source ~/.bashrc

Note: Calling source on .bashrc only has to be done once for our current shell session.
Anytime we open up a new terminal, the contents of .bashrc will be automatically executed (including our updates).

Now that we have installed virtualenv and virtualenvwrapper , the next step is to actually create the Python virtual environment — we do this using the mkvirtualenv command.

Here, you need to make your choice, would you like to choose Python 2.7 or Python 3? Your answer would define the next command

1
2
#Install OpenCV 3.3 with Python on Ubuntu 16.04
mkvirtualenv cv -p python2

Otherwise,

1
2
#Install OpenCV 3.3 with Python on Ubuntu 16.04
mkvirtualenv cv -p python3

Whichever command you use, the end result is that we have created a Python virtual environment named cv (short for “computer vision”). You can name it whatever you want(ai, vi, mi, myworld, myvision, yourvision etc. etc.)

You can verify that it works by simply running the below command:

1
2
#Install OpenCV 3.3 with Python on Ubuntu 16.04
workon cv

To validate that you are in the cv virtual environment, simply examine your command line — if you see the text (cv) preceding your prompt, then you are in the cv virtual environment:

 

Let’s install the great NumPy into your Python virtual environment

This the final step before we compile OpenCV. NumPy is a Python package used for numerical processing. Execute the following command:

1
2
#Install OpenCV 3.3 with Python on Ubuntu 16.04
pip install numpy

Step #4: Configuring and compiling OpenCV on Ubuntu 16.04

At this point, all of our necessary prerequisites have been installed — we are now ready to compile and OpenCV!

1
2
3
4
5
6
7
8
9
10
11
12
#Install OpenCV with Python on Ubuntu 16.04
workon cv
cd ~/opencv-3.3.0/
mkdir build
cd build
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-3.3.0/modules \
-D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \
-D BUILD_EXAMPLES=ON ..

Note: If you are getting an error related to stdlib.h: No such file or directory during either the cmake or make phase of this tutorial you’ll also need to include the following option to CMake: -D ENABLE_PRECOMPILED_HEADERS=OFF . In this case I would suggest deleting your build directory, re-creating it, and then re-running CMake with the above option included. This will resolve the stdlib.h error.

Inside this directory we create a sub-directory named build and change into it. The build directory is where the actual compile is going to take place.

Finally, we execute cmake to configure our build.

Before we move on to the actual compilation of OpenCV, make sure you examine the output of CMake!

To do this, scroll down the section titled Python 2 and Python 3 .

If you are compiling OpenCV on Ubuntu 16.04 with Python 2.7 support, make sure the Python 2 section includes valid paths to the Interpreter , Libraries , numpy , and packages path . Your output should be similar to mine below:

Examining this output, you can see that:

The Interpreter points to the Python 2.7 binary in the cv virtual environment.
Libraries points to the Python 2.7 library (which we installed during the final step of Step #1).
The numpy value points to our NumPy installation in the cv virtual environment.
And finally, the packages path points to lib/python2.7/site-packages . When combined with the CMAKE_INSTALL_PREFIX , this means that after compiling OpenCV, we’ll find our cv2.so bindings in /usr/local/lib/python2.7/site-packages/ .
Similarly, if you’re compiling OpenCV 16.04 with Python 3 support, you’ll want to ensure your Python 3 section looks similar to mine below:

Again, notice how my Interpreter , Libraries , numpy and packages path have all been correctly set.

If you do not see the cv virtual environments in these variable paths, it’s almost certainly because you are NOT in the cv virtual environment prior to running CMake!

If that is indeed the case, simply access the cv virtual environment by calling workon cv and re-run the CMake command mentioned above.

Assuming your CMake command exited without any errors, you can now compile OpenCV:

1
2
#Install OpenCV with Python on Ubuntu 16.04
make -j8

The -j switch controls the number of processes to be used when compiling OpenCV — you’ll want to set this value to the number of processors/cores on your machine. In my case, I have a octa-core processor, so I set -j8 . Using multiple processes allows OpenCV to compile faster; however, there are times where race conditions are hit and the compile bombs out.

Below you can find a screenshot of a successful OpenCV 3.3 with Python compile on Ubuntu 16.04

Hurray, Finally, The last step is to actually install OpenCV 3 on Ubuntu 16.04:

1
2
3
4
#Install OpenCV with Python on Ubuntu 16.04
cd ~
sudo make install
sudo ldconfig

Step #5: Finish your OpenCV install
Hold tight, you are almost there, just a few more steps and we are done with it…

For Python 2.7:
After running sudo make install , your Python 2.7 bindings for OpenCV 3 should now be located in /usr/local/lib/python-2.7/site-packages/ . You can verify this using the ls command:

1
2
3
4
#Install OpenCV with Python on Ubuntu 16.04
ls -l /usr/local/lib/python2.7/site-packages/
total 1972
-rw-r--r-- 1 root staff 2016608 Sep 15 09:11 cv2.so

Note: In some cases, you may find that OpenCV was installed in /usr/local/lib/python2.7/dist-packages rather than /usr/local/lib/python2.7/site-packages (note dist-packages versus site-packages ). If your cv2.so bindings are not in the site-packages directory, be sure to check dist-pakages .

The final step is to sym-link our OpenCV cv2.so bindings into our cv virtual environment for Python 2.7:

1
2
3
#Install OpenCV with Python on Ubuntu 16.04
cd ~/.virtualenvs/cv/lib/python2.7/site-packages/
ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so

For Python 3.5:
After running sudo make install , your OpenCV + Python 3 bindings should be located in /usr/local/lib/python3.5/site-packages/ . Again, you can verify this using the ls command:

1
2
3
4
#Install OpenCV with Python on Ubuntu 16.04
ls -l /usr/local/lib/python3.5/site-packages/
total 1972
-rw-r--r-- 1 root staff 2016816 Sep 13 17:24 cv2.cpython-35m-x86_64-linux-gnu.so

For python 3, the filename might not be cv2.so, so we need to change it and symlink it.

1
2
3
4
5
#Install OpenCV with Python on Ubuntu 16.04
cd /usr/local/lib/python3.5/site-packages/
sudo mv cv2.cpython-35m-x86_64-linux-gnu.so cv2.so
cd ~/.virtualenvs/cv/lib/python3.5/site-packages/
ln -s /usr/local/lib/python3.5/site-packages/cv2.so cv2.so

Step #6: Testing your OpenCV install
First of all, Congratulations, you now have OpenCV 3.3 installed on your Ubuntu 16.04 system!

To verify that your installation is working:

Open up a new terminal.
Execute the workon command to access the cv Python virtual environment.
Attempt to import the Python + OpenCV bindings.
Below are the steps on how to perform it:

1
2
3
4
5
6
7
8
9
10
#Install OpenCV with Python on Ubuntu 16.04
cd ~
workon cv
python
Python 3.5.2 (default, Jul 5 2016, 12:43:10)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
import cv2
cv2.__version__
'3.1.0'

That’s all for this post.
Before i sum-up, i would like to share my personal thanks message and lots of credits to my guy Adrian for this wonderful tutorial that he shared on his blog.

]]>
/install-opencv-3-3-and-python2-7-3-5-bindings-on-ubuntu-16-04/feed/ 0