Machine Learning Part 4: Setting Up Python Environment for Computer Vision and Machine Learning

5 minute read

So, I am here with you again, in the new post of Machine Learning tutorial series. Next time, I told you about Linear Regression and promised to talk further more in the next post. And I am also gonna ask you to do some coding, remember? But before doing that, we need more than our hands. If you are gonna have a show, then you must have the stage prepared, right? Similarly, if you want to write some lines of code, then you need to prepare the developing environment. Today, I will show you how to do that.

In this post, I am assuming that you are using a computer running on Linux based OS (like Ubuntu, Red Hat, ect). For ones who are sticking to Windows, I am so sorry. I will write a post for Windows users soon.

We will install OpenCV for Computer Vision, and Keras (Theano backend) for Machine Learning. I will make it short and clear, just to make sure that you will have a working environment, without spending so much time on struggling with unknown errors.


Firstly, open your Terminal, and type the lines below to Update and Upgrade the packages:

sudo apt-get update
sudo apt-get upgrade

From now on, just hit y when prompted. Next, we will install the necessary packages for OpenCV:

sudo apt-get install build-essential cmake git pkg-config libjpeg8-dev \
libjasper-dev libpng12-dev libgtk2.0-dev \
libavcodec-dev libavformat-dev libswscale-dev libv4l-dev gfortran
sudo apt-get install libtiff5-dev 

Next, we will need library to optimizing purpose (there will be a lot of simultaneous computation in Computer Vision and Machine Learning which mainly bottleneck the performance). We will use ATLAS (among many other BLAS implementation):

sudo apt-get install libatlas-base-dev

After that, we will install pip, a tool used for installing Python packages by PyPA:

sudo python

In the next step, we will need to install virtualenv and virtualenvwrapper, which are tools for isolating Python environments. You may ask why we have to do that. Imagine you are using Python for many purposes (like game developing, computer vision, machine learning, etc). Because each requires different configuration, it will be a good idea to work seperately to avoid confliction.

sudo pip install virtualenv virtualenvwrapper

To make virtualenv work, we will have to apply these lines to our ~/.bashrc by typing sudo vim ~/.bashrc (I prefer Vim, you can use whatever you want). Then add these lines to the end of the file:

export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/

Now, let’s create our virtual environment for installing OpenCV

source ~/.bashrc
mkvirtualenv opencv

From this step, make sure you are in opencv environment (you can know by seeing if there is (opencv) before the $ mark).

Next we need to install python development tools package:

sudo apt-get install python2.7-dev

Then we will install numpy, a very powerful module for dealing with array computation:

pip install numpy

Note that you can not add sudo all the time. Because sudo means that you are execute as superuser, it will install directly into your system environment, not the virtual environment!

Next, we will download OpenCV, then checkout to 3.0.0 branch:

cd ~
git clone
cd opencv
git checkout 3.0.0
cd ~
git clone
cd opencv_contrib
git checkout 3.0.0

Now we got everything ready to be installed, let’s compile and install OpenCV:

cd ~/opencv
mkdir build
cd build
	-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
sudo make install
sudo ldconfig

In the last step, we only need to link the installed OpenCV to our virtual environment:

cd ~/.virtualenvs/cv/lib/python2.7/site-packages/
ln -s /usr/local/lib/python2.7/site-packages/

Now let’s test if OpenCV was installed successfully (make sure that you are still standing in opencv virtual environment):

>>> import cv2
>>> cv2.__version__

If you see the output like above, then Congratulations! You have successfully installed OpenCV!


Now let’s move on to install Keras. Actually we will almost use the scikit-learn library, not Keras in the near future, but Keras is not so hard to install, so I decided to add it here so that I won’t have to make another post for Keras.

As I mentioned earlier, you may not want to mess up your environments. So let’s create another virtual enviroment for Keras.

# Exit the current virtual environment
mkvirtualenv keras

Now let’s install necessary packages for Keras:

pip install numpy scipy scikit-learn pillow h5py

Keras actually works on top of Theano and Tensorflow (if you don’t give a damn about what they are, then that’s just fine!). Suppose that you are using Keras with Theano backend, so you will have to install Theano first:

pip install --upgrade --no-deps git+git://

Wait until the Theano installation completes, then we can install keras:

pip install keras

We have just finished our Keras installation. If you want to use OpenCV in keras virtual environment, you can link it the way we did above:

cd ~/.virtualenvs/keras/lib/python2.7/site-packages/
ln -s /usr/local/lib/python2.7/site-packages/

Now let’s test our installation. Again, make sure that you are still in keras enviroment.

>>> import keras
Using Theano backend.

If you had the exact output as above, then Congratulations again! Wait… what if you didn’t, but received Using TensorFlow backend error instead. Well, sometimes Keras fails to choose the right backend, and you must do it manually. But don’t worry, it should be just a piece of cake!

All you have to do is modify the file ~/.keras/keras.json, replace ‘TensorFlow’ with ‘Theano’, then try to test it again. It should work just fine!

So now you have installed all you need for learning Computer Vision and Machine Learning. Great job guys! It was not as hard as you imagined right?

So now you have swords and shields. We are ready to be on the field. I know you guys don’t want to wait no more. So I will post the next post soon, I promise. Before that, make sure you revise all the stuff about Linear Regression. And I will be back. See you!

Leave a Comment