Cannot install openCV 3.1.0 with python3. CMAKE not including or linking python correctly

user3667125 picture user3667125 · Mar 7, 2017 · Viewed 13.2k times · Source

I'm trying to get OpenCV 3.1.0 installed with Python3 on my machine. Because I have Ubuntu 16.04, I am following this guide exactly:

http://www.pyimagesearch.com/2016/10/24/ubuntu-16-04-how-to-install-opencv/

However, I have trouble in step 4, after running cmake. The output of my cmake program has this snippet:

--   OpenCL:
--     Version:                     dynamic
--     Include path:                /home/kevin/opencv-3.1.0/3rdparty/include/opencl/1.2
--     Use AMDFFT:                  NO
--     Use AMDBLAS:                 NO
-- 
--   Python 2:
--     Interpreter:                 /home/kevin/.virtualenvs/cv/bin/python (ver 3.5.2)
-- 
--   Python 3:
--     Interpreter:                 /home/kevin/.virtualenvs/cv/bin/python3 (ver 3.5.2)
-- 
--   Python (for build):            /home/kevin/.virtualenvs/cv/bin/python
-- 
--   Java:
--     ant:                         NO
--     JNI:                         /usr/lib/jvm/java-8-oracle/include /usr/lib/jvm/java-8-oracle/include/linux /usr/lib/jvm/java-8-oracle/include
--     Java wrappers:               NO
--     Java tests:                  NO

The cmake file doesnt seem to properly link the missing properties under the python section (libraries, numpy, packages_path). I tried compiling opencv and installing it, and when I load python3 and import cv, it says it could not find the cv package.

I tried modifying the CMAKE command, so instead of using what the guide suggested, I added three parameters to force cmake to include the missing python properties (the last three properties were my modification):

cmake -D CMAKE_BUILD_TYPE=RELEASE     \
-D CMAKE_INSTALL_PREFIX=/usr/local     \
-D INSTALL_PYTHON_EXAMPLES=ON     \
-D INSTALL_C_EXAMPLES=OFF     \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules     \
-D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python     \
-D BUILD_EXAMPLES=ON    \
-D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so    \
-D PYTHON_NUMPY_INCLUDE_DIR=~/.virtualenvs/cv/lib/python3.5/site-packages/numpy/core/include    \
-D PYTHON_PACKAGES_PATH=~/.virtualenvs/cv/lib/python3.5/site-packes ..

But still, the output of cmake is the same (it only lists the interpretter, and no other python properties), and when I try building opencv and installing it, my python interpretter could not find the opencv package.

My /usr/local/lib/python3.5/ file did NOT have a site-packages directory after I finished installing opencv either.

Can anyone point me in the right direction? I've been trying to search the past few hours on how to fix this, but could not find a way so far, so I'd greatly appreciate any guidance. Thank you!

Update

I realize that I have to add -D BUILD_opencv_python3=yes to the cmake command so that opencv looks for the python libraries. However, this is the new output that I get from cmake:

--   Python 3:
--     Interpreter:                 /home/kevin/.virtualenvs/cv/bin/python (ver 3.5.2)
--     Libraries:                   NO
--     numpy:                       NO (Python3 wrappers can not be generated)
--     packages path:               lib/python3.5/site-packages

Any idea why cmake is not finding my python 3 libraries correctly (even when I add "-D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so " to the cmake options)?

Update

I fixed the problem by deleting the entire /build directory, and running cmake on a fresh directory. I think the error continued to show up because of some caching done by cmake, but adding the flag -D BUILD_opencv_python3=yes seem to do the trick for me.

Answer

user3667125 picture user3667125 · Jun 4, 2017

I fixed my problem by deleting the entire /build directory, and running cmake again (with -D BUILD_opencv_python3=yes). I think it gave an error because of some sort of caching from cmake, so when I deleted everything and started over again, the error was gone.