Unable to load shared library OpenCvSharpExtern, Ubuntu 18.04

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
SuryaKumarSEcommented, Mar 30, 2020

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.

1reaction
mburumaxwellcommented, Oct 12, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

'OpenCvSharp.NativeMethods' threw an exception. Unable ...
'OpenCvSharp.NativeMethods' threw an exception. Unable to load shared library 'OpenCvSharpExtern' or one of its dependencies., Ubuntu 18.04.
Read more >
OpenCvSharp.NativeMethods' threw an exception. Unable to ...
[Solved]-'OpenCvSharp.NativeMethods' threw an exception. Unable to load shared library 'OpenCvSharpExtern' or one of its dependencies., Ubuntu 18.04-docker.
Read more >
[Solved]-'OpenCvSharp.NativeMethods' threw an exception. Unable ...
NativeMethods' threw an exception. Unable to load shared library 'OpenCvSharpExtern' or one of its dependencies., Ubuntu 18.04-Opencv.
Read more >
Where/How do I find missing ffmpeg dependencies?
DllNotFoundException: Unable to load shared library 'OpenCvSharpExtern' or one of its dependencies. In order to help diagnose loading ...
Read more >
Unable to load shared library 'OpenCvSharpExtern' or one ...
Unable to load shared library 'OpenCvSharpExtern' or one of its dependencies. In order to help diagnose loading problems, consider setting ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found