OpenCV3 + Python + Mac

OpenCV3 + Python + Mac

OpenCV is an open-source library for computer vision that we have found useful in bringing many applications to production settings.  It is written in C++, but now also supports Python bindings, which can make rapid prototyping easier. Below we summarize our procedure for installing OpenCV3 on Mac OS X, with Python bindings and virtual environment support. The information can be found on many webpages, but was difficult to find all in one place. All of the following commands should be executed in the Terminal.

First install the Homebrew package manager if you have not already:
ruby -e "$(curl -fsSL"

Install OpenCV3:
brew tap homebrew/science
brew install opencv3 --with-contrib
echo /usr/local/opt/opencv3/lib/python2.7/site-packages >> /usr/local/lib/python2.7/site-packages/opencv3.pth

At this point, you can test python bindings as follows:
python -c "import cv2; print cv2.__version__"
and the output should be 3.x.x.

Lastly, some more operations are needed to allow these Python bindings to work inside a virtual environment. To manage virtual environments on Macs, we recommend using virtualenvwrapper, which can be installed as follows:
pip install virtualenv
pip install virtualenvwrapper
echo 'source "/usr/bin/"' >> ~/.bash_profile
source "/usr/bin/"

Now, supposing the desired virtual environment is named “cv”,
mkvirtualenv cv
cp /usr/local/opt/opencv3/lib/python2.7/site-packages/* ~/.virtualenvs/cv/lib/python2.7/site-packages/

Lastly, test that the Python bindings work inside the virtual environment: