Wednesday, July 16, 2014

StudyNote -- Install OpenCV in MacOSX for Python support

Compile OpenCV to work with Python support is simple. We need to install numpy, cython and nose to support the OpenCV. I first learnt that there is a great thing that can make the installation easier, the thing is easy_install.

This is quotation from wikipedia
"EasyInstall is a package manager for the Python programming language that provides a standard format for distributing Python programs and libraries (based on the Python Eggs wrapper). easy_install is a module bundled with setuptools, a third-party library meant to enhance the Python standard library's distutils(Distribution utilities).[1] It is analogous to RubyGems for the Ruby programming language.
By default, EasyInstall looks in the Python Package Index (PyPI) for the desired packages and uses the metadata there to download and install the package and its dependencies. It is also hosted itself on the PyPI.
Python Eggs are a way of bundling additional information with a Python project, that allows the project's dependencies to be checked and satisfied at runtime, as well as allowing projects to provide plugins for other projects.
EasyInstall is not a fully fledged package manager. It cannot list local packages nor update them all. Pip and distribute are Python applications designed to fulfil the same role as easy_install. Distribute was created specifically due to the lack of progress in easy_install development. [2] Start from 0.7, EasyInstall merged distribute."

There are a few examples about how to use this easy_install
1. If I would like to install a package by its name, easy_install will search PyPI for the latest version, download, build and install. The command is like this

easy_install nose

2. Say, after some time, I would like to update the package nose to the latest version, I can use the command like this
easy_install --upgrade nose

3. Install the a downloaded package from the location in my Mac
easy_install  /download/package_name

These are the commands that I used this time, I also found some more example from https://pythonhosted.org/setuptools/easy_install.html

I copy and paste here
Install or upgrade a package by name and version by finding links on a given “download page”:
easy_install -f http://pythonpaste.org/package_index.html SQLObject
Download a source distribution from a specified URL, automatically building and installing it:
easy_install http://example.com/path/to/MyPackage-1.2.3.tgz
Install a source distribution that’s already downloaded and extracted in the current directory (New in 0.5a9):
easy_install .
Find a source distribution or Subversion checkout URL for a package, and extract it or check it out to~/projects/sqlobject (the name will always be in all-lowercase), where it can be examined or edited. (The package will not be installed, but it can easily be installed with easy_install ~/projects/sqlobject. See Editing and Viewing Source Packages below for more info.):
easy_install --editable --build-directory ~/projects SQLObject
Install a distribution within your home dir:
easy_install --user SQLAlchemy
After, required package installed. Copy cv2.so under /OpenCV/build/lib to /usr/local/lib/python2.7/site-packages. Then add "export PYTHONPATH=/usr/local/lib/python2.7/site-packages:"$PYTHONPATH" into ~/.bash_profile.

I thought I can use PATH instead of PYTHONPATH, I tried, but when I tried with PATH, Python was not able to import cv. When tried with PYTHONPATH, Python can import both cv and cv2.

When coding, before using OpenCV, remember to import cv2 and/or cv.

DONE, let's rock coding.

Tuesday, July 15, 2014

StudyNote -- The different among .bash_profile, .bashrc and .profile

In Unix system, when a user first login the system, system will start Bourne shell which is the default one. It reads the commands from .profile. The commands in profile file are for all shells if they do not have default setting file.

.bash_profile is loaded only by bash shell at the time when bash shell is logged in. If bash shell cannot find and read .bash_profile, it will read setting from .profile instead.

When a interactive shell that is not a login shell starts, it reads the commands from ./bashrc. 

However, in Mac OSX, it runs every terminal session as a login shell, which means each time a terminal starts, it reads commands from .bash_profile instead of .bashrc.

In conclusion, .profile and .bash_profile are for login shells. .bash_profile is only for bash shell while .profile is for all shells if they do not have a default file. .bashrc is for bash shell as well, but it is loaded by non login bash shell

Monday, July 14, 2014

StudyNote -- Set up Java support for OpenCV in Mac OS

Before trying to set up Java support for OpenCV, Ant and CMake should be installed.

The way to install ant is as following,

1. Download the source code
2. Decompress the file
3. Move the folder to somewhere, I moved to /usr/local/ant
4. export the record in ~/.bash_profile.(The difference among bash_profile, profile, bashrc will be posted in next article)
5. The command I used is 'export PATH=/usr/local/apache-ant/bin:"$PATH"'

The steps to compile OpenCV for Java is as following,

1. Download OpenCV source code
2. Decompress the source code file
3. I built a folder name built/ in OpenCV folder.
The command I used to compile the binary code is 
1. cd OpenCV folder
2. mkdir built
3. cd built
4. cmake -G "Unix Makefiles" ..
The .. means the directory of the source code is in parent folder. This is very important, I ignored the dot dot and wasted a lot of time.
5. make -j2
j2 is the number of the core in your computer, my CPU is duo core.
6. make install

After it finishes, the jar file is in built/bin/, move it the origin Java Jar path and invoke to use it next time. For C/C++ use, the header files of OpenCV is in /usr/local/include, the library is in /usr/local/lib.

Done.

Or you can use MacPort, it is much more quicker and easier.