Wednesday, June 22, 2016

Insatlling Opencv 3.0 Python 2.7 and Supporting Packages in ubuntu

Assuming U have already installed Python2.7-dev and pip (python package manager). 

Step 1:

Open up a terminal and update the apt-get  package manager followed by upgrading any pre-installed packages:
Install OpenCV 3.0 and Python 2.7+ on Ubuntu
Shell
1
2
$ sudo apt-get update
$ sudo apt-get upgrade

 

Step 2:

Now we need to install our developer tools:
The pkg-config  is likely already installed, but be sure to include it just in case. We’ll be using git  to pull down the OpenCV repositories from GitHub. The  cmake  package is used to configure our build.

 

Step 3:

OpenCV needs to be able to load various image file formats from disk, including JPEG, PNG, TIFF, etc. In order to load these image formats from disk, we’ll need our image I/O packages:
Install OpenCV 3.0 and Python 2.7+ on Ubuntu
Shell
1
$ sudo apt-get install libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev

 

Step 4:

At this point, we have the ability to load a given image off of disk. But how do we display the actual image to our screen? The answer is the GTK development library, which the highgui  module of OpenCV depends on to guild Graphical User Interfaces (GUIs):

 

Step 5:

We can load images using OpenCV, but what about processing video streams and accessing individual frames? We’ve got that covered here:

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

Step 6:

Install libraries that are used to optimize various routines inside of OpenCV:

sudo apt-get install libatlas-base-dev gfortran

Step 7:

Our environment is now all setup — we can proceed to change to our home directory, pull down OpenCV from GitHub, and checkout the 3.0.0  version:

$ cd ~
$ git clone https://github.com/Itseez/opencv.git
$ cd opencv
$ git checkout 3.1.0   

(replace 3.1.0 by latest version such as 3.2.0)


we also need the opencv_contrib repo as well. Without this repository, we won’t have access to standard keypoint detectors and local invariant descriptors (such as SIFT, SURF, etc.) that were available in the OpenCV 2.4.X version. We’ll also be missing out on some of the newer OpenCV 3.0 features like text detection in natural images:

 
$ cd ~
$ git clone https://github.com/Itseez/opencv_contrib.git
$ cd opencv_contrib
$ git checkout 3.1.0
Time to setup the build:
cd ~/opencv
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
Update (3 January 2016): In order to build OpenCV 3.1.0 , you need to set -D INSTALL_C_EXAMPLES=OFF  (rather than ON ) in the cmake  command. There is a bug in the OpenCV v3.1.0 CMake build script that can cause errors if you leave this switch on. Once you set this switch to off, CMake should run without a problem.
Now we can finally compile OpenCV:
$ make -j4
Where you can replace the 4 with the number of available cores on your processor to speedup the compilation.
 
Thanks to Adreian


Installing Supporting Packages:

sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-notebook 
python-pandas python-sympy python-nose

 

List of Support Packages available for python:

 

click here


FAQ

Package installation

There are two standard methods for installing a package.
pip install
The pip install script is available within our scientific Python installation and is very easy to use (when it works). During the installation process you already saw many examples of pip install in action. Features include:
  • If supplied with a package name then it will query the PyPI site to find out about that package. Assuming the package is there then pip install will automatically download and install the package.
  • Will accept a local tar file (assuming it contains an installable Python package) or a URL pointing to a tar file.
  • Can install in the user package area via pip install <package or URL> --user (but see discussion further down)
python setup.py install
Some packages may fail to install via pip install. Most often there will be some obvious (or not) error message about compilation or missing dependency. In this case the likely next step is to download the installation tar file and untar it. Go into the package directory and look for files like:
 
INSTALL
README
setup.py
setup.cfg

 

If there is an INSTALL or README file then hopefully you will find useful installation instructions. Most well-behaved python packages do the installation via a standard setup.py script. This is used as follows:
python setup.py --help  # get options
python setup.py install # install in the python area (root / admin req'd)
python setup.py install --user # install to user's package area

 

Where do packages get installed?

An important option in the installation process is where to put the package files. We’ve seen the --user option in pip install and python setup.py install. What’s up with that? In the section below we document how this works. See the discussion in Multiple Pythons on your computer for a reason you might want to do this, but first please read this warning:
Warning
We strongly recommend against installing packages with --user unless you are an expert and really understand what you are doing, . This is because the local user version will always take precedence and can thus potentially disrupt other Python installations and cause hard-to-understand problems. Big analysis packages like CIAO, STSci_Python or CASA are carefully tested assuming the integrated environment they provide. If you start mucking this up then all bets are off.

WITH --user

Packages get installed in a local user-owned directory when you do something like either of the following:
pip install --user aplpy
python setup.py install --user 
 
This puts the packages into:
Mac ~/Library/Python/2.x/lib/python/site-packages
Linux ~/.local/lib/python-2.x/site-packages
Windows %APPDATA%/Python/Python2x/site-packages

WITHOUT --user

 

If you use Anaconda or a non-root Python installation then there is no issue with permissions on any platform since the entire installation is local to a directory you own.

However, installing to a system-wide Python installation will require root or admin privilege. Installing this way has the benefit of making the package available for all users of the Python installation, but has the downside that it is more difficult to back out changes if required. In general we recommend using only the system package manager (e.g. yum) to install packages to the system Python. This will ensure integrity of your system Python, which is important even if you are the only user.

How do I find a package once installed?

Finding the file associated with a package or module is simple, just use the help command in IPython:
 
import scipy
help scipy

Where does Python look for modules?

The official reference on Modifying Python’s Search Path gives all the details. In summary:
When the Python interpreter executes an import statement, it looks for modules on a search path. A default value for the path is configured into the Python binary when the interpreter is built. You can determine the path by importing the sys module and printing the value of sys.path:

$ python
Python 2.2 (#11, Oct  3 2002, 13:31:27)
[GCC 2.96 20000731 (Red Hat Linux 7.3 2.96-112)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/local/lib/python2.3', '/usr/local/lib/python2.3/plat-linux2',
 '/usr/local/lib/python2.3/lib-tk', '/usr/local/lib/python2.3/lib-dynload',
 '/usr/local/lib/python2.3/site-packages']
>>>

Within a script it is possible to adjust the search path by modify sys.path which is just a Python list. Generally speaking you will want to put your path at the front of the list using insert:
import sys 
 sys.path.insert(0, '/my/path/python/packages') 
 
You can also add paths to the search path using the PYTHONPATH environment variable.


No comments:

Post a Comment