Dims
Dims

Reputation: 50989

How to build OpenCV for both python versions 2 and 3?

In OpenCV linux install doc, section Building OpenCV from Source Using CMake it is said to run command like

cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

Simultaneously, there are given parameters for python:

[optional] Building python. Set the following python parameters:
PYTHON2(3)_EXECUTABLE = <path to python>
PYTHON_INCLUDE_DIR = /usr/include/python<version>
PYTHON_INCLUDE_DIR2 = /usr/include/x86_64-linux-gnu/python<version>
PYTHON_LIBRARY = /usr/lib/x86_64-linux-gnu/libpython<version>.so
PYTHON2(3)_NUMPY_INCLUDE_DIRS = /usr/lib/python<version>/dist-packages/numpy/core/include/

Some of these parameters can easily be set for both version of Python:

cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D PYTHON2_EXECUTABLE=/usr/bin/python -D PYTHON3_EXECUTABLE=/usr/bin/python3  ..

but another ones are ambiguous

PYTHON_INCLUDE_DIR = /usr/include/python<version>

Is it possible to build for both versions of Python at once?

Upvotes: 3

Views: 6242

Answers (3)

fr_andres
fr_andres

Reputation: 6667

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

Upvotes: 2

sleepyhead
sleepyhead

Reputation: 469

I would recommend this guide as an install reference.

But in general all you have to do is to install both versions of python and then run the install script. There is no need to manually specify python paths, cmake will find them.

Attached screenshot with recent OpenCV install output: terminal screenshot, python paths

Upvotes: 2

glenfant
glenfant

Reputation: 1318

No, bindings to binary modules (compiled from C) are different in Python 2 and Python 3. The same built libs cannot be used from Python 2 and Python 3.

But you may run build instructions for Python 2, then Python 3 using distinct CMAKE_INSTALL_PREFIX values.

Upvotes: 0

Related Questions