Saturday, January 28, 2017

Install opencv3 with CUDA support on a mac

Install from source (recommended):

sudo xcode-select --install # if not done already
brew tap homebrew/science
brew install cmake pkg-config jpeg libpng libtiff openexr eigen tbb
cd ~/CppProjects/
git clone --depth 1 https://github.com/opencv/opencv
git clone --depth 1 https://github.com/opencv/opencv_contrib

cd /Users/kaiyin/anaconda3/envs/tensorflow/lib/
ln -s libpython3.5m.dylib libpython3.5.dylib 

# install for python 3.5
# tensorflow is an anaconda python3.5 environment on my machine created for tensorflow
export ENV_TENSORFLOW=/Users/kaiyin/anaconda3/envs/tensorflow
export PREFIX=/opt/local
export PY_DYLIB="$ENV_TENSORFLOW/lib/libpython3.5.dylib"
export OPENCV_CONTRIB=~/CppProjects/opencv_contrib/modules
export PY_INCLUDE="$ENV_TENSORFLOW/include/python3.5m"
export PY_BINARY="$ENV_TENSORFLOW/bin/python3.5"
cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=$PREFIX \
    -D OPENCV_EXTRA_MODULES_PATH="$OPENCV_CONTRIB" \
    -D PYTHON3_LIBRARY="$PY_DYLIB" \
    -D PYTHON3_INCLUDE_DIR="$PY_INCLUDE" \
    -D PYTHON3_EXECUTABLE="$PY_BINARY" \
    -D BUILD_opencv_python2=OFF \
    -D BUILD_opencv_python3=ON \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D INSTALL_C_EXAMPLES=OFF \
    -D BUILD_EXAMPLES=ON ..

make -j8 # use 8 jobs for compiling
sudo make install
cp $PREFIX/lib/python3.5/site-packages/cv2.cpython-35m-darwin.so  $ENV_TENSORFLOW/lib/python3.5/site-packages


# install for python 2.7
# tf27 is an anaconda python2.7 environment on my machine created for tensorflow
export ENV_TENSORFLOW=/Users/kaiyin/anaconda3/envs/tf27
export PREFIX=/opt/local
export PY_DYLIB="$ENV_TENSORFLOW/lib/libpython2.7.dylib"
export OPENCV_CONTRIB=~/CppProjects/opencv_contrib/modules
export PY_INCLUDE="$ENV_TENSORFLOW/include/python2.7"
export PY_BINARY="$ENV_TENSORFLOW/bin/python2.7"
cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=$PREFIX \
    -D OPENCV_EXTRA_MODULES_PATH="$OPENCV_CONTRIB" \
    -D PYTHON2_LIBRARY="$PY_DYLIB" \
    -D PYTHON2_INCLUDE_DIR="$PY_INCLUDE" \
    -D PYTHON2_EXECUTABLE="$PY_BINARY" \
    -D BUILD_opencv_python2=ON \
    -D BUILD_opencv_python3=OFF \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D INSTALL_C_EXAMPLES=OFF \
    -D BUILD_EXAMPLES=ON ..


make -j8 # use 8 jobs for compiling
sudo make install
cp $PREFIX/lib/python2.7/site-packages/cv2.so $ENV_TENSORFLOW/lib/python2.7/site-packages/

Verify your installation in python 2.7:

# source activate tf27
(tf27) kaiyin@kaiyins-mbp 21:11:12 | /opt/local/lib/python3.5/site-packages =>
ipython
Python 2.7.13 |Continuum Analytics, Inc.| (default, Dec 20 2016, 23:05:08)
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

IPython profile: kaiyin

In [1]: import cv2
c
In [2]: cv2.__version__
Out[2]: '3.2.0-dev'

Verify your installation in python 3.5:

# source activate tensorflow
(tensorflow) kaiyin@kaiyins-mbp 21:13:13 | /opt/local/lib/python3.5/site-packages =>
ipython
Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:52:12)
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

IPython profile: kaiyin

In [1]: import cv2; cv2.__version__
Out[1]: '3.2.0-dev'

1 comments:

Unknown said...
This comment has been removed by the author.