2

I have tried installing OpenCV from source. It installs perfectly for python 2.7 and python 3.5, but isn't installing for python 3.6.

I built it using the following command:

cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D PYTHON_EXECUTABLE=/usr/bin/python3.6 ..

The output shows:

--   Python 3:
--     Interpreter:                 /usr/bin/python3 (ver 3.5.2)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.5m.so (ver 3.5.2)
--     numpy:                       /home/courts/.local/lib/python3.5/site-packages/numpy/core/include (ver 1.12.0)
--     packages path:               lib/python3.5/dist-packages
-- 
--   Python (for build):            /usr/bin/python3

And when I make install the OpenCV library becomes present in the dist-packages folder for python3.5, but not python3.6.

This is in the build log:

-- Found PythonInterp: /usr/bin/python3.6 (found suitable version "3.6.2", minimum required is "2.7") 
-- Found PythonInterp: /usr/bin/python3 (found suitable version "3.5.2", minimum required is "3.4") 
-- Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython3.5m.so (found suitable exact version "3.5.2") 
Amir
  • 609
  • 1
  • 9
  • 21
Morgoth
  • 1,843

2 Answers2

1

I listed all the compiler flags by running:

cmake -L | awk '{if(f)print} /-- Cache values/{f=1}'

Using the flags that looked helpful, I constructed this:

cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local/ \
      -D PYTHON_EXECUTABLE=/usr/bin/python3.6 \
      -D PYTHON_INCLUDE=/usr/include/python3.6/ \
      -D PYTHON_LIBRARY=/usr/lib/python3.6/ \
      -D PYTHON_PACKAGES_PATH=/usr/local/lib/python3.6/dist-packages/ \
      -D PYTHON_NUMPY_INCLUDE_DIR=/usr/local/lib/python3.6/dist-packages/numpy/core/include/ \
      ..

This worked.

Morgoth
  • 1,843
0

After following these steps first to install Python 3.6 on Ubuntu 16 you will have to do the following to get OpenCV compiled only for Python 3.6. This has been tested for OpenCV 3.4.3.

curl -L https://github.com/opencv/opencv/archive/3.4.3.zip -o opencv.zip
curl -L https://github.com/opencv/opencv_contrib/archive/3.4.3.zip -o opencvContrib.zip
unzip -q opencvContrib.zip
unzip -q opencv.zip && cd opencv-3.4.3/ && mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=RELEASE \
        -DBUILD_opencv_python3=yes \
        -DCMAKE_INSTALL_PREFIX=/usr/local/ \
        -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.3/modules \
        -DPYTHON3_EXECUTABLE=/usr/bin/python3.6 \
        -DPYTHON3_INCLUDE=/usr/include/python3.6/ \
        -DPYTHON3_INCLUDE_DIR=/usr/include/python3.6m \
        -DPYTHON3_LIBRARY=/usr/lib/python3.6/config-3.6m-x86_64-linux-gnu/libpython3.6.so \
        -DPYTHON3_PACKAGES_PATH=/usr/local/lib/python3.6/dist-packages/ \
        -DPYTHON_NUMPY_INCLUDE_DIR=/usr/local/lib/python3.6/dist-packageis/numpy/core/ \
        -DBUILD_NEW_PYTHON_SUPPORT=ON 
make -j 4 && make install && cd / && rm opencv.zip && rm opencvContrib.zip && rm -rf opencv-3.4.3/
Amir
  • 609
  • 1
  • 9
  • 21