Tuesday 23 April 2013

How to install OpenCV on OpenShift

I wanted to deploy an OpenCV(Python) application on the internet and needed a free hosting service after searching I found OpenShift which is an Platform as a service provider. They offer a variety of application type (python, ruby, php, django etc.) The free tier allow you to host 3 application with 1GB of storage. So it took me weeks before I could get OpenCV installed properly with no errors - one major problem was storage constraints so I had to delete any file that was unimportant and install only the necessities. After the whole thing was over I thought it would be nice to write about it.

Sign up and create an application

The first step would be to sign up for an OpenShift account. Then create a new application select a Do-It-Yourself (custom) application and complete the wizard. After the wizard is completed you would need to go to the application page and copy the secure shell (ssh) session command which you can access by clicking the "WANT TO LOG IN TO YOUR APPLICATION?" link. Having copied the command, open a terminal and paste it to get started.

Installation

Disclaimer: From here on you're going to be looking at loads of scrolling text. So if you are allergic to boredom get a drink or open up a movie on the side. Copy the scripts below and paste in the command line. Note that I delete the apps , data and doc folders from the OpenCV installation so as to save space this could be a problem if you need haarcascade.xml or any stuff located in these folders, so if that is your case you must check and customize your installation from line 57 of the script but be careful to avoid the "Disk quota exceeded" error.
# Install python
cd $OPENSHIFT_TMP_DIR
wget http://python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2
tar jxf Python-2.7.3.tar.bz2
cd Python-2.7.3
./configure --prefix=$OPENSHIFT_DATA_DIR --enable-shared
make
make altinstall

export PATH=$OPENSHIFT_DATA_DIR/bin:$PATH
export LD_LIBRARY_PATH=$OPENSHIFT_DATA_DIR/lib

# Install setuptools
cd $OPENSHIFT_TMP_DIR
wget --no-check-certificate https://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg#md5=fe1f997bc722265116870bc7919059ea
sh setuptools-0.6c11-py2.7.egg

export PATH=$OPENSHIFT_DATA_DIR/bin:$PATH
export LD_LIBRARY_PATH=$OPENSHIFT_DATA_DIR/lib

# Install pip
cd $OPENSHIFT_TMP_DIR
wget --no-check-certificate http://pypi.python.org/packages/source/p/pip/pip-1.1.tar.gz
tar zxf pip-1.1.tar.gz
cd pip-1.1
python2.7 setup.py install --prefix=$OPENSHIFT_DATA_DIR/lib/python2.7/site-packages/
python2.7 setup.py install

export PATH=$OPENSHIFT_DATA_DIR/bin:$PATH
export LD_LIBRARY_PATH=$OPENSHIFT_DATA_DIR/lib

# Install numpy
$OPENSHIFT_DATA_DIR/bin/pip install numpy

# House-cleaning
cd $OPENSHIFT_TMP_DIR
rm -r Python-2.7.3
rm Python-2.7.3.tar.bz2
rm -r pip-1.1/
rm pip-1.1.tar.gz
rm setuptools-0.6c11-py2.7.egg

# Install CMake
cd $OPENSHIFT_TMP_DIR
wget http://www.cmake.org/files/v2.8/cmake-2.8.10.2.tar.gz
tar xvf cmake-2.8.10.2.tar.gz
cd cmake-2.8.10.2
./configure --prefix=$OPENSHIFT_DATA_DIR
gmake
gmake install

# Download OPenCV
cd $OPENSHIFT_TMP_DIR
wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.3/OpenCV-2.4.3.tar.bz2/download
tar xvf OpenCV-2.4.3.tar.bz2

# More House-cleaning
rm cmake-2.8.10.2.tar.gz
rm OpenCV-2.4.3.tar.bz2
cd OpenCV-2.4.3/
rm -r 3rdparty/
rm -r android
rm -r doc
rm -r data
rm -r ios
rm -r samples
rm -r apps
rm README
rm -r ../cmake-2.8.10.2/Tests
rm -r ../cmake-2.8.10.2/Testing
rm -r ../cmake-2.8.10.2/Example
rm -r ../cmake-2.8.10.2/Docs
rm -r ../cmake-2.8.10.2/Source
Congratulations you are now half way through the process, But now you have to do something before installing OpenCV. To reduce the build size; we turn off build tests and remove the lines that copy the data, apps and doc folders. Just follow these steps:
  1. Open CMakeList.txt in vi editor using the command vi CMakeList.txt.
  2. Show line numbers using the :set number command.
  3. Go to Lines 155 and 156 and turn off regression and performance test.
  4. Delete Lines 448 to 456.
  5. Save and Quit using the :wq command.
mkdir release
cd release

$OPENSHIFT_DATA_DIR/bin/cmake cmake ../OpenCV-2.4.3 -D BUILD_NEW_PYTHON_SUPPORT=ON -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=$OPENSHIFT_DATA_DIR -D PYTHON_LIBRARY=$OPENSHIFT_DATA_DIR/lib/libpython2.7.so -D CMAKE_INCLUDE_PATH=$OPENSHIFT_DATA_DIR/include/python2.7 -D PYTHON_INCLUDE_DIR=$OPENSHIFT_DATA_DIR/include/python2.7 -D PYTHON_PACKAGES_PATH=$OPENSHIFT_DATA_DIR/lib/python2.7/site-packages -D PYTHON_EXECUTABLE=$OPENSHIFT_DATA_DIR/bin/python2.7 -D WITH_OPENEXR=OFF -D BUILD_DOCS=OFF -DBUILD_SHARED_LIBS=ON ..

make 
make install

Test

Open the python console by typing python2.7 in the command line. import cv2, if you have no error you are good to go but if you have a "NameError: name 'cv2' is not defined" something is wrong.
Python 2.7.3 (default, May 28 2013, 07:58:37) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> A = cv2.imread("A.jpg")
>>> print A
None

Conclusion

This concludes the installation you can now clean up the $OPENSHIFT_TMP folder and install Django or any other web framework you want to use. Hope this was helpful, feel free to share this post and ask question if any. Happy Coding!

10 comments:

  1. I need help with that, I think that I found 2 errors.
    The first one is "wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.3/OpenCV-2.4.3.tar.bz2/download " --> dowload not needed,
    the second one is the name of CMakeList.txt is CMakeLists.txt. (Lists)
    After that i did everything but at the end, python2.7 not found. I added the variable path again and it worked, but I have an error with the ***** import cv2 (not found)

    Please, anyone can help me?

    ReplyDelete
    Replies
    1. Hi Adria,

      Could you post the exact error message that you get...

      Delete
    2. [photopotter-pew14.rhcloud.com release]\> python2.7
      bash: python2.7: command not found
      [photopotter-pew14.rhcloud.com release]\> export PATH=$OPENSHIFT_DATA_DIR/bin:$
      PATH
      [photopotter-pew14.rhcloud.com release]\> export LD_LIBRARY_PATH=$OPENSHIFT_DAT
      A_DIR/lib
      [photopotter-pew14.rhcloud.com release]\> python2.7
      Python 2.7.3 (default, Apr 9 2014, 07:44:05)
      [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
      Type "help", "copyright", "credits" or "license" for more information.
      >>> import cv2
      Traceback (most recent call last):
      File "", line 1, in
      ImportError: No module named cv2

      Delete
  2. Hola segui este tutorial paso a paso pero no consigo configurar la cv2 para python:
    en Openshift hize una app:

    rhc app create servidorprueba python-2.6 --from-code https://github.com/openshift-quickstart/bottle-openshift-quickstart.git
    Luego de configurar todo este post por ssh:

    [servidorprueba-especievegetal.rhcloud.com release]\> export PATH=$OPENSHIFT_DA
    TA_DIR/bin:$PATH
    [servidorprueba-especievegetal.rhcloud.com release]\> export LD_LIBRARY_PATH=$O
    PENSHIFT_DATA_DIR/lib
    [servidorprueba-especievegetal.rhcloud.com release]\> python2.7 -V
    Python 2.7.7
    [servidorprueba-especievegetal.rhcloud.com release]\> python2.7
    Python 2.7.7 (default, Feb 26 2015, 14:35:40)
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import numpy
    >>> import cv2
    Traceback (most recent call last):
    File "", line 1, in
    ImportError: No module named cv2

    AYUDAaaaa. Gracias

    ReplyDelete
    Replies
    1. try

      $ export PYTHONPATH=$OPENSHIFT_DATA_DIR/lib/python2.7/site-packages/

      Delete
    2. Gracias Stephen Nneji
      Yo reemplaze el opencv 2.4.3 por el 2.4.10 y posterormente utilice
      export PYTHONPATH=$OPENSHIFT_DATA_DIR/lib/python2.7/site-packages/
      *******RESULTADO***********
      [servidorcv-especievegetal.rhcloud.com lib]\> cd $OPENSHIFT_DATA_DIR/lib/python2.7/site-packages/
      [servidorcv-especievegetal.rhcloud.com site-packages]\> dir
      cv2.so numpy README
      cv.py numpy-1.9.2-py2.7.egg-info setuptools-0.6c11-py2.7.egg
      easy-install.pth pip-1.1-py2.7.egg setuptools.pth
      [servidorcv-especievegetal.rhcloud.com site-packages]\> python2.7
      bash: python2.7: no se encontró la orden
      [servidorcv-especievegetal.rhcloud.com site-packages]\> export PATH=$OPENSHIFT_DATA_DIR/bin:$PATH
      [servidorcv-especievegetal.rhcloud.com site-packages]\>
      [servidorcv-especievegetal.rhcloud.com site-packages]\> export LD_LIBRARY_PATH=$OPENSHIFT_DATA_DIR/lib
      [servidorcv-especievegetal.rhcloud.com site-packages]\> python2.7
      Python 2.7.3 (default, Mar 12 2015, 15:54:59)
      [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
      Type "help", "copyright", "credits" or "license" for more information.
      >>> import numpy
      >>> import cv2
      >>> import cv
      >>> from cv import CreateImageHeader, SetData, GetMat, IPL_DEPTH_8U, CV_CHAIN_APPROX_NONE
      >>> from cv2 import cvtColor, findContours, COLOR_BGR2GRAY, RETR_TREE
      >>>

      Delete
  3. Hola Stephen Nneji , tengo un problema ya logre configurar el opencv y python. pero error..
    CMD...
    $ rhc ssh servidorcv

    Welcome to OpenShift shell

    This shell will assist you in managing OpenShift applications.

    !!! IMPORTANT !!! IMPORTANT !!! IMPORTANT !!!
    Shell access is quite powerful and it is possible for you to
    accidentally damage your application. Proceed with care!
    If worse comes to worst, destroy your application with "rhc app delete"
    and recreate it
    !!! IMPORTANT !!! IMPORTANT !!! IMPORTANT !!!

    Type "help" for more info.


    [servidorcv-especievegetal.rhcloud.com 5501ecf2fcf9338e740001aa]\>
    [servidorcv-especievegetal.rhcloud.com 5501ecf2fcf9338e740001aa]\> python -V
    Python 2.6.6
    [servidorcv-especievegetal.rhcloud.com 5501ecf2fcf9338e740001aa]\> python2.7 -V

    bash: python2.7: command not found
    [servidorcv-especievegetal.rhcloud.com 5501ecf2fcf9338e740001aa]\> export PATH=
    $OPENSHIFT_DATA_DIR/bin:$PATH
    [servidorcv-especievegetal.rhcloud.com 5501ecf2fcf9338e740001aa]\> export LD_LI
    BRARY_PATH=$OPENSHIFT_DATA_DIR/lib
    [servidorcv-especievegetal.rhcloud.com 5501ecf2fcf9338e740001aa]\> python2.7 -V
    Python 2.7.3
    [servidorcv-especievegetal.rhcloud.com 5501ecf2fcf9338e740001aa]\>
    **********************************************************************************************************
    Al ejecutar mi app python utiliza el python por defecto 2.6. ¿Como logro establecer por defector el python 2.7?

    ReplyDelete
    Replies
    1. Look at the answer on this link
      http://stackoverflow.com/questions/19256127/two-versions-of-python-on-linux-how-to-make-2-7-the-default

      Delete
  4. Hi, This post helped me a lot while setting up OpenCV on OpenShift platform. This solution works like a charm. I just want to add something. Someone who is trying to compile an OpenCV c++ code, will have to set one more environmental variable named PKG_CONFIG_PATH.

    export PKG_CONFIG_PATH=$OPENSHIFT_DATA_DIR/lib/pkgconfig/

    Hope it helps.

    Thanks again.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete