shimat / opencvsharp

OpenCV wrapper for .NET
Apache License 2.0
5.39k stars 1.15k forks source link

Unable to load shared library OpenCvSharpExtern, Ubuntu 22.04 #1448

Open dsoronda opened 2 years ago

dsoronda commented 2 years ago

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:

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.

cmonzon44 commented 2 years ago

Any suggestions to fix that? I have the same error. env: ubuntu 22 and .net6

Evelios commented 2 years ago

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

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.

lejsekt commented 2 years ago

@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?

lewispurvis commented 1 year ago

Is there any reason that the nuget packages for 22.04 and 20.04 can't be uploaded to nuget? The master branch builds for 20.04 just fine, i am guessing it hasn't been thoroughly tested?

If I am able to build for that target, is there a licence restriction that stop me from uploading the 20.04 version for a specific version of OpenCV (say... 4.6.0) to the nuget repository for example?

lejsekt commented 1 year ago

@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.

dsoronda commented 1 year ago

@lejsekt please share the code and make PR. This issue is blocker for me and I hope that PR will be approved. Thanks for your effort.

ransagy commented 1 year ago

We just hit the same issue upgrading from .NET Core 3.1 to .NET 6, although we are actually running on Ubuntu 18.04, but we're getting the same results here.

lejsekt commented 1 year ago

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.

lejsekt commented 1 year ago

And similar issue in another repo: https://github.com/sdcb/PaddleSharp/issues/25. It might be an inspiration for how to create the package.

n0099 commented 1 year ago

https://github.com/shimat/opencvsharp/issues/1448#issuecomment-1298124942

taking the existing Dockerfile and adding a step

$ diff -u *.Dockerfile
--- Focal.Dockerfile    2022-10-07 19:50:30.000000000 +0800
+++ Jammy.Dockerfile    2022-10-07 19:52:36.000000000 +0800
@@ -1,4 +1,4 @@
-FROM mcr.microsoft.com/dotnet/aspnet:6.0-focal as builder
+FROM mcr.microsoft.com/dotnet/aspnet:6.0-jammy as builder

 ENV DEBIAN_FRONTEND=noninteractive
 ENV OPENCV_VERSION=4.6.0
@@ -21,7 +21,7 @@
       libavcodec-dev \
       libavformat-dev \
       libswscale-dev \
-      libdc1394-22-dev \
+    #   libdc1394-dev \
       libxine2-dev \
       libv4l-dev \
       libtheora-dev \
@@ -29,7 +29,7 @@
       libxvidcore-dev \
       libopencore-amrnb-dev \
       libopencore-amrwb-dev \
-      libavresample-dev \
+      libswresample-dev \
       x264 \
       libtesseract-dev \
       libgdiplus \
@@ -122,7 +122,7 @@
n0099 commented 1 year ago

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 \
jsheely commented 1 year ago

Not sure where we are with this here is where I've gotten.

Continuing using the ubuntu20.yml I was able to create a custom nupkg package which I used docker cp to get it out of the docker container

Then in my application I used dotnet add package OpenCvSharp4_.runtime.ubuntu.22.10-x64 -s $(pwd) (aka. Path to the custom nupkg) and it installed

But I still get error

Unhandled exception: 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.

The path it keeps looking for the file in is bin/Debug/net7.0/runtimes/linux-x64/nativeassets/netcoreapp/libOpenCvSharpExtern: but the nupkg installs it in ubuntu20.04/native so I'm not really sure if this ever worked. I tried copying the lib into the directory it's looking for it in but no success. Same error

I'm not sure if I'm missing something or I just don't know what I'm doing. Both are likely true

n0099 commented 1 year ago

Where is your built result libOpenCvSharpExtern.so located? You can just put it under the same path as the main executable file, they run your program with LD_LIBRARY_PATH=. ./executable

jsheely commented 1 year ago

@n0099 It's getting installed in the bin/Debug/net7.0/runtimes/ubuntu20.04-x64/native directory

Not sure I follow how that environment variable works. Will try it. I'm currently just using dotnet run

Update I copied the file into the bin/Debug/net7.0 directory and tried running the LD_LIBRARY_PATH=. dotnet run bin/debug/net7.0/app.dll but that did not work. Tried moving .so into the src directory. Did not work

jsheely commented 1 year ago

Oh I ran the ldd libOpenCvSharpExtern.so and see that libtesseract.so.4 => not found I think that has something to do with it

Update Yep all I needed was to install libtesseract-dev locally on my host machine. My build worked. Upon further reading of your DockerFile it basically did the same thing I did but in a way smarter and easier way. Both files work

Update I did have to move the libOpenCVSharpExtern.so from the ubuntu directory to the nativeassets directory though

cp bin/Debug/net7.0/runtimes/ubuntu.20.04-x64/native/libOpenCvSharpExtern.so bin/Debug/net7.0/runtimes/linux-x64/nativeassets/netcoreapp
n0099 commented 1 year ago

use LD_LIBRARY_PATH=bin/debug/net7.0 dotnet run bin/debug/net7.0/app.dll since the . refers to pwd, and your pwd is not in bin/debug/net7.0 or just cd your pwd to it. if this still fails, try export LD_LIBRARY_PATH=path; dotnet run path/app.dll

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.