Installing OpenCV-Python
Let's see how to install OpenCV with Python support on multiple platforms.
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 any error. 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.
Mac OS X
To install OpenCV-Python, we will be using Homebrew. Homebrew is a great package manager for Mac OS X and it will come in handy when you are installing various libraries and utilities on OS X. If you don't have Homebrew, you can install it by running the following command on 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 pip
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 package NumPy 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/2.4.9/
. 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:
$ cd /Library/Python/2.7/site-packages/ $ ln -s /usr/local/Cellar/opencv/2.4.9/lib/python2.7/site-packages/cv.py cv.py $ ln -s /usr/local/Cellar/opencv/2.4.9/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 message. You are now ready to use OpenCV in Python.
Linux (for Ubuntu)
Before we start, we need to install some dependencies. Let's install them using the package manager as shown below:
$ sudo apt-get -y install libopencv-dev build-essential cmake libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff4-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev libtbb-dev libqt4-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils python-scipy python-pip python-virtualenv
Now that you have installed the necessary packages, let's go ahead and build OpenCV with Python support:
$ wget "https://github.com/Itseez/opencv/archive/2.4.9.tar.gz" -O ./opencv/opencv.tar.gz $ cd opencv $ tar xvzf opencv.tar.gz -C . $ mkdir release $ cd release $ sudo apt-get –y install cmake $ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -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 ../ $ make –j4 $ sudo make install
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.
If you have any other Linux distribution, please refer to the OpenCV downloads page (http://opencv.org/downloads.html) for installation details.