4

This is not my first time to build opencv, however, I am not sure why the cv2.so is not generated. I use anaconda and here is my cmake line:

cmake -D CMAKE_INSTALL_PREFIX=../output -D PYTHON_EXECUTABLE=/home/b.safwat/anaconda2/envs/opencv3.4/bin/python WITH_FFMPEG=YES -enable-shared --disable-static -D BUILD_NEW_PYTHON_SUPPORT=ON ..

The output does not show the numpy and I suspect this part:

--   Python (for build):  
     /home/<user>/anaconda2/envs/<myenv>/bin/python3
--     Pylint:                      
      /home/<user>/anaconda2/bin/pylint (ver: 1.6.4, checks: 113)

However, I have numpy installed and functioning properly.

Have anyone builded the latest version successfully? did they change the name of cv2.so library?

Kasparov92
  • 1,365
  • 4
  • 14
  • 39

3 Answers3

1

I had the same issue with generating Python 2.7 bindings. Found the solution by manually inspecting opencv/CMakeLists.txt file. I think transposing the variables for Python 3 may solve your issue.

Symptom

Before you start building opencv, you can already tell whether you will get the python binding file cv2.so by examining the cmake command output at lines containing To be built or Unavailable. In my case I had:

Unavailable: cnn_3dobj cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv dnn_modern hdf java matlab ovis python2 python3 sfm viz

Notice how python2 and python3 show up as unavailable right away at the end.

Fix (adapt python2 references for python3 if needed)

In CMakeLists.txt there's a line containing

BUILD_opencv_python2

Next to it are a bunch of lines that generate the output of the cmake command. They are helpful as they inform about which variables cmake expects. In particular, the references to numpy are mandatory to build the python bindings. From these lines, I learned that for opencv 3.4.1, I had to invoke cmake using:

-D PYTHON2_NUMPY_INCLUDE_DIRS=/usr/lib/python2.7/dist-packages/numpy/core/include
-D PYTHON2_NUMPY_VERSION=1.12.1

I can see from your command that you use the deprecated BUILD_NEW_PYTHON_SUPPORT flag. For opencv 3.4 the flag names have changed, and according to the content of CMakeLists.txt the new variable name is:

-D BUILD_opencv_python3=ON

I also read in another SO comment that cv2.so won't be generated without the following flag (untested):

-D BUILD_EXAMPLES=ON

TL;DR

Edits to your command:

  • Remove: -D BUILD_NEW_PYTHON_SUPPORT=ON
  • Add: -D BUILD_opencv_python3=ON -D BUILD_EXAMPLES=ON -D PYTHON3_NUMPY_INCLUDE_DIRS=<path_to_numpy>/core/include -D PYTHON2_NUMPY_VERSION=<your_numpy_version>
zeiky
  • 101
  • 3
1

I found the exact solution here, and what exactly fixed my issue is by adding the following lines:

-D PYTHON_DEFAULT_EXECUTABLE=<path/to/desired/python/environment>/bin/python3.5
-D PYTHON_INCLUDE_DIRS=<path/to/desired/python/environment>/include/python3.5m
-D PYTHON_EXECUTABLE=<path/to/desired/python/environment>/bin/python3.5
-D PYTHON_LIBRARY=<path/to/desired/python/environment>libpython3.5m.so.1
Kasparov92
  • 1,365
  • 4
  • 14
  • 39
0

Check if python3-numpy is installed.If it is not installed, cv2.so will not be generated.

bgth
  • 450
  • 9
  • 26