Ask Your Question
0

At my Wit's End with OpenCV Build from Source, Please Help.

asked 2020-11-13 14:39:51 -0600

Shrinjay gravatar image

Hey all,

As the title says, I'm getting increasingly annoyed with building opencv from source. I can't just use a python wheel as I require gstreamer support, so it seems the only way to do this is build from source.

The error I'm getting is: "ImportError: DLL load failed while importing cv2: The specified module could not be found." when I try to importcv2.

I've googled the error, taken steps that have been recommended there (see below), but nothing seems to work.

Steps I've taken: 1. Downloaded the opencv 4.40 binary, have also tried with 4.50. Generated with CMAKE using correct paths to python installation, include dirs, library etc. 2. Built and installed in VS2019 in Release Mode for x64. 3. Copied cv2.pyd to my Python38/Lib/site-packages directory.

Attempts to Resolve:

  • Built from a wheel and then uninstalled wheel to get dll files required, no avail. Installed and uninstalled opencv-python with pip to get dll files, no avail.

  • Checked with DependencyWalker, saw opencv450_world.dll was missing,tracked it down inopencv/build/x64/vc15/bin and copied it to Python38/DLL, and failing that, site-packages.
  • Copied init.py, configure.py and configure_load.py to
    site-packages, resulted in recursion error.
  • Tried with python 2.7, same error.
  • Tried a clean install of both
    python versions then repeating steps above, no avail.
  • Tried different versions of opencv, 4.50 and 4.10, no avail.

Any other suggestions? I'd be happy to pull more logs if you'd like. However, the CMake and VS processes run without a hitch.

Seriously, thanks so much in advance.

Kind Regards, Shrinjay

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2020-11-14 12:52:52 -0600

Eduardo gravatar image

I just tried it successfully:

  • install Python on Windows: https://www.python.org/downloads/
  • after the installation, you should have in the user environment variable the path to the Python folder. Something like C:\Users\user\AppData\Local\Programs\Python\Python39\
  • git clone opencv
  • configure using CMake GUI, you should check that the following CMake variables are set to a valid Python folder, e.g. on my machine:

PYTHON3_EXECUTABLE: e.g. C:/Users/user/AppData/Local/Programs/Python/Python39/python.exe PYTHON3_INCLUDE_DIR: e.g. C:/Users/user/AppData/Local/Programs/Python/Python39/include PYTHON3_LIBRARY: e.g. C:/Users/user/AppData/Local/Programs/Python/Python39/libs/python39.lib PYTHON3_NUMPY_INCLUDE_DIRS: e.g. C:/Users/user/AppData/Local/Programs/Python/Python39/lib/site-packages/numpy/core/include

PYTHON3_PACKAGES_PATH: e.g. C:/Users/user/AppData/Local/Programs/Python/Python39/Lib/site-packages

For the test, I have used a custom installation directory for PYTHON3_PACKAGES_PATH, something like: C:/Users/user/opencv/build/install/Python

If you need to install Numpy: open a PowerShell terminal and install Numpy with pip install numpy

Beware, you may encounter the following bug report (as of 2020/11/14) with numpy 1.19.4 and Windows 10 2004.

If so, uninstall Numpy with pip uninstall numpy and install this specific version pip install numpy==1.19.3.

Once CMake has been correctly configured, you should have these CMake variables ticked: BUILD_opencv_python3, BUILD_opencv_python_bindings_generator and BUILD_opencv_python_tests.

Generate the CMake project and open it with Visual Studio. Build in Release mode (CMakeTargets > ALL_BUILD). And install (CMakeTargets > INSTALL).

Now, add to the user environment variables, to the path variable the path to the OpenCV dll: %OpenCV_DIR%\x64\vc16\bin. Here I have added another OpenCV_DIR environment variable that point to the install folder of OpenCV.

Same thing for the PYTHONPATH user environment variable. Create one if needed that point to the directory that contains the Python OpenCV package. See the PYTHON3_PACKAGES_PATH CMake variable.

Now open a new PowerShell terminal. You can check that the environment variables are correctly loaded with (here to print the PYTHONPATH env variable):

echo $env:PYTHONPATH

Now you should be able to run Python OpenCV script, e.g.:

``` import cv2 as cv

print(cv.getBuildInformation()) ```

You can also list the Python packages on your machine:

  • pip install ipython
  • ipython in a PowerShell terminal
  • help('modules')
  • you can also test directly the OpenCV import in the interactive Python
edit flag offensive delete link more

Comments

If I could hug you, I would right now. Thank you so, so, so, very much. That was a clear explanation and I realized all I was missing is PYTHONPATH. Seriously, I can't describe the gratitude. .

Shrinjay gravatar imageShrinjay ( 2020-11-16 00:23:13 -0600 )edit
0

answered 2020-11-14 07:55:11 -0600

berak gravatar image

unless you use the BUILD_SHARED_LIBS=OFF cmake option (building static opencv libs instead of dynamic ones), your cv2 will depend on other opencv dlls/sos, and you have to add their location to the environment PATH variable, so they can be found at runtime

edit flag offensive delete link more

Comments

Which DLLs? opencvworld_450 and the DLLs in install/vc16/bin? I have both VC16/bin and lib in my environment PATH. Am I missing some DLL locations?

Shrinjay gravatar imageShrinjay ( 2020-11-14 10:33:13 -0600 )edit

usually, if you build from src, BUILD_opencv_world=OFF (please check your cmake settings !), so your cv2.pyd will not depend on that, but "single module libs", like opencv_core450.dll

berak gravatar imageberak ( 2020-11-14 10:41:04 -0600 )edit

I see. I've got those libs in python37/DLLs, as well I copied all the DLLs from vc16/bin right into the cv2.pyd directory. Still doesn't work :/

Shrinjay gravatar imageShrinjay ( 2020-11-14 12:34:55 -0600 )edit

You definitively do not need to copy any DLLs or copy any Python packages. You don't need to copy so on Linux, it is similar on Windows.

It's like building OpenCV from source. Default configuration is the way to go. Just tweak the variables you need.

Eduardo gravatar imageEduardo ( 2020-11-14 12:55:36 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-11-13 14:39:51 -0600

Seen: 3,326 times

Last updated: Nov 14 '20