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 18.04 #889

Closed alexdr488 closed 2 years ago

alexdr488 commented 4 years ago

Summary of your issue

When I run any application (with code that runs on Windows) with OpenCvSharp 4, I get an error "System.DllNotFoundException: Unable to load shared library "OpenCvSharpExtern". I tried to put different versions of packages, but the problem remained. How do I run a project on Ubuntu? I have not found a working solution for this operating system.

Environment

C#, Ubuntu 18.04.4-x64 Installed packages (nuget): OpenCvSharp4 OpenCvSharp4.runtime.ubuntu.18.04-x64

Example code:

class Program
    {
        static void Main(string[] args)
        {
            Mat src = new Mat("1.jpg", ImreadModes.Grayscale);
            Mat dst = new Mat();

            Cv2.Canny(src, dst, 50, 200);
            using (new Window("src image", src)) 
            using (new Window("dst image", dst)) 
            {
                Cv2.WaitKey();
            }
        }
    }

Output:

Unhandled exception. System.TypeInitializationException: The type initializer for 'OpenCvSharp.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
   at OpenCvSharp.NativeMethods.redirectError(CvErrorCallback errCallback, IntPtr userdata, IntPtr& prevUserdata)
   at OpenCvSharp.ExceptionHandler.RegisterExceptionCallback()
   at OpenCvSharp.NativeMethods.LoadLibraries(IEnumerable`1 additionalPaths)
   at OpenCvSharp.NativeMethods..cctor()
   --- End of inner exception stack trace ---
shimat commented 4 years ago

Please try using ldd to check dependencies of the libOpenCvSharpExtern.so and refer to the CI script for necessary packages. https://github.com/shimat/opencvsharp/blob/master/.github/workflows/ubuntu18.yml#L25

SuryaKumarSE commented 4 years ago

Hello @shimat

I have faced the same issue which was mentioned above “Unable to load shared library OpenCvSharpExtern, Ubuntu 18.04” and I have tried adding all the set of dependencies which you have referenced in the link below: https://github.com/shimat/opencvsharp/blob/master/.github/workflows/ubuntu18.yml#L25

But when I try checking the dependency of the libOpenCvSharpExtern.so using ldd command, I can able to still find some of the dependency is being missed as shown below:

libavcodec.so.57 => not found libavformat.so.57 => not found libavutil.so.55 => not found libswscale.so.4 => not found libjpeg.so.8 => not found

Note: I have installed and checking this assemblies in docker container which is being hosted in Google Kubernetes.

Environment : C#, Ubuntu 18.04.4-x64 Installed packages (nuget): OpenCvSharp4 OpenCvSharp4.runtime.ubuntu.18.04-x64

Kindly let me know your suggestions for the same.

ffrankozz commented 4 years ago

Hello @SuryaKumarSE Any chance of solution so far for your problem? I have bumped into the same problem after dockerizing and trying to find out a solution. It seems @shimat is not in a position to reply.

makejack commented 4 years ago

add export LD_LIBRARY_PATH="/lib:/usr/lib:/usr/local/lib" environment variables and restart the system

arialblack14 commented 3 years ago

@makejack that did not work for me. Any update on this?

marianmelnychuk commented 3 years ago

@arialblack14 I have the same problem. Did you find solution?

arialblack14 commented 3 years ago

@marianmelnychuk No, sorry :(

makejack commented 3 years ago

@arialblack14 I set the environment variable after installing OpenCv. you can use the "ldd" command to detect the dependency of libOpenCvSharpExtern.so, use the "apt-file" command to search for “not found” dependency , install the searched dependency .

stale[bot] commented 2 years 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.

nietras commented 2 years ago

It would be really great if the Linux so file had all dependencies linked in statically like for windows or perhaps have it as a separate nuget package (OpenCvSharp4.runtime.ubuntu.18.04-x64-static) so it can be used without having to install lots of deps, hence so it works like on windows.

mburumaxwell commented 2 years ago

@nietras ,

I figured out a way to get the necessary bits into a docker image without having to build the entire image, with long C++ builds, every time. Maybe this can help you or anyone else on this thread. Unfortunately, I don't think it is possible to place them in a NuGet package like for Windows because it would require about 800MiB in size for ubuntu 18.04

In the Dockerfile add the following lines

RUN apt-get update && apt-get install -y --no-install-recommends libexpat1
COPY --from=shimat/ubuntu20-dotnet6-opencv4.5.3 /usr/lib /usr/lib
COPY --from=shimat/ubuntu20-dotnet6-opencv4.5.3 /lib /lib

A complete Dockerfile (based on the one generated by Visual Studio) would look like this:

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:6.0-bullseye-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
RUN apt-get update && apt-get install -y --no-install-recommends libexpat1
COPY --from=ubuntu20-dotnet6-opencv4.5.3 /usr/lib /usr/lib
COPY --from=ubuntu20-dotnet6-opencv4.5.3 /lib /lib

FROM mcr.microsoft.com/dotnet/sdk:6.0-bullseye-slim AS build
WORKDIR /src
COPY ["MyProject/MyProject.csproj", "src/MyProject/"]
RUN dotnet restore "src/MyProject/MyProject.csproj"
COPY . .
WORKDIR "/src/MyProject"
RUN dotnet build "MyProject.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "MyProject.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyProject.dll"]

In my case, I built the 4.6 image into a private container registry hence

RUN apt-get update && apt-get install -y --no-install-recommends libexpat1
COPY --from=myregistry.azurecr.io/opencvsharp:4.6.0 /usr/lib /usr/lib
COPY --from=myregistry.azurecr.io/opencvsharp:4.6.0 /lib /lib

@shimat , maybe you could have this in the documentation as an alternative to having to build an image from source?

shimat commented 2 years ago

I actually run my company's systems that way. However, I do not guarantee the maintenance of my container images (shimat/ubuntu20...) in Docker Hub (I may remove them suddenly one day),

I recommend that users themselves register OpenCvSharp container images with a container registry such as Docker Hub, Amazon ECR, Google Container Registry, etc.