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

前端 未结 5 2096
我在风中等你
我在风中等你 2020-12-13 10:29

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 python

相关标签:
5条回答
  • 2020-12-13 11:06

    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.

    0 讨论(0)
  • 2020-12-13 11:06

    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

    0 讨论(0)
  • 2020-12-13 11:08

    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.
    0 讨论(0)
  • 2020-12-13 11:30

    You can override the python executable to build to by appending the argument PYTHON_DEFAULT_EXECUTABLE with the python executable URI during the cmake invokation.

    cmake {...} -DPYTHON_DEFAULT_EXECUTABLE=$(which python3) ..
    
    0 讨论(0)
  • 2020-12-13 11:31

    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)

    0 讨论(0)
提交回复
热议问题