opencvsharp: Unable to load shared library OpenCvSharpExtern, Ubuntu 22.04

Summary of your issue

I’m trying OpenCvSharp on Ubuntu 22.04 and I’m getting following error

 System.DllNotFoundException : Unable to load shared library 'OpenCvSharpExtern' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libOpenCvSharpExtern: cannot open shared object file: No such file or directory

I’m referencing OpenCvSharp4.runtime.ubuntu.18.04-x64 package.

Environment

Ubuntu 22.04

What did you do when you faced the problem?

libOpenCvSharpExtern is located in build folder under ./bin/Debug/net6.0/runtimes/ubuntu.18.04-x64/native/ instead ./bin/Debug/net6.0/. So running ldd libOpenCvSharpExtern.so returns shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory

Then I copied libOpenCvSharpExtern.so to ./bin/Debug/net6.0/ and try to run application from there resulting in same : System.DllNotFoundException : Unable to load shared library 'OpenCvSharpExtern' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libOpenCvSharpExtern: cannot open shared object file: No such file or directory exception.

Then I run ldd libOpenCvSharpExtern.so again resulting in :

❯ ldd libOpenCvSharpExtern.so
        linux-vdso.so.1 (0x00007ffc407f8000)
        libtesseract.so.4 => /lib/x86_64-linux-gnu/libtesseract.so.4 (0x00007f5be46f6000)
        libgtk-x11-2.0.so.0 => /lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0 (0x00007f5be427f000)
        libgdk-x11-2.0.so.0 => /lib/x86_64-linux-gnu/libgdk-x11-2.0.so.0 (0x00007f5be89f9000)
        libcairo.so.2 => /lib/x86_64-linux-gnu/libcairo.so.2 (0x00007f5be4157000)
        libgdk_pixbuf-2.0.so.0 => /lib/x86_64-linux-gnu/libgdk_pixbuf-2.0.so.0 (0x00007f5be89c9000)
        libgobject-2.0.so.0 => /lib/x86_64-linux-gnu/libgobject-2.0.so.0 (0x00007f5be8967000)
        libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007f5be401d000)
        libdc1394.so.22 => not found
        libavcodec.so.57 => not found
        libavformat.so.57 => not found
        libavutil.so.55 => not found
        libswscale.so.4 => not found
        libjpeg.so.8 => /lib/x86_64-linux-gnu/libjpeg.so.8 (0x00007f5be3f9c000)
......

So, I have few questions like:

  • can we have proper Ubuntu 22.04 nuget package ?
  • list of required Ubuntu packages that we need to install before using OpenCvSharp ?
  • fix deploy script so we have libOpenCvSharpExtern.so in proper location ?

I would like to use OpenCvSharp but this is blocking me for using it.

Example code:

	byte[] image = new ();// not important
	Mat src = Cv2.ImDecode( image, ImreadModes.Color );

Output:

 Error Message:
   System.TypeInitializationException : The type initializer for 'OpenCvSharp.Internal.NativeMethods' threw an exception.
  ----> System.DllNotFoundException : Unable to load shared library 'OpenCvSharpExtern' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libOpenCvSharpExtern: cannot open shared object file: No such file or directory

What did you intend to be?

Compile and run.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 17 (4 by maintainers)

Most upvoted comments

Here’s the code @dsoronda

It’s just taking the existing Dockerfile and adding a step to build a NuGet package.

package.zip

One can then create the package by:

docker buildx build -f Focal.Dockerfile . -o export
docker buildx build -f Jammy.Dockerfile . -o export

However, concerning Jammy.Dockerfile, I’m just guessing the needed native deps. But the tests are passing and the OpenCV functionality I need in my project seems to work as well.

@Evelios How far did you get?

I’ve made two NuGet packages for internal use that contain binaries for Ubuntu 20.04 and Ubuntu 22.04. I’m now working on dockerizing the creation of both. I could then turn that into a PR, would you be interested?

For anyone who wants to use 4.7.0 that was released last month, @shimat has already created a dockerfile for building libOpenCvSharpExtern.so under Ubuntu 22.04, so we can build it from scratch

wget https://raw.githubusercontent.com/shimat/opencvsharp/4.7.0.20230114/docker/ubuntu22-dotnet6-opencv4.7.0/Dockerfile
docker buildx build . -o export

Wait for it to complete then cd export/artifacts, finally you should be able to see the libOpenCvSharpExtern.so.

Before using it for your .NET application, you should check out if there are sufficient dependencies had been installed on your environment(out of the building system Ubuntu 22.04 in the docker container):

ldd *.so* | grep found

and install any(not need to install all of them in dockerfile) missing packages to fix these missing dependencies: https://github.com/shimat/opencvsharp/blob/7ed8972de415fad623d425e56b4f87ef2503d68b/docker/ubuntu22-dotnet6-opencv4.7.0/Dockerfile#L9-L34

If you want to use a more recent version of Tesseract(libtesseract-dev from the default source is still in 4.1.1, and the latest is 5.3.0), replace the line 9 of Dockerfile https://github.com/shimat/opencvsharp/blob/7ed8972de415fad623d425e56b4f87ef2503d68b/docker/ubuntu22-dotnet6-opencv4.7.0/Dockerfile#L9 with

RUN apt-get update && apt-get install -y software-properties-common && add-apt-repository ppa:alex-p/tesseract-ocr5
   && apt-get -y install --no-install-recommends \

@lewispurvis if you manage to make these two packages part of the general release procedure, it’d be really great! I’d much rather use these, than my privately maintained ones. I can share the code for building the packages, if you’re interested.

I have been trying to get a solution to this one too. I’ve been following through the steps in the github workflows and am in the process of trying to get it compiled on Ubuntu 22.04 but I’m getting stuck as well.

I am working on a pull request to allow them to publish this runtime package.

A brief rundown of where I’m at, I added .github/workflows/ubuntu22.yml with the following changes

  • Runs on ubuntu-22.04 from ubuntu-22.04
  • libswresample-dev used instead of the deprecated package libawresample-dev
  • libdc1394-dev used instead of the deprecated libdc1394-22-dev

At this point I’m out of ideas on what to run next. I’m running into issues of the libavresample.so package which is not currently built for Ubuntu 22 Jelly Jam and the replacement that I thought could be a good alternative libswresample-dev apparently isn’t quite the right fit for a quick swap which doesn’t seem to be the case

[ 88%] Building CXX object OpenCvSharpExtern/CMakeFiles/OpenCvSharpExtern.dir/wechat_qrcode.cpp.o
[ 91%] Building CXX object OpenCvSharpExtern/CMakeFiles/OpenCvSharpExtern.dir/xfeatures2d.cpp.o
[ 94%] Building CXX object OpenCvSharpExtern/CMakeFiles/OpenCvSharpExtern.dir/ximgproc.cpp.o
make[2]: *** No rule to make target '/usr/lib/x86_64-linux-gnu/libavresample.so', needed by 'OpenCvSharpExtern/libOpenCvSharpExtern.so'.  Stop.
make[2]: *** Waiting for unfinished jobs....
[ 97%] Building CXX object OpenCvSharpExtern/CMakeFiles/OpenCvSharpExtern.dir/xphoto.cpp.o
make[1]: *** [CMakeFiles/Makefile2:98: OpenCvSharpExtern/CMakeFiles/OpenCvSharpExtern.dir/all] Error 2
make: *** [Makefile:136: all] Error 2
Error: Process completed with exit code 2.

I also found this stack overflow about the difference between avresample and swresample.