Installing OpenCV-Python
In this section, we explain how to install OpenCV 3.X with Python 2.7 on multiple platforms. If you desire it, OpenCV 3.X also supports the use of Python 3.X and it will be fully compatible with the examples in this book. Linux is recommended as the examples in this book were tested on that OS.
Windows
In order to get OpenCV-Python up and running, we need to perform the following steps:
- Install Python: Make sure you have Python 2.7.x installed on your machine. If you don't have it, you can install it from: https://www.python.org/downloads/windows/.
- Install NumPy: NumPy is a great package to do numerical computing in Python. It is very powerful and has a wide variety of functions. OpenCV-Python plays nicely with NumPy, and we will be using this package a lot during the course of this book. You can install the latest version from: http://sourceforge.net/projects/numpy/files/NumPy/.
We need to install all these packages in their default locations. Once we install Python and NumPy, we need to ensure that they're working fine. Open up the Python shell and type the following:
>>> import numpy
If the installation has gone well, this shouldn't throw up any errors. Once you confirm it, you can go ahead and download the latest OpenCV version from: http://opencv.org/downloads.html.
Once you finish downloading it, double-click to install it. We need to make a couple of changes, as follows:
- Navigate to
opencv/build/python/2.7/
. - You will see a file named
cv2.pyd
. Copy this file toC:/Python27/lib/site-packages
.
You're all set! Let's make sure that OpenCV is working. Open up the Python shell and type the following:
>>> import cv2
If you don't see any errors, then you are good to go! You are now ready to use OpenCV-Python.
macOS X
To install OpenCV-Python, we will be using Homebrew. Homebrew is a great package manager for macOS X and it will come in handy when you are installing various libraries and utilities on macOS X. If you don't have Homebrew, you can install it by running the following command in your terminal:
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Even though OS X comes with inbuilt Python, we need to install Python using Homebrew to make our lives easier. This version is called brewed Python. Once you install Homebrew, the next step is to install brewed Python. Open up the terminal, and type the following:
$ brew install python
This will automatically install it as well. Pip is a package management tool to install packages in Python, and we will be using it to install other packages. Let's make sure the brewed Python is working correctly. Go to your terminal and type the following:
$ which python
You should see /usr/local/bin/python
printed on the terminal. This means that we are using the brewed Python, and not the inbuilt system Python. Now that we have installed brewed Python, we can go ahead and add the repository, homebrew/science
, which is where OpenCV is located. Open the terminal and run the following command:
$ brew tap homebrew/science
Make sure the NumPy package is installed. If not, run the following in your terminal:
$ pip install numpy
Now, we are ready to install OpenCV. Go ahead and run the following command from your terminal:
$ brew install opencv --with-tbb --with-opengl
OpenCV is now installed on your machine, and you can find it at /usr/local/Cellar/opencv/3.1.0/
. You can't use it just yet. We need to tell Python where to find our OpenCV packages. Let's go ahead and do that by symlinking the OpenCV files. Run the following commands from your terminal (please, double check that you are actually using the right versions, as they might be slightly different):
$ cd /Library/Python/2.7/site-packages/ $ ln -s /usr/local/Cellar/opencv/3.1.0/lib/python2.7/site-packages/cv.py cv.py $ ln -s /usr/local/Cellar/opencv/3.1.0/lib/python2.7/site-packages/cv2.so cv2.so
You're all set! Let's see if it's installed properly. Open up the Python shell and type the following:
> import cv2
If the installation went well, you will not see any error messages. You are now ready to use OpenCV in Python.
If you want to use OpenCV within a virtual environment, you could follow the instructions in the Virtual environments section, applying small changes to each of the commands for macOS X.
Linux (for Ubuntu)
First, we need to install the OS requirements:
[compiler] $ sudo apt-get install build-essential [required] $ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev git libgstreamer0.10-dev libv4l-dev [optional] $ sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
Once the OS requirements are installed, we need to download and compile the latest version of OpenCV along with several supported flags to let us implement the following code samples. Here we are going to install Version 3.3.0:
$ mkdir ~/opencv $ git clone -b 3.3.0 https://github.com/opencv/opencv.git opencv $ cd opencv $ git clone https://github.com/opencv/opencv_contrib.git opencv_contrib $ mkdir release $ cd release $ 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/opencv_contrib/modules -D BUILD_PYTHON_SUPPORT=ON -D WITH_XINE=ON -D WITH_OPENGL=ON -D WITH_TBB=ON -D WITH_EIGEN=ON -D BUILD_EXAMPLES=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D BUILD_EXAMPLES=ON ../ $ make -j4 ; echo 'Running in 4 jobs' $ sudo make install
If you are using Python 3, place -D
+ flags together, as you see in the following command:
cmake -DCMAKE_BUILD_TYPE=RELEASE....
Virtual environments
If you are using virtual environments to keep your test environment completely separate from the rest of your OS, you could install a tool called virtualenvwrapper by following this tutorial: https://virtualenvwrapper.readthedocs.io/en/latest/.
To get OpenCV running on this virtualenv, we need to install the NumPy package:
$(virtual_env) pip install numpy
Following all the previous steps, just add the following three flags on compilation by cmake
(pay attention that flag CMAKE_INSTALL_PREFIX
is being redefined):
$(<env_name>) > cmake ... -D CMAKE_INSTALL_PREFIX=~/.virtualenvs/<env_name> \ -D PYTHON_EXECUTABLE=~/.virtualenvs/<env_name>/bin/python -D PYTHON_PACKAGES_PATH=~/.virtualenvs/<env_name>/lib/python<version>/site-packages ...
Let's make sure that it's installed correctly. Open up the Python shell and type the following:
> import cv2
If you don't see any errors, you are good to go.
Troubleshooting
If the cv2
library was not found, identify where the library was compiled. It should be located at /usr/local/lib/python2.7/site-packages/cv2.so
. If that is the case, make sure your Python version matches the one package that has been stored, otherwise just move it into the according site-packages
folder of Python, including same for virtualenvs.
During cmake
command execution, try to join -DMAKE
... and the rest of the -D
lines. Moreover, if execution fails during the compiling process, some libraries might be missing from the OS initial requirements. Make sure you installed them all.
You can find an official tutorial about how to install the latest version of OpenCV on Linux at the following website: http://docs.opencv.org/trunk/d7/d9f/tutorial_linux_install.html.
If you are trying to compile using Python 3, and cv2.so
is not installed, make sure you installed OS dependency Python 3 and NumPy.
OpenCV documentation
OpenCV official documentation is at http://docs.opencv.org/. There are three documentation categories: Doxygen, Sphinx, and Javadoc.
In order to obtain a better understanding of how to use each of the functions used during this book, we encourage you to open one of those doc pages and research the different uses of each OpenCV library method used in our examples. As a suggestion, Doxygen documentation has more accurate and extended information about the use of OpenCV.