Install Ubuntu 12.04 Python 2.7 OpenCV 3 on Google Compute Engine

Here are a list of steps to get you started on a Google Compute Engine with an instance of Ubuntu and to install Python and OpenCV for doing image processing on the cloud.

GCE VM Instance

  • sign up for Gmail
  • goto and select “Billing” from the left hand menu, click on “New billing account” and add the required details.
  • select “Projects” from the left hand menu and click on “Create Project” and then enter PROJECT NAME and PROJECT ID.
    • you can create multiple projects. multiple VM Instances can be created within a project. applications within a VM Instance can have access to other applications in the same instance or on other instances within the same project.
  • on the left hand side of the screen is the “main menu”. the Overview section would have come up and a number of suggestions would be visible in the “Project Dashboard” to get you started.
    • from the main menu select: APIs & Auth > APIs
      • have a look at the APIs included by default. make sure that no unnecessary ones are “On”
    • from the main menu select: Compute > Compute Engine > VM Instances
      • click on the blue button “Create an instance”. at later stages there will be button “New Instance”
        • give it a name,
        • choose FIREWALL options,
        • choose ZONE
        • choose MACHINE TYPE
        • choose IMAGE SOURCE as “New disk from image”
        • choose IMAGE as Ubuntu 12.04 LTS
        • choose BOOT DISK TYPE
          • unchecking “Delete boot disk when instance is deleted” will allow you to use this disk type at a later stage with a different VM Instance where the option for IMAGE SOURCE can be chosen as “Existing Disk”
        • choose EXTERNAL IP
        • click on “Create”
      • the VM Instances will appear as a list and some usage data is provided in a graph.
        • click on “SSH” which is an option on the right most column of the list of instances. this will create a secure connection to the particular instance. it will open the “terminal” in a browser window. by clicking on the “vertical dots” beside the word “SSH” there are options for using a different means to connect with the instance.
    • from the main menu select: APIs & Auth > APIs
      • have a look at the APIs included, at this stage. make sure that no unnecessary ones are “On”

From here onwards it is assumed that the user is at the command prompt of the terminal and is connected as user@project

On Linux like systems

OpenCV is a library that needs to be built from its binaries. This can be a challenge to handle correctly with the required dependencies. The bellow instructions can be useful for any such library. Run the following commands and whenever prompted to continue, type the option Y and press enter to proceed.

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential checkinstall cmake cmake-curses-gui unzip

Before installing a package like OpenCV it is worthwhile to create an isolated environment using Virualenv. Installing it is easy via PIP. however, to get PIP working properly we need setuptools which, is installed via the following command

wget -O - | sudo python

and then we get required python 2.7 bindings, PIP and virtualenv (these things are not guaranteed to work with python 3.x so for now lets continue with python 2.7.x)

sudo apt-get install python-dev python-pip python-vertualenv

Now we use pip to install virtualenvwrapper

sudo pip install virtualenvwrapper

Use nano ~/.bashrc to edit bashrc and add the following lines at the bottom of the file

# For using different python interpreters and packages
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/

then source it

source ~/.bashrc

Installing OpenCV

This does not work on Ubuntu 14.04 nor 14.10

We need these before installing OpenCV:

sudo apt-get -q install libopencv-dev python-numpy pkg-config yasm libtiff4-dev libjpeg-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev libtbb-dev libqt4-dev libgtk2.0-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils ffmpeg

New releases of numpy are less frequent compared to that of OpenCV but projects dependent on an old version of numpy may be valuable at some point while other projects may need a newer version. So the plan is to create a virtualenv which has some basic libraries and install OpenCV in another environment with a link to the basic libraries. A new version of OpenCV may still require an older version of numpy so environment isolation is the only sane way to deal with such potential struggles. Thereafter create app projects in separate environments that can utilize various versions of OpenCV whenever required.

mkvirtualenv baselibs
pip install numpy

Note that sudo was not used in the above line of code.

Now we deactivate that environment and install OpenCV3 Beta in a new one, while adding a link to the python sitepackages of baselibs.

mkvirtualenv cv3beta
mkdir tarsAndZips
cd tarsAndZips
mkdir build
cd build

Now use cmake curses gui to inspect and change configurable values of the source file

ccmake ..

press c, bunch of options show up, press t, more options show up, use arrow keys to navigate and press enter to edit the field. Change the following to the shown values

BUILD_opencv_python2  ON
(make sure)
CMAKE_INSTALL_PREFIX  ~/.virtualenvs/cv3beta/local
PYTHON2_INCLUDE_DIR  ~/.virtualenvs/cv3beta/include/python2.7
PYTHON2_NUMPY_INCLUDE_DIRS  ~/.virtualenvs/baselibs/lib/python2.7/site-packages/numpy/core/include
(make sure)
PYTHON2_PACKAGES_PATH  ~/.virtualenvs/cv3beta/lib/python2.7/site-packages

Now, press c. It should find value for TBB_STDDEF_PATH. Press c again and then press g.

Proceed to make and then install

sudo make install

Config the install

sudo sh -c 'echo "~/.virtualenvs/cv3beta/local/lib" > /etc/'
sudo ldconfig

Thus, python binding for opencv3-beta is ready to use within a virtualenv running in a cloud instance of Ubuntu 12.04 LTS.

Final Note:

Upon trying to import cv2 this error shows up:

libdc1394 error: Failed to initialize libdc1394

It is because the built cv2 is trying to look for camera hardware on the server and since there is none it throws that warning. The module is successfully imported and the warning can be ignored.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s