Skip to content

Instantly share code, notes, and snippets.

@keunhong
Last active September 23, 2021 18:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keunhong/fcd58db8414aba0ce798dfb56f1fdb0e to your computer and use it in GitHub Desktop.
Save keunhong/fcd58db8414aba0ce798dfb56f1fdb0e to your computer and use it in GitHub Desktop.

Get the prefix for the virtualenv directory and the main python installation. Note that the pyenv commands are somewhat misleading.

PREFIX_MAIN=`pyenv virtualenv-prefix`
PREFIX_VENV=`pyenv prefix`

Download opencv and opencv_contrib into a clean directory. Make a separate build directory in same directory as opencv and opencv_contrib and enter it.

mkdir build
cd build

Run cmake.

cmake -D CMAKE_BUILD_TYPE=RELEASE \
  -D CMAKE_CXX_COMPILER=g++-5 \
  -D CMAKE_C_COMPILER=gcc-5 \
  -D CMAKE_INSTALL_PREFIX="$PREFIX_VENV" \
  -D PYTHON3_EXECUTABLE="$PREFIX_VENV"/bin/python3 \
  -D PYTHON_INCLUDE_DIR="$PREFIX_MAIN"/include/python3.6m/ \
  -D PYTHON_INCLUDE_DIR2="$PREFIX_MAIN"/include/python3.6m/ \
  -D PYTHON3_PACKAGES_PATH="$PREFIX_VENV"/lib/python3.6/site-packages/ \
  -D PYTHON3_LIBRARY="$PREFIX_MAIN"/lib/libpython3.6m.so \
  -D PYTHON3_NUMPY_INCLUDE_DIRS="$PREFIX_VENV"/lib/python3.6/site-packages/numpy/core/include \
  -D BUILD_opencv_python3=ON \
  -D BUILD_opencv_legacy=OFF  \
  -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules \
  ../opencv

Check the ouput and make sure python3 is properly enabled. Then build and install

make
make install

You do NOT need to run make install as root since we are installing to a virtualenv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment