I have a problem compiling opencv with cuda and gstreamer, tried every possibility but can't solve it, please help

The following settings are exactly the same, and it works when I build without gstreamer. But if I set gstreamer related, it doesn’t work.
I have tried many possibilities. I have been trying to install gstreamer, uninstall gstreamer, change gstreamer version, and build opencv these days. But there is no solution.

gstreamer installed in D:\gstreamer
added path D:\gstreamer\1.0\msvc_x86_64\bin
D:\gstreamer\1.0\msvc_x86_64\lib
D:\gstreamer\1.0\msvc_x86_64\lib\gstreamer-1.0

and cmake all ok without error.

  Video I/O:
    DC1394:                      NO
    FFMPEG:                      YES (prebuilt binaries)
      avcodec:                   YES (58.134.100)
      avformat:                  YES (58.76.100)
      avutil:                    YES (56.70.100)
      swscale:                   YES (5.9.100)
      avresample:                YES (4.0.0)
    GStreamer:                   YES (1.18.6)
    DirectShow:                  YES
    Media Foundation:            YES
      DXVA:                      YES

vs2022 built without error too.

y final in python when i import cv2.
it show

Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\code\python\yolov5cvhwpy311\!testcvHW2.py", line 1, in <module>
    import cv2
  File "D:\python\Python311\Lib\site-packages\cv2\__init__.py", line 181, in <module>
    bootstrap()
  File "D:\python\Python311\Lib\site-packages\cv2\__init__.py", line 153, in bootstrap
    native_module = importlib.import_module("cv2")
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\python\Python311\Lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ImportError: DLL load failed while importing cv2: 找不到指定的模块。

Process finished with exit code 1

I suspect you need to add the directory containing the gstreamer dll’s using

import os
os.add_dll_directory("D:\\gstreamer\\1.0\\msvc_x86_64\\bin\\")

See this guide for more details on the error you are getting.

2 Likes

thank you very much. finally, its work. Is this necessary in python for it to work? Or is it caused by a problem with my system environment? See [this](https://github.com/cudawarped/opencv-experiments/blob/master/nbs/ImportError_dll_load_failed_while_importing_cv2.ipynb) guide,and sitll dont know why the system path no working for gstreamer,but working for cuda :cold_face:, but fin , I can now test with gstreamer

2 Likes

For OpenCV to use gstreamer it needs to know where it is. An alternative would be to copy the gstreamer dll’s into the same directory as your .pyd but I would not advise this.

Since python 3.8 dll’s can only be loaded from trusted locations

This means you cannot rely on using your system/user path and you need to explicitly add them with os.add_dll_directory. If your using conda or a similar distribution then it may automatically add the directories on your system/user path on initialization.

2 Likes

Thank you very much for your patient reply, I learned something I didn’t know before.

1 Like