As of December 2018, I was able to compile the latest OpenCV version in my Ubuntu16.04 machine with CUDA, FFMpeg and TIFF (useful to work with Caffe too), and to run it in both Python2.7 and 3.5. The build was highly inspired in this post, so kudos to them!
Clone the latest repos:
Note that both have to be in the same release otherwise cmake will complain. Make sure to clone both at the same time:
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
Configure and build:
First, create the standard build environment:
cd opencv
mkdir build
cd build
Then, the following cmake command worked for me:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=<LOCATION_OF_YOUR_OPENCV_CONTRIB>/modules \
-D BUILD_EXAMPLES=ON \
-D BUILD_opencv_python2=ON \
-D WITH_FFMPEG=1 \
-D WITH_TIFF=ON \
-D WITH_CUDA=ON \
-D CUDA_GENERATION=Pascal \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D WITH_LAPACK=OFF \
-D PYTHON2_EXECUTABLE=/usr/bin/python \
-D PYTHON2_INCLUDE_DIR=/usr/include/python2.7 \
-D PYTHON2_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython2.7.so \
-D PYTHON2_NUMPY_INCLUDE_DIRS=/usr/local/lib/python2.7/dist-packages/numpy/core/include \
-D PYTHON3_EXECUTABLE=/usr/bin/python3 \
-D PYTHON3_INCLUDE_DIR=/usr/include/python3.5 \
-D PYTHON3_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/local/lib/python3.5/dist-packages/numpy/core/include \
-D INSTALL_PYTHON_EXAMPLES=ON ..
Note that you have to set the <LOCATION_OF_YOUR_OPENCV_CONTRIB> (the place where you cloned it), and you may have to adapt your PYTHON2and PYTHON3flags to your system's disposition.
I got some warnings (not sure how bad that is), but it went through: after a while you should see something like
-- Install to: /usr/local
-- -----------------------------------------------------------------
--
-- Configuring done
Make and install
Again, the standard make -j<NUM_CORES> && sudo make install works here. Make sure to adjust -j to your number of CPU cores to speed up the compilation process. After that, the python command
import cv2
worked both in Python 2 and 3. Hope this helps!
Andres
cmake/OpenCVDetectPython.cmake... near the bottom. There are two complete sets of those variables, one prefixed withPYTHON2and other withPYTHON3. (e.g. I see bothPYTHON2_INCLUDE_DIRandPYTHON3_INCLUDE_DIRthere).