opencv / opencv-python

Automated CI toolchain to produce precompiled opencv-python, opencv-python-headless, opencv-contrib-python and opencv-contrib-python-headless packages.
https://pypi.org/project/opencv-python/
MIT License
4.52k stars 843 forks source link

Unable to build on RHEL7 #367

Closed tedski closed 4 years ago

tedski commented 4 years ago

Expected behaviour

Following the manual build process to build on RHEL7 should work as documented.

Actual behaviour

Build fails with a failure related to opencv-430.jar.

  [ 98%] Built target opencv_java_jar_source_copy
  Scanning dependencies of target opencv_java_jar
  [ 98%] Generating opencv-430.jar
  Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=256m; support was removed in 8.0
  Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=1024m; support was removed in 8.0
  Buildfile: build.xml

  jar:
      [javac] Compiling 125 source files to /tmp/pip-req-build-8ts0bteb/_skbuild/linux-x86_64-3.7/cmake-build/modules/java/jar/opencv/build/classes
        [jar] Building jar: /tmp/pip-req-build-8ts0bteb/_skbuild/linux-x86_64-3.7/cmake-build/bin/opencv-430.jar
  Target 'jar' failed with message 'Problem creating jar: /tmp/pip-req-build-8ts0bteb/_skbuild/linux-x86_64-3.7/cmake-build/bin/opencv-430.jar (No such file or directory) (and the archive is probably corrupt but I could not delete it)'.

  BUILD FAILED
  /tmp/pip-req-build-8ts0bteb/_skbuild/linux-x86_64-3.7/cmake-build/modules/java/jar/opencv/build.xml:18: Problem creating jar: /tmp/pip-req-build-8ts0bteb/_skbuild/linux-x86_64-3.7/cmake-build/bin/opencv-430.jar (No such file or directory) (and the archive is probably corrupt but I could not delete it)

  Total time: 2 seconds
  gmake[2]: *** [CMakeFiles/dephelper/opencv_java_jar] Error 1
  gmake[1]: *** [modules/java/jar/CMakeFiles/opencv_java_jar.dir/all] Error 2
  gmake: *** [all] Error 2
    File "/tmp/pip-build-env-6z50yb3j/overlay/lib/python3.7/site-packages/skbuild/setuptools_wrap.py", line 589, in setup
      cmkr.make(make_args, env=env)
    File "/tmp/pip-build-env-6z50yb3j/overlay/lib/python3.7/site-packages/skbuild/cmaker.py", line 507, in make
      os.path.abspath(CMAKE_BUILD_DIR())))

Steps to reproduce

Issue submission checklist
tedski commented 4 years ago

This is an issue with the virtual environment. I cannot reproduce this outside of the virtual environment, therefore I cannot reliably say this is an issue with opencv-python. I believe this is an issue with adding some FindPython hints to CMAKE_ARGS in order to properly build.

skvark commented 4 years ago

It looks like OpenCV's Cmake script found some Java on your machine but failed to compile the java wrapper and failed the whole build. You could disable java via cmake args, I'll see if it makes sense to disable it in setup.py directly.

tedski commented 4 years ago

One thing I observed that was interesting and may be useful to you is that when building opencv-python using pip wheel ., the jar directory (/tmp/pip-req-build-x6z899pg/_skbuild/linux-x86_64-3.6/cmake-build/bin/ in this issue) was not created. However, when building the upstream opencv repo it was. As a workaround during investigation, I patched one of the CMake files as so:

diff --git a/modules/java/jar/CMakeLists.txt b/modules/java/jar/CMakeLists.txt
index 33817bcc62..f302d46ca1 100644
--- a/modules/java/jar/CMakeLists.txt
+++ b/modules/java/jar/CMakeLists.txt
@@ -9,6 +9,7 @@ file(MAKE_DIRECTORY "${OPENCV_JAVA_DIR}/build/classes")
 set(java_src_dir "${OPENCV_JAVA_DIR}/java")
 file(MAKE_DIRECTORY "${java_src_dir}")

+file(MAKE_DIRECTORY "${OpenCV_BINARY_DIR}/bin")
 set(JAR_NAME opencv-${OPENCV_JAVA_LIB_NAME_SUFFIX}.jar)
 set(OPENCV_JAR_FILE "${OpenCV_BINARY_DIR}/bin/${JAR_NAME}" CACHE INTERNAL "")

This worked around the jar building issue, but it seems odd that I would have this issue and others wouldn't.

tedski commented 4 years ago

I tried disabling java and I have a new flavor of error:

$ echo $CMAKE_ARGS
-DBUILD_opencv_java=OFF
$ echo $ENABLE_HEADLESS
1
$ python3 setup.py sdist --formats=gztar
[SNIP]
$ pip install dist/opencv-python-headless-4.3.0+914404d.tar.gz
[SNIP]
Successfully built opencv-python-headless
Installing collected packages: opencv-python-headless
  Attempting uninstall: opencv-python-headless
    Found existing installation: opencv-python-headless 4.3.0+914404d
    Uninstalling opencv-python-headless-4.3.0+914404d:
      Created temporary directory: /home/tstrzalk/venvs/opencv/lib/python3.7/site-packages/~v2
      Removing file or directory /home/tstrzalk/venvs/opencv/lib/python3.7/site-packages/cv2/
      Created temporary directory: /home/tstrzalk/venvs/opencv/lib/python3.7/site-packages/~pencv_python_headless-4.3.0+914404d.dist-info
      Removing file or directory /home/tstrzalk/venvs/opencv/lib/python3.7/site-packages/opencv_python_headless-4.3.0+914404d.dist-info/
      Successfully uninstalled opencv-python-headless-4.3.0+914404d

Successfully installed opencv-python-headless-4.3.0+914404d

Seems like it worked, but I get an error on import:

>>> import cv2
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-2-c8ec22b3e787> in <module>()
----> 1 import cv2

~/src/opencv-python/cv2/__init__.py in <module>()
      3 import sys
      4
----> 5 from .cv2 import *
      6 from .data import *
      7

ModuleNotFoundError: No module named 'cv2.cv2'
skvark commented 4 years ago

The jar directory was not probably created because we generate here only the Python bindings, nothing else.

The other issue you are having is caused by the fact that you are trying to import the package in the same directory where there original sources are (note the script path). Python always searches for packages from the current directory first and after that from the environment. Just go to some other directory and try again.

tedski commented 4 years ago

🤦 That's right. I got lost in the weeds there. Building without java enabled worked well.