简体   繁体   中英

how to build opencv for python3 when both python2 and python3 are installed

I was trying to build opencv for python3. However, cmake always sets python build option to be python2.7.11 even after I manually specified include and lib option for python3:

    --   Python 2:
    --   Interpreter:                 /home/ryu/anaconda2/bin/python2.7 (ver 2.7.11)

    --   Python 3:
    --     Interpreter:                 /usr/bin/python3 (ver 3.4.3)
    --     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.4m (ver 3.4.3)
    --     numpy:                       /home/ryu/.local/lib/python3.4/site-packages/numpy/core/include (ver 1.11.0)  

    --  packages path:               lib/python3.4/dist-packages

-- 
--   **Python (for build):            /home/ryu/anaconda2/bin/python2.7**

Did I miss some cmake option?

OS: Ubuntu 14,04

thanks

您可以通过在cmake调用期间将参数PYTHON_DEFAULT_EXECUTABLE附加到python可执行文件URI来覆盖要构建的python可执行文件。

cmake {...} -DPYTHON_DEFAULT_EXECUTABLE=$(which python3) ..

I was struggling with this one for some hours and the answers mentioned above didn't solve the problem straightaway.

Adding to Ivan's answer, I had to include these flags in cmake to make this work:

-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D BUILD_opencv_python3=ON \
-D HAVE_opencv_python3=ON \
-D PYTHON_DEFAULT_EXECUTABLE=<path_to_python3> 

I leave that here, so it is maybe useful for someone else in the future.

I've been trying to install opencv on a Pi3 and this solution didn't work for me as python (for build) was always set to Python2.7 but I found that by changing the order of an elseif statement at the bottom of 'OpenCVDetectPython.cmake' fixed the problem. For me, this file is located at '~/opencv-3.3.1/cmake'.

The original code segment:

if(PYTHON_DEFAULT_EXECUTABLE)
    set(PYTHON_DEFAULT_AVAILABLE "TRUE")
elseif(PYTHON2INTERP_FOUND) # Use Python 2 as default Python interpreter
    set(PYTHON_DEFAULT_AVAILABLE "TRUE")
    set(PYTHON_DEFAULT_EXECUTABLE "${PYTHON2_EXECUTABLE}")
elseif(PYTHON3INTERP_FOUND) # Use Python 3 as fallback Python interpreter (if there is no Python 2)
    set(PYTHON_DEFAULT_AVAILABLE "TRUE")
    set(PYTHON_DEFAULT_EXECUTABLE "${PYTHON3_EXECUTABLE}")
endif()

My re-ordered code segment:

if(PYTHON_DEFAULT_EXECUTABLE)
    set(PYTHON_DEFAULT_AVAILABLE "TRUE")
elseif(PYTHON3INTERP_FOUND) # Use Python 3 as fallback Python interpreter (if there is no Python 2)
    set(PYTHON_DEFAULT_AVAILABLE "TRUE")
    set(PYTHON_DEFAULT_EXECUTABLE "${PYTHON3_EXECUTABLE}")
elseif(PYTHON2INTERP_FOUND) # Use Python 2 as default Python interpreter
    set(PYTHON_DEFAULT_AVAILABLE "TRUE")
    set(PYTHON_DEFAULT_EXECUTABLE "${PYTHON2_EXECUTABLE}")
endif()

I don't know the reasoning behind it, but cmake is set to default to python2 if python2 exists, swapping the order of these elseif statements switches it to default to python3 if it exists

** Disclaimer **

  1. I was using the script found at https://gist.github.com/willprice/c216fcbeba8d14ad1138 to download, install and build everything (script was modified to not create a virtual environment as I didn't want one and with j1 not j4 as it failed around 85% when running with multiple cores).
  2. I don't think the relevant file exists until you have attempted a build.

it was take some hours for me. I built Dockerfile with opencv for python3 the key string is

pip install numpy

Full Docker file:

FROM python:3.8

RUN apt-get update && apt-get -y install \
    cmake \
    qtbase5-dev \
    libdc1394-22-dev \
    libavcodec-dev \
    libavformat-dev \
    libswscale-dev

RUN cd /lib \
    && git clone --branch 4.1.1 --depth 1  https://github.com/opencv/opencv.git \
    && git clone --branch 4.1.1 --depth 1  https://github.com/opencv/opencv_contrib.git

RUN pip install numpy \
    && mkdir /lib/opencv/build \
    && cd /lib/opencv/build \
    && cmake -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local -DWITH_TBB=ON -DWITH_V4L=ON -DWITH_QT=ON -DWITH_OPENGL=ON -DWITH_FFMPEG=ON -DOPENCV_ENABLE_NONFREE=ON -DOPENCV_EXTRA_MODULES_PATH=/lib/opencv_contrib/modules .. \
    && make -j8 \
    && make install

CMD ["bash"]

The main point is to force compiler to build cv2 module for python

To make it we need python3 should be included in line To be built in CMakeCache.txt file in build folder of opencv

ref https://breakthrough.github.io/Installing-OpenCV/

If there are any errors, ensure that you downloaded all the required packages - the output should help track down what is missing. To ensure the Python module will be built, you should see python2 in the list of configured modules after running cmake

(in my case python3)

Changing the options in cmake did nothing for me no matter what options I modified. The simpliest (hacky) solution for me was to

sudo mv /usr/bin/python2.7 /usr/bin/pythonNO-temp

Then you build and install opencv

then

sudo mv /usr/bin/pythonNO-temp /usr/bin/python2.7

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM