Hello,
The main issue I’m facing is that when I try to import cv2 that I manually built to the python environment, I received an error saying that the DLL load failed. Allow me to tell you the steps I did prior to this point.
I wanted to use Gstreamer with OpenCV, but since OpenCV doesn’t come native with Gstreamer, I had to build it manually from source. Here’s my current setup:
- Windows 10 22H2
- Python 3.12
- Gstreamer 1.24.0 MinGWx64
- Cmake GUI 3.29.0
- OpenCV 4.9.0 downloaded from the official site
- Visual Studio Community 2022 (17.9.4)
I had this guide as reference for my installation.
After I browsed for the source file and press configure, the CMake GUI did some process and this is what it looked like after the process was done
Notice that the values of some names in the Gstreamer section seems to be missing. Also in the Cmake log (lower part), Gstreamer is marked as NO.
I then added “GSTREAMER_DIR” as an entry via the “Add entry” button, pointing to my Gstreamer installation folder. After re-configuring, all of the values are now filled as like this, and Gstreamer is now marked as YES (1.24.0)
Next, I proceed to Generate, open the project in Visual Studio, then build the source as shown in the guide. The final build had 1 failed component (not sure if it’s right to call it a component, but it showed something like this)
========== Build: 94 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========== Build completed at 10:44 PM and took 08:27.743 minutes ==========
There were also a few errors as shown in the picture below (the website seems to block me from posting more than 1 image, so let me post it in the comments instead)
But when I checked inside the build\lib\python3\Release folder, there was a .pyd file in it (cv2.cp312-win_amd64.pyd). I then copied this to my site-packages folder in my python installation folder and tried to import it. But the import didn’t work, and it showed this error, saying that DLL load failed. Runtime was in CMD, by the way.
C:\Users\NICE>python
Python 3.12.2 (tags/v3.12.2:6abddd9, Feb 6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.add_dll_directory(r"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.4\bin")
<AddedDllDirectory('C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v12.4\\bin')>
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\NICE\AppData\Local\Programs\Python\Python312\Lib\site-packages\cv2\__init__.py", line 181, in <module>
bootstrap()
File "C:\Users\NICE\AppData\Local\Programs\Python\Python312\Lib\site-packages\cv2\__init__.py", line 153, in bootstrap
native_module = importlib.import_module("cv2")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\NICE\AppData\Local\Programs\Python\Python312\Lib\importlib\__init__.py", line 90, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ImportError: DLL load failed while importing cv2: The specified module could not be found.
>>>
I also tried to use process monitoring to check what DLLs are missing. Upon brief inspection, most DLLs have the result of “NAME NOT FOUND”, but a few lines down, that corresponding DLL is loaded from another directory and the result is “SUCCESS”, so I’m assuming there was no problem for those DLLs. However, there is one DLL file that seems to never get the “SUCCESS” result, and it is the opencv_videoio.dll. I noticed that the project name shown in my Visual Studio error list is also similar to this name, so maybe there’s some correlation between them?
What’s weird is that ‘opencv_videoio.dll’ is inside the build folder. I even tried to manually include the build folder using this code
import os
os.add_dll_directory(path\to\my\build\folder)
I'm really stumped as to what to do next. Can you point me to the right direction?
Some things that I think is relevant:
-
When I didn’t manually add the missing values and built it with Gstreamer marked as NO, the build was successful and I could import it properly in python by copying the .pyd file to my site-lib directory. So I’m thinking if something is wrong with my Gstreamer installation or something? (I tried installing the MSVC version as well, and it also didn’t build properly. I might say it built worse, failing to build around 40 components instead of just 1)
-
I also tried installing NVIDIA CUDA Toolkit as a last ditch attempt. I made sure to reset the CMake cache, and repeat the process from scratch, but the error seems to be the same. Similar to the page, I also tried including the NVIDIA CUDA Toolkit installation folder to the dll directory but the error is still not fixed.