Skip to content

macOS OpenCV

Michael Hulse edited this page Feb 14, 2017 · 4 revisions

TL;DR from Install OpenCV 3 on macOS with Homebrew (the easy way)

# Install Python 2.7 and 3.x:
$ brew install python python3

# Add the homebrew/science repository to the set of packages we are tracking:
$ brew tap homebrew/science

# Installing OpenCV 3 with Python 3 bindings via Homebrew:
$ brew install opencv3 --with-contrib --with-python3 --HEAD

# Tell Homebrew’s Python 2.7 install to look for additional packages in /usr/local/opt/opencv3/lib/python2.7/site-packages
$ echo /usr/local/opt/opencv3/lib/python2.7/site-packages >> /usr/local/lib/python2.7/site-packages/opencv3.pth

# Rename bindings:
$ cd /usr/local/opt/opencv3/lib/python3.6/site-packages/
$ mv cv2.cpython-36m-darwin.so cv2.so
$ cd ~

# Create another .pth  file, this time for the Python 3 + OpenCV 3 install:
$ echo /usr/local/opt/opencv3/lib/python3.6/site-packages >> /usr/local/lib/python3.6/site-packages/opencv3.pth

Note: The above commands will fail if you don’t verify the exact Python version number.

Validate that OpenCV 3 with Python 2.7 bindings are working:

$ python
Python 3.6.0 (default, Dec 24 2016, 08:01:42)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
readline: ~/.inputrc: line 48: -o: unknown variable name
readline: ~/.inputrc: line 48: -o: unknown variable name
>>> import cv2
>>> cv2.__version__
'3.2.0-dev'
>>> 

Same with Python 3.x:

$ python3
Python 3.6.0 (default, Dec 24 2016, 08:01:42)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
readline: ~/.inputrc: line 48: -o: unknown variable name
readline: ~/.inputrc: line 48: -o: unknown variable name
>>> import cv2
>>> cv2.__version__
'3.2.0-dev'
>>> 

Read the article above to set virtual environments.

Clone this wiki locally