Don’t judge me, I know how this title sounds. The harsh reality is — as per writing of this post — that I always have a hard time getting CMake to recognize the right Python path when I’m using an Anaconda environment. In theory it’s just
git clone https://github.com/opencv/opencv.git opencv mkdir opencv/build && cd opencv/build cmake -DCMAKE_INSTALL_PREFIX=/usr/local .. make -j8 (sudo) make install
and there you go. It will happily pick up your environment’s Python installation and build that for you. Of course, it looks more along the lines of
cmake \ -DCMAKE_INSTALL_PREFIX="/usr/local" \ -DOPENCV_EXTRA_MODULES_PATH="../opencv_contrib/modules" \ -DBUILD_DOCS=OFF \ -DBUILD_TESTS=OFF \ -DBUILD_EXAMPLES=OFF \ -DBUILD_PERF_TESTS=OFF \ -DBUILD_opencv_dnn=OFF \ -DENABLE_FAST_MATH=ON \ -DWITH_OPENMP=ON \ -DWITH_TBB=ON \ -DMKL_WITH_TBB=ON \ -DMKL_WITH_OPENMP=ON \ -DCMAKE_CXX_COMPILER="/usr/bin/g++-5" \ -DCMAKE_C_COMPILER="/usr/bin/gcc-5" \ -DCUDA_HOST_COMPILER="/usr/bin/gcc-5" \ -DCUDA_FAST_MATH=ON \ -DCUDA_ARCH_BIN="5.2" \ -DWITH_CUBLAS=ON \ ..
Whenever you then add
conda activate my-environment
to the mix though, all hell breaks loose and you end up with partially configured Python 2 and no Python 3 support at all. The trick seems to be not to rely on OpenCV’s standard Python configuration values
PYTHON3_LIBRARY PYTHON3_EXECUTABLE PYTHON3_INCLUDE_DIR PYTHON3_INCLUDE_DIR2 PYTHON3_NUMPY_INCLUDE_DIRS
but rather to use the seemingly undocumented values
PYTHON3_LIBRARIES PYTHON3_INCLUDE_PATH
as well, giving it the nice appearance of
cmake \ -DCMAKE_BUILD_TYPE=RELEASE \ -DCMAKE_INSTALL_PREFIX="/your/anaconda3" \ -DOPENCV_EXTRA_MODULES_PATH="../opencv_contrib/modules" \ -DBUILD_DOCS=OFF \ -DBUILD_TESTS=OFF \ -DBUILD_EXAMPLES=OFF \ -DBUILD_PERF_TESTS=OFF \ -DBUILD_opencv_dnn=ON \ -DTINYDNN_USE_NNPACK=OFF \ -DTINYDNN_USE_TBB=ON \ -DTINYDNN_USE_OMP=ON \ -DENABLE_FAST_MATH=ON \ -DWITH_OPENMP=ON \ -DWITH_TBB=ON \ -DMKL_WITH_TBB=ON \ -DMKL_WITH_OPENMP=ON \ -DCMAKE_CXX_COMPILER="/usr/bin/g++-5" \ -DCMAKE_C_COMPILER="/usr/bin/gcc-5" \ -DCUDA_HOST_COMPILER="/usr/bin/gcc-5" \ -DCUDA_FAST_MATH=ON \ -DCUDA_ARCH_BIN="5.2" \ -DWITH_CUBLAS=ON \ -DBUILD_opencv_python2=OFF \ -DPYTHON_EXECUTABLE="/your/anaconda3/bin/python3" \ -DPYTHON_LIBRARY="/your/anaconda3/lib/libpython3.6m.so" \ -DPYTHON3_LIBRARY="/your/anaconda3/lib/libpython3.6m.so" \ -DPYTHON3_EXECUTABLE="/your/anaconda3/bin/python3" \ -DPYTHON3_INCLUDE_DIR="/your/anaconda3/include/python3.6m" \ -DPYTHON3_INCLUDE_DIR2="/your/anaconda3/include" \ -DPYTHON3_NUMPY_INCLUDE_DIRS="/your/anaconda3/lib/python3.6/site-packages/numpy/core/include" \ -DPYTHON3_INCLUDE_PATH="/your/anaconda3/include/python3.6m" \ -DPYTHON3_LIBRARIES="/your/anaconda3/lib/libpython3.6m.so" \ -DHDF5_C_LIBRARY_z="/your/anaconda3/lib/libz.so" \ ..
And then it’ll do what you want it to do. Because I’m tired of retrying everytime, on github.com/sunsided/opencv-cmake is my repo with support for the OpenCV Extras Module and some documentation for this.
So, what paths go in there? Well, this blog post has a very flashy way of finding out.
For
python3 -c "import sys; print(sys.prefix)"
so it will install OpenCV directly into your Anaconda installation.
For
which python3
and
python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())"
Finally, a
python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"
which you a can use to determine the