Installing OpenCV and Using its Python Wrapper (For Linux)

If you are new to OpenCV, here is a quick introduction taken verbatim from opencv.org. OpenCV is released under a BSD license and hence it’s free for both academic and commercial use. It has C++, C, Python and Java interfaces and supports Windows, Linux, Mac OS, iOS and Android. OpenCV was designed for computational efficiency and with a strong focus on real-time applications. Written in optimized C/C++, the library can take advantage of multi-core processing. Enabled with OpenCL, it can take advantage of the hardware acceleration of the underlying heterogeneous compute platform.

In this tutorial,  we will learn how to install OpenCV and make it available to your python installation so you can use it in your Python application. The best way to install OpenCV is to download the source code and compile it yourself using 'make' . Here are the steps you need to follow:

    1.  Download the source code from opencv.org to your computer.
    2. Unzip the folder and change the directory to the folder that was downloaded
    3. Make a new directory in this folder called 'build' and change the directory to this directory.
    4. Now install all the dependencies that you will need to build the source code.
      sudo apt-get install build-essential
      sudo apt-get install cmake libgtk2.0-dev pkg-config
      sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
      [Optional] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
      The last command is optional.
    5. Configure make by running the following command. The following command specifies that build type is “Release Mode” and installation path is /usr/local. Observe the -D before each option and .. at the end. This is the format of CMake.
      cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
      You can also add a lot more configurations depending on what features you want in your build. For example, the following command will enable TBB and Eigen.
      cmake -D WITH_TBB=ON -D WITH_EIGEN=ON ..
      To enable documentation and disable tests and samples, you would run the following command:
      cmake -D BUILD_DOCS=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF ..
      To disable all GPU related features, you would run the following:
      cmake -D WITH_OPENCL=OFF -D WITH_CUDA=OFF -D BUILD_opencv_gpu=OFF -D BUILD_opencv_gpuarithm=OFF -D BUILD_opencv_gpubgsegm=OFF -D BUILD_opencv_gpucodec=OFF -D BUILD_opencv_gpufeatures2d=OFF -D BUILD_opencv_gpufilters=OFF -D BUILD_opencv_gpuimgproc=OFF -D BUILD_opencv_gpulegacy=OFF -D BUILD_opencv_gpuoptflow=OFF -D BUILD_opencv_gpustereo=OFF -D BUILD_opencv_gpuwarping=OFF ..
    6. When you are happy with your make configuration, you are ready to build the source code by running the following command.
      sudo make install

Upon successful build, your installation will be in the folder /usr/local/lib. Depending on what version of Python you already had, you will see 'cv2.so' in the site-packages of that python folder. In my case, I had Anaconda distribution of Python 2.7 and therefore, I had 'cv2.so' file in /usr/local/lib/python2.7/site-packages
screen1

Now we need to make 'cv2.so' file available to our Python. We can do this by adding PYTHONPATH environment variable that will point to  /usr/local/lib/python2.7/site-packages. This can be done by running the following command which will add the line to your .bashrc file.
export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages

Now to test whether it works. Just go to python shell and type 'import cv2'. If you don't see any error, there you have it. Now you can use the power of OpenCV in your python application.

References:

OpenCV-Python Tutorials

OpenCV Documentation

Books

Getting Started With Caffe For Deep Learning

In your home directory:

wget https://github.com/BVLC/caffe/archive/master.zip
unzip master.zip
cd caffe-master/
cp Makefile.config.example Makefile.config

 

To compile the caffe:

make all -j4
make test -j4
make runtest

if everything goes well, inside caffe-master directory :

./data/mnist/get_mnist.sh
./examples/mnist/create_mnist.sh

 

To run mnist sample network in the caffe_root:

./examples/mnist/train_lenet.sh 2> examples/mnist/name_of_your_file.log.txt

 

 

This will run the caffe and write the log in the file examples/mnist/name_of_your_file.log.txt. To see your logs, open another SSH in another tab:

cd caffe-master/
tail -f examples/mnist/name_of_your_file.log.txt

 

When your job is done, press Ctrl+C to exit tail output and then you can:

cd examples/mnist/
../../tools/extra/parse_log.sh name_of_your_file.log.txt

This will generate a readable format of name_of_your_file.log.txt.test and name_of_your_file.log.txt.train. You can use scp to copy these files into your local machine and use Matlab/octav/python etc. to plot your training and test.
If by accident you logged off the machine, you can see your job list (something like taskmanager in windows) by using command:
top

if you want to call your job, use commmand:
kill -9 <PID>
You can change solver prototxt to change your network architecture, parameters etc. We will go over it in the class.

Find more information about training mnist in caffe:

http://caffe.berkeleyvision.org/gathered/examples/mnist.html (Links to an external site.)