Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Travis Windows: ImportError: DLL load failed: The specified module could not be found. #263

Closed
pfaion opened this issue Nov 20, 2019 · 19 comments

Comments

@pfaion
Copy link

pfaion commented Nov 20, 2019

Hi, thanks for putting all the work into this! ✨

I have an issue with using opencv-python on Travis Windows instances.
Here a simple import cv2 will result in ImportError: DLL load failed: The specified module could not be found.

The problem is: this even happens with opencv-python-headless.

I prepared a small example: https://travis-ci.org/pfaion/travis-opencv-python-test/builds/614589763#L68-L72

More context

I had similar problems when loading manually compiled opencv DLLs on travis. It appears this stems from travis using a Windows server version without GUI support. For manually compiled opencv DLLs I was able to fix this by excluding all Windows GUI options in cmake for OpenCV:

...
    -DWITH_WIN32UI=OFF\
    -DWITH_DSHOW=OFF\
    -DWITH_MSMF=OFF\
    -DWITH_DIRECTX=OFF\
...

Now I tried to use opencv-python-headless but I still can't load the DLL. I found a thread where other people reported this problem in a travis forum:
https://travis-ci.community/t/python-and-opencv-dll-load-fails-every-time/4431/6

Apparently someone already figured out the cause of the problem here:

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

So I assume this is just a flag that was not turned off correctly for the headless build? I quickly searched through the issues, but could not found it, so here it is 🙂

@skvark
Copy link
Member

skvark commented Nov 20, 2019

Before I'm switching those off I need to know if that has some side effect somewhere. It might be that if those settings are turned off you can't open for example web cams anymore.

@pfaion
Copy link
Author

pfaion commented Nov 21, 2019

Sounds good, how can we figure this out?

I would have assumed that the headless version is the minimal set of functionality which does not need GUI support. So if opening web cams needs GUI support then maybe this is just not supposed to be supported with headless? Or is this rather a question of documentation? Because currently I also could not find any information about which features are/aren't included in opencv-python-headless.

@skvark
Copy link
Member

skvark commented Nov 21, 2019

GUI support refers to opening windows for example with cv2.imshow. Reading video stream from webcam (there's no need to open graphical user interface) requires that there is some backend which supports reading webcams, e.g. cap = cv2.VideoCapture(0 + cv2.CAP_DSHOW). Server environments could be used for example to process images from cameras.

From OpenCV Cmake output:

--   GUI: 
--     Win32 UI:                    YES
--     VTK support:                 NO

--   Video I/O:
--     DC1394:                      NO
--     FFMPEG:                      YES (prebuilt binaries)
--       avcodec:                   YES (58.54.100)
--       avformat:                  YES (58.29.100)
--       avutil:                    YES (56.31.100)
--       swscale:                   YES (5.5.100)
--       avresample:                YES (4.0.0)
--     GStreamer:                   NO
--     DirectShow:                  YES
--     Media Foundation:            YES
--       DXVA:                      NO

https://docs.opencv.org/master/d0/da7/videoio_overview.html

DSHOW, MSMF etc. are used for video input / output. It might be that cameras or other devices could be opened only via FFmpeg but I doubt it supports all scenarios on Windows.

On Linux this is quite a lot easier since disabling dependencies under the GUI section removes dependency to libs such as libsm, libxext, libxrender and the headless package works out of the box in server environments. On Windows the situation seems to be a bit trickier since the dependency chain is more complicated. This needs to be investigated further - for example is it enough to disable only DirectX to fix the issue.

@pfaion
Copy link
Author

pfaion commented Nov 21, 2019

Correct, maybe not all of the flags are needed. I just disabled everything that referenced GUI on Windows and it worked.

I can test for the minimal set of cmake flags to disable for the build on Windows to work. Will come back to you once I have figured that out.

@skvark
Copy link
Member

skvark commented Nov 21, 2019

This might be related: #261

@pfaion
Copy link
Author

pfaion commented Nov 22, 2019

Sadly installing Media Foundation doesn't seem to help: https://travis-ci.org/pfaion/travis-opencv-python-test/jobs/615439058#L20

@pfaion
Copy link
Author

pfaion commented Nov 22, 2019

TLDR; The WITH_MSMF flag has to be disabled for OpenCV to work on travis.


I had 4 flags disabled in my OpenCV build from source:

  • WITH_WIN32UI
  • WITH_DSHOW
  • WITH_MSMF
  • WITH_DIRECTX

I verified again that loading the DLL works on Travis Windows when all flags are disabled:
BUILD SUCCESS

When I turned MSMF back on, it did not work anymore: FAIL: MSMF turned back on
Turning any 1 other flag back on would still work:

I then did a final test with only WITH_MSMF=OFF which also were able to load the DLL successfully:
SUCCESS: only MSMF disabled

@Borda
Copy link

Borda commented Jan 31, 2020

so can it be disabled also in the pip wheels?

@pfaion
Copy link
Author

pfaion commented Feb 1, 2020

No unfortunately the wheels do not offer that option. You need to compile OpenCV on your own with the compiler flag WITH_MSMF=OFF

@Borda
Copy link

Borda commented Feb 1, 2020

That is not very convenient to do compile every time if you want to test other project, I saw that your build takes about 20min

@pfaion
Copy link
Author

pfaion commented Feb 1, 2020

You can use travis caches to keep a once-built OpenCV between jobs: https://docs.travis-ci.com/user/caching/
Maybe that works for you.

@abhiTronix
Copy link
Contributor

@pfaion Not possible. Jobs in a build matrix are independent, and they do not share storage plus they are not guaranteed to be running simultaneously. See travis-ci/travis-ci#6054 (comment)

@Borda
Copy link

Borda commented Feb 2, 2020

He meant between consecutive jobs for each configuration over particular builds...

@abhiTronix
Copy link
Contributor

@Borda yes, i meant for the same.

@pfaion
Copy link
Author

pfaion commented Feb 3, 2020

@abhiTronix it's a new feature from Travis. Your linked comment is very old from 2016. Have a look at caches and workspaces, you can share data between consequtive jobs and between parallel jobs now!

@pfaion
Copy link
Author

pfaion commented Feb 3, 2020

I experimented with a setup once where I had a pre stage that would be responsible for managing the OpenCV build. It would use caches to store the build across entire travis builds and rebuild in case the cache was not there or I manually triggered a rebuild. The OpenCV build would be made available within jobs via travis workspaces. This all worked very fine. I ended up not using this because of some other non-OpenCV related issues with the build pipeline.

skvark added a commit that referenced this issue Mar 28, 2020
skvark added a commit that referenced this issue Mar 28, 2020
@skvark
Copy link
Member

skvark commented Mar 28, 2020

I disabled MSMF from the headless builds. Change will be included in next release.

@skvark skvark closed this as completed Mar 28, 2020
@pfaion
Copy link
Author

pfaion commented Apr 6, 2020

This is awesome, thank you so much!

@mshabunin
Copy link

FYI, we have added plug-in mode support for Media Foundation backend in OpenCV: opencv/opencv#17009 It means that MSMF will become optional dependency and it will be possible to create single binary for platforms with and without Media Foundation. However this feature did not get into 4.3.0 release and have not been thoroughly tested yet.

pfaion added a commit to pupil-labs/pye3d-detector that referenced this issue May 13, 2020
opencv-python won't load on windows travis instances,
see opencv/opencv-python#263
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants