Setting up OpenCV for Python 3 on macOS Sierra with 5 easy steps

Vladimir Sinitcin
2 min readOct 6, 2016

--

At SuitApp we’re making a couple of things related with computer vision and of course we’re using OpenCV for that tasks. Earlier I had to make a lot of efforts to setup OpenCV with mine Python 3.5: Download sources, configure, find solutions to make it compile well… And after upgrading to macOS Sierra I had to make this setup again, but fortunately I found more convenient way to do it.

But no worries, I will share shortcut with you.

We need homebrew, virtualenv, some additional formulas and a bit of console.

Step 1 (if you have no homebrew)

Installing homebrew by running command in console:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Step 2 (optional)

This step is optional if you already have python3

brew install python3

Step 3

Obtaining formulas for science

brew tap homebrew/science

Step 4

Installing opencv3 with all options we need

brew install opencv3 --with-contrib --with-python3

Those options with-contrib and with-python3 just for my setup. Here is a list of available ones which may be useful for you.

--32-bit
Build 32-bit only
--c++11
Build using C++11 mode
--with-contrib
Build "extra" contributed modules
--with-cuda
Build with CUDA support
--with-ffmpeg
Build with ffmpeg support
--with-gphoto2
Build with gphoto2 support
--with-gstreamer
Build with gstreamer support
--with-jasper
Build with jasper support
--with-java
Build with Java support
--with-libdc1394
Build with libdc1394 support
--with-opengl
Build with OpenGL support (must use --with-qt5)
--with-openni
Build with openni support
--with-openni2
Build with openni2 support
--with-python3
Build with python3 support
--with-qt
Build the Qt4 backend to HighGUI
--with-qt5
Build the Qt5 backend to HighGUI
--with-quicktime
Use QuickTime for Video I/O instead of QTKit
--with-tbb
Enable parallel code in OpenCV using Intel TBB
--without-eigen
Build without eigen support
--without-numpy
Use a numpy you've installed yourself instead of a Homebrew-packaged numpy
--without-opencl
Disable GPU code in OpenCV using OpenCL
--without-openexr
Build without openexr support
--without-python
Build without Python support
--without-tests
Build without accuracy & performance tests
--HEAD
Install HEAD version

Step 5

All that we have left to do now is to add opencv3 to our virtualenv (if we’ve created one).

We need to rename compiled cv2 module to appropriate one.

By locating console to opencv dir:

cd /usr/local/opt/opencv3/lib/python3.5/site-packagesmv cv2.cpython-35m-darwin.so ./cv2.so

Then we’re just adding path to this lib to our virtualenv

echo '/usr/local/opt/opencv3/lib/python3.5/site-packages' >> ~/.virtualenvs/suitapp/lib/python3.5/site-packages/opencv3.pth

Where ~/.virtualenvs/suitapp/lib/python3.5/site-packages/ is path to your own virtualenv.

And now we can activate our virtualenv and try to import cv2 to check that everything is working.

workon suitapp
python -c "import cv2"

If there is no error, my congratulations — we did it!

P.S. Feel free to fix my English, or any other mistakes.

--

--

Vladimir Sinitcin

CTO at Atomic Creative, volunteer in heart and engineer in mind