PythonCall.pyimport opencv on Windows

I’m trying to PythonCall.pyimport opencv on Windows:

const cv2 = PythonCall.pynew()
function __init__()
    PythonCall.pycopy!(cv2, pyimport("cv2"))
end

but I run into

Python: ImportError: DLL load failed while importing cv2: The specified procedure could not be found.

Here are the CI reports (you can see that it works perfectly fine in all the other OS).

Has anyone seen something like this and knows how to set up Python dependencies for Windows-CI, specifically for opencv?

Why not use GitHub - JuliaImages/OpenCV.jl: Use OpenCV in Julia!! 🚀?

Excellent question! I did, and it was marvelous (i.e. easier and faster than using opencv via python), but it randomly failed tests (see here for CI reports). I’m pretty sure the issue is in OpenCV.calibrateCamera. The problem was that OpenCV.calibrateCamera was returning nonsensical values (1e-300 etc).
I’m in the process of writing tests for OpenCV.jl in hopes of solving this issue.

My guess is that you have run into DLL Hell.

I would try to figure out the dependency situation using a Windows dependency walker such as the following:

Alternatively, try to load the opencv_JLL firs.

Okay…

  1. Thank you very much, you solved that issue :heart:
  2. I still don’t:
    • know if what you meant was to add OpenCV_jll as a dependency to my package and using OpenCV_jll.
    • understand why in the world that would work (but it does!)
    • accept that while opencv via PythonCall together with OpenCV_jll works, each alone doesn’t…

In any case, I hope we can solve the issues in OpenCV_jll and just ignore this oddity. For the curious, here’s my PR regarding that: add tests for chessboard corner detection by yakir12 · Pull Request #52 · JuliaImages/OpenCV.jl · GitHub

Why it would work is that Python’s opencv will load some version of libopencv, which probably depends on some library that is incompatible with a library already loaded by Julia. By loading OpenCV_jll first (which will also load libopencv) you essentially force a compatible set of libraries to be loaded first and then the Python opencv package uses those instead.

A fix which avoids depending on OpenCV_jll is to find which library is clashing and ensure a compatible version is used instead - but it is particularly annoying to do this on Windows (“DLL hell”).

1 Like

Thank you, that explains it very well.

I’ll keep hacking at improving OpenCV.jl instead, that seems like the future anyways.

1 Like