3

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

1 Answer 1

9

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:

Sign up to request clarification or add additional context in comments.

5 Comments

I needed to add -D BUILD_SHARED_LIBS=OFF because the cv2.so binary it made couldn't find my dylibs(?), and I also followed the update on your first site saying that OpenCV 3.1.0 is broken unless you switch to -D INSTALL_C_EXAMPLES=OFF (didn't bother trying with it ON)
I got this error with I run make - j8: fatal error: 'opencv2/core/hal/intrin.hpp' file not found . Any idea how to resolve this?
@NickT Thanks, I've updated the answer for 3.1.0. Not sure what the deal is with BUILD_SHARED_LIBS though.
@sooon Try checking out 3.1.0 for both repos and building again.
I'd use just a single prefix of the virtual environment. Mixing different Pythons calls for problems... Also, it's enough to set the CMake flag -DPython3_ROOT_DIR="$(pyenv prefix)", it will set the other flags accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.