Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'OpenCvSharp.NativeMethods' threw an exception. Unable to load shared library 'OpenCvSharpExtern' or one of its dependencies., Ubuntu 18.04

I have used the OpenCvSharp library in my .net core 3.0 application when running this application in the windows environment, the application is running without any issues. When the same application is deployed and hosted in the docker running in ubuntu 18.04 the following exception is occurred:

System.DllNotFoundException: Unable to load shared library 'OpenCvSharpExtern' or one of its dependencies.

I have installed the following packages (nuget) which are required for running the OpenCvSharp4 in the ubuntu environment:

  • OpenCvSharp4
  • OpenCvSharp4.runtime.ubuntu.18.04-x64

When Checked the dependency of the libOpenCvSharpExtern.so library using the following command:

ldd libOpenCvSharpExtern.so

Some of the dependency assemblies was missing, so I have installed all the necessary packages which is mentioned in the below GitHub thread which reports the same exact issue:

https://github.com/shimat/opencvsharp/issues/889

But still the some of the dependencies are found to be missing and those assemblies name are given 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

I have also tried installing the following packages “libjpeg62-turbo-dev” and “libavcodec-extra57” for fixing the ‘not found’ assemblies. But still the issue occurred.

Any suggestion on how to fix this issue would be appreciated.

like image 974
Surya Kumar Avatar asked Mar 31 '20 11:03

Surya Kumar


1 Answers

We had the same issue. After a lot of experimentation we got it working with the following setup (Docker):

csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>netstandard2.0;net472</TargetFrameworks>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="OpenCvSharp4" Version="4.4.0.20200725" />
    <PackageReference Include="OpenCvSharp4.runtime.debian.10-amd64" Version="4.3.0.20200424" />
    <PackageReference Include="OpenCvSharp4.runtime.win" Version="4.4.0.20200725" />
    <PackageReference Include="System.Drawing.Common" Version="4.7.0" />
  </ItemGroup>
</Project>

dockerfile

[...]

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
[...]

RUN apt-get update && apt-get install -y \
    apt-utils \
    libgdiplus \
    libc6-dev \
    libgtk2.0-dev \
    libtbb-dev \
    libatlas-base-dev \
    libvorbis-dev \
    libxvidcore-dev \
    libopencore-amrnb-dev \
    libopencore-amrwb-dev \
    libavresample-dev \
    x264 \
    v4l-utils \
    libwebp-dev \
    tesseract-ocr \
    libtesseract-dev \
    libleptonica-dev \
    libtiff-dev \
    libavcodec-dev \
    libavformat-dev \
    libswscale-dev \
    libdc1394-22-dev \
    libxine2-dev \
    libv4l-dev
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

[...]

Unfortunately, I'm not entirely sure which of the many apt packages did the trick, I'm just happy it works now ;)

like image 134
Hannes Sachsenhofer Avatar answered Nov 06 '22 04:11

Hannes Sachsenhofer