Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling OpenCV3 with pyenv using python 3.5.0 on OSX

Took me a while to figure this out so I thought I'd write it down.

like image 932
Wesley Avatar asked Sep 25 '26 09:09

Wesley


1 Answers

Install python with shared library support through pyenv:

env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.5.0

Make a new virtualenv named opencv

pyenv virtualenv 3.5.0 opencv

Activate the virtualenv and install numpy

pyenv activate opencv
pyenv rehash
pip install numpy

Clone opencv and opencv_contrib and put it in ~/

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

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

cd ~/opencv
mkdir build
cd build

Run CMake and install

PREFIX_MAIN=`pyenv virtualenv-prefix`
PREFIX=`pyenv prefix`
cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX="$PREFIX" \
    -D PYTHON3_EXECUTABLE="$PREFIX"/bin/python3.5 \
    -D PYTHON3_PACKAGES_PATH="$PREFIX"/lib/python3.5/site-packages \
    -D PYTHON3_LIBRARY="$PREFIX_MAIN"/lib/libpython3.5m.dylib \
    -D PYTHON3_INCLUDE_DIR="$PREFIX_MAIN"/include/python3.5m \
    -D PYTHON3_NUMPY_INCLUDE_DIRS="$PREFIX"/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 INSTALL_NAME_DIR=${CMAKE_INSTALL_PREFIX}/lib \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules ..

make -j8

make install

Note: if you are using python 3.5.2 you need to change

PYTHON3_EXECUTABLE="$PREFIX"/bin/python3
PYTHON3_LIBRARY="$PREFIX_MAIN"/lib/libpython3.5m.a

source: basically mashed these guides together:

  • http://www.pyimagesearch.com/2015/06/29/install-opencv-3-0-and-python-3-4-on-osx/
  • https://stackoverflow.com/a/24112175/1591757
  • http://answers.opencv.org/question/4134/cmake-install_name_tool-absolute-path-for-library-on-mac-osx/
like image 86
Wesley Avatar answered Sep 27 '26 02:09

Wesley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!