May 6, 2015 · Computer Vision

OpenCV and Python Project Setup

For my computer vision projects I like to setup an organized directory structure and keep all dependencies in one place. virtualenv is a great tool, which helps you accomplish this. I'll show you how I structure my projects, install OpenCV and install packages.

For my projects I use Python 2.7 and OpenCV 2.4.11 on Mac OS X. Many of these steps should be similar for other operating systems, except you will use a tool other than Homebrew to manage packages.

Install Python and OpenCV

If you haven't done this yet, you will want to install Python and OpenCV. I used Homebrew to do this, which is a package manager for OS X. I highly recommend installing Homebrew if you haven't done so using these instructions.

OS X comes with Python preinstalled, however, I occasionally had issues with stock Python, so I reinstalled it using Homebrew. On the command line type:

brew install python  

Next you want to install OpenCV. Before installing OpenCV, I found you have to install numpy system-wide.

pip install numpy  
brew tap homebrew/science  
brew install opencv  

OpenCV should now work with Python. You can test this out by opening a Python console by typing python on the command line and then running import cv2. If that works fine, you're ready to use OpenCV.

Set up virtualenv

virtualenv is a great tool for Python that manages project dependencies. Different versions of Python packages could potentially cause problems for your application. With virtualenv you can specify all the packages a project needs and which versions to use. They're isolated from system-wide packages and ones from other projects. As I'll show you later, it also helps make an application portable. To install virtualenv on the command line type:

pip install virtualenv  

Whenever you want to set up a virtualenv inside a project directory you run:

virtualenv --no-site-packages venv  

The environment is created inside the venv directory. To activate the environment type source venv/bin/activate and to deactivate type deactivate.

Having to source the virtual environment can get annoying really quickly, so I also use a tool called autoenv, which does this automatically. Install autoenv:

brew install autoenv  
echo 'source /usr/local/opt/autoenv/activate.sh' >> ~/.bash_profile  

autoenv requires a .env file in the project directory. I create one using this example:

BASE_PATH=`dirname "${BASH_SOURCE}"`  
PWD=`pwd`

if [[ "${BASE_PATH}" == "${PWD}" ]]  
then  
    if [[ -e venv/bin/activate ]]
    then
        source venv/bin/activate
    fi
fi  

Install packages

To install packages for the virtual environment just run pip install <package>. Make sure the virtual environment is activated before running any pip commands. For an OpenCV project you'll need to install numpy. I would also recommend installing pytest to run tests.

To use OpenCV in a virtual environment you also need to copy OpenCV site-package files to the virtualenv site-package directory. You should be able to do this with:

cp /usr/local/lib/python2.7/site-packages/cv* ./venv/lib/python2.7/site-packages  

If this doesn't work you can find the location of the OpenCV files like this:

import cv2  
print cv2.__file__  

After you've finished installing packages you should generate a requirements file that lists the project's dependencies. In the project directory type:

pip freeze > requirements.txt  

Now whenever you set up this application on another system you only need to run this command to install all project dependencies:

pip install -r requirements.txt  

Project skeleton

I follow this guide to structure my project directory. There's a directory for all application files. There's a directory for test files. You also want a setup.py and README.md file.

If you're using git (which I highly recommend) you should have a .gitignore file. I use this one and to the end I add:

# virtualenv
venv/  
.env

Create a new project

Up to this point you've installed Python, OpenCV and virtualenv. You've learned how to create a virtual environment, install packages and structure a project directory. To summarize, this is how I set up a new OpenCV project, assuming I have all the requirements installed:

mkdir new_project  
cd new_project  
mkdir project_name test  
touch setup.py README.md project_name/__init__.py test/__init__.py  
touch .gitignore  

Then I copy the previous example into .gitignore. The project directory is set up, so now set up virtualenv.

virtualenv --no-site-packages venv  
touch .env  

I copy the example into .env. Now you can install packages.

source venv/bin/activate  
pip install numpy  
pip install pytest  
cp /usr/local/lib/python2.7/site-packages/cv* ./venv/lib/python2.7/site-packages  
pip freeze > requirements.txt  

Next commit the project to git and now you're ready to start coding your OpenCV project! This project should also be easily portable to other systems.

Comments powered by Disqus