Skip to content

Instantly share code, notes, and snippets.

@fpdevil
Last active June 2, 2016 05:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fpdevil/a29d53d00679041ff36b8ee2aac94420 to your computer and use it in GitHub Desktop.
Save fpdevil/a29d53d00679041ff36b8ee2aac94420 to your computer and use it in GitHub Desktop.
Mac OS X OpenCV3 + Python 3.X

OpenCV3 with Python3.5 on Mac OS X Yosemite

Installation of the dependencies

  • First install the python3 using homebrew
$ uname -a
Darwin apple 15.5.0 Darwin Kernel Version 15.5.0: Tue Apr 19 18:36:36 PDT 2016; root:xnu-3248.50.21~8/RELEASE_X86_64 x86_64

$ brew install python3
$ brew linkapps
$ brew install cmake pkg-config
$ brew install jpeg libpng libtiff openexr
$ brew install eigen tbb

Set the Path Variables

export PATH=/usr/local/bin:$PATH
export PYTHONPATH=/usr/local/lib/python3.5/site-packages

Check the Python 3 version and path

$ which python3
/usr/local/bin/python3

Install the NumPy Package using the Python package manager

$ pip3 install numpy

Check the python system variables

$ python3 -m sysconfig
$ python3
>>> import numpy as np
>>> np.get_include()
'/usr/local/lib/python3.5/site-packages/numpy/core/include'

Clone the opencv and opencv_contrib packages from github

$ git clone https://github.com/Itseez/opencv.git
$ cd opencv
$ git checkout 3.1.0

$ git clone https://github.com/Itseez/opencv_contrib
$ cd opencv_contrib
$ git checkout 3.1.0

Create the build directory

$ cd opencv
$ mkdir build
$ cd build

Using cmake to configure

  • Adjust the Python3 Paths and set INSTALL_C_EXAMPLES=OFF
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
        -D CMAKE_INSTALL_PREFIX=/usr/local \
        -D PYTHON3_EXECUTABLE=/usr/local/bin/python3 \
        -D PYTHON3_PACKAGES_PATH=/usr/local/lib/python3.5/site-packages \
        -D PYTHON3_LIBRARY=/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/libpython3.5m.dylib \
        -D PYTHON3_INCLUDE_DIR=/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/include/python3.5m \
        -D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/local/lib/python3.5/site-packages/numpy/core/include \
        -D INSTALL_C_EXAMPLES=OFF \
        -D INSTALL_PYTHON_EXAMPLES=ON \
        -D BUILD_EXAMPLES=ON \
        -D BUILD_opencv_python3=ON \
        -D OPENCV_EXTRA_MODULES_PATH=/opt/software/opencv_contrib/modules ..

If there are no errors, run the make

$ make -j4

Install the library

$ make install

Check the OpenCV Python shared object

> ll /usr/local/lib/python3.5/site-packages/cv2*
-rwxr-xr-x  /usr/local/lib/python3.5/site-packages/cv2.cpython-35m-darwin.so

> ll lib/python3/
-rwxr-xr-x  cv2.cpython-35m-darwin.so

# Check the version
$ python3
Python 3.5.1 (default, Feb 21 2016, 11:54:27)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.1.0'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment