Python and OpenCV: DLL Load fails every time

I’m trying to get my CI working on all 3 platforms (Win, Mac, Linux). Mac and Linux are ok, but something about Windows is giving me trouble.

Whenever import cv2 is called, which is the import for OpenCV, the build throws this error:

ImportError while importing test module 
 'C:\Users\travis\build\samclane\Discordia\Discordia\test\test_all.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
Discordia\test\test_all.py:14: in <module>
    from Discordia.Interface.Rendering.DesktopApp import MainWindow
Discordia\Interface\Rendering\DesktopApp.py:12: in <module>
    import pixelhouse as ph
c:\python37\lib\site-packages\pixelhouse\__init__.py:2: in <module>
    from .canvas import Canvas, load
c:\python37\lib\site-packages\pixelhouse\canvas.py:2: in <module>
    import cv2
c:\python37\lib\site-packages\cv2\__init__.py:3: in <module>
    from .cv2 import *
E   ImportError: DLL load failed: The specified module could not be found.

I’ve tried adding choco install opencv, which probably helps, but it still can’t find the location. The console output from TravisCI states:

“You will need to manually create an OPENCV_DIR environment variable then add %OPENCV_DIR%\bin to your PATH.
Alternately, you can rerun this and pass /Environment:C:\path\to Follow the OpenCV instructions on their website to figure out which directory you would want.”.

So I’ve tried to EXPORT the folder that choco claims it installed OpenCV in, /c/tools. Here is my .travis.yaml:

language: python            # this works for Linux but is an error on macOS or Windows
matrix:
  include:
    - name: "Python 3.7 on Xenial Linux"
      python: 3.7           # this works for Linux but is ignored on macOS or Windows
      dist: xenial          # required for Python >= 3.7
    - name: "Python 3.7 on macOS"
      os: osx
      osx_image: xcode10.2  # Python 3.7.2 running on macOS 10.14.3
      language: shell       # 'language: python' is an error on Travis CI macOS
    - name: "Python 3.7 on Windows"
      os: windows           # Windows 10.0.17134 N/A Build 17134
      language: shell       # 'language: python' is an error on Travis CI Windows
      before_install:
        - choco install python
        - choco install opencv  # need opencv binaries
      env:
        - PATH=/c/Python37:/c/Python37/Scripts:/c/tools/bin:$PATH**
        - OPENCV_DIR=/c/tools**
  allow_failures:
    - os: windows           # Windows 10.0.17134 N/A Build 17134
install:
  - pip3 install --upgrade pip || pip3 install --upgrade --user pip  # --user is required for Windows
  - pip3 install -r requirements.txt || pip3 install -r  --user requirements.txt
  - pip3 install pytest pytest-cov codecov || pip3 install --user pytest pytest-cov codecov  # install coverage monitor
# 'python' points to Python 2.7 on macOS but points to Python 3.7 on Linux and Windows
# 'python3' is a 'command not found' error on Windows but 'py' works on Windows only
script:
  - pytest --cov=./

after_success:
  - codecov

I’m pretty sure I’m not adding the right OPENCV_DIR variable in my PATH, but I have no way of verifying specifically where it would be installed. Do I have my env. vars. correct?

Any help would be appreciated. Thanks.

EDIT: My full Build Log is here: https://travis-ci.com/samclane/Discordia/jobs/220275952

Please link to the build, there’s insufficient information for diagnostics.

@native-api whoops, didn’t know you could link to full builds externally.

https://travis-ci.com/samclane/Discordia/jobs/220275952

Remove choco install opencv and all associated envvars.

One of your pip modules installs opencv_python as a dependency and that is what your test actually uses. The OPENCV_DIR and/or PATH envvars with an unrelated OpenCV installation there must be confusing it.

Same thing happens. That’s why I originally added the choco install and ENVARS.

The missing DLLs are %windir%\system32\MF.dll, MFPLat.dll and MFReadWrite.dll which are a part of DirectX. So, the root cause is

opencv-python-headless gives the same result because this is a result of OpenCV-Python being linked with AVFoundation support.

So the only option for you at the moment is to build and use a custom opencv-python build without AVFoundation support.

I had a feeling this was a non-trivial problem, with Windows support being in dev and all. I can just manually run the test suite on my Windows machine.

How do you know this is because of AVFoundation support, may I ask? Is that something that implicitly makes GUI calls, so any opencv import will fail, or something?

This is my best guess but with high confidence. Those DLLs constitute the Media Foundation interface, and that is the only thing in the configuration banner that looks relevant. The only other relevant thing, the bunded FFMpeg DLL, does not link to any “unusual” system DLLs. So whether it’s this component or another, it’s customizing the build anyway, so doesn’t make much difference.

I also checked that DirectX is not available on Windows Server Core just like the GUI components.

This all sounds above my knowledge level at the moment. I don’t think I’ll be rewriting opencv-python anytime soon. It’ll be shorter to just wait on GUI support to come to the Windows image.

Thank you very much for answering my questions.

FWIW, Media Foundation is actually available as an optional component but installing it still doesn’t solve the problem.

so what is the proper configuration? mine is also failing…