We’re building dotnet 6.x applications and are attempting to use the OpenCVSharp nuget package which wraps the native OpenCV libraries.
Of course, ideally, we would build a specific version of these libraries for NixOS 22.05, but wanted to see if we could get a basic example working with the packages that are currently available.
More specifically, we’re using OpenCVSharp4/4.6.0.20220608 and OpenCVSharp4.runtime.ubuntu.18.04-x64/4.6.0.20220608 nuget packages.
To test, generate a console app w/ opencvsharp deps
dotnet new console -n ConsoleApp01
cd ConsoleApp01
dotnet add package OpenCvSharp4
dotnet add package OpenCvSharp4.runtime.ubuntu.18.04-x64
Replace the contents of Program.cs with
using OpenCvSharp;
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
// Create a OpenCVSharp Mat type; See exception.
var mat = new Mat();
Run the app
dotnet run
Thrown exception
Hello, World!
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. 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.Internal.NativeMethods.redirectError(CvErrorCallback errCallback, IntPtr userdata, IntPtr& prevUserdata)
at OpenCvSharp.Internal.ExceptionHandler.RegisterExceptionCallback()
at OpenCvSharp.Internal.NativeMethods.LoadLibraries(IEnumerable`1 additionalPaths)
at OpenCvSharp.Internal.NativeMethods..cctor()
--- End of inner exception stack trace ---
at OpenCvSharp.Internal.NativeMethods.core_Mat_new1(IntPtr& returnValue)
at OpenCvSharp.Mat..ctor()
at Program.<Main>$(String[] args) in /home/talos/projects/opencvsharp-hello-world/ConsoleApp01/Program.cs:line 7
Check for missing dependencies
ldd $HOME/.nuget/packages/opencvsharp4.runtime.ubuntu.18.04-x64/4.6.0.20220608/runtimes/ubuntu.18.04-x64/native/libOpenCvSharpExtern.so
libcairo.so.2 => not found
libgdk_pixbuf-2.0.so.0 => not found
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 => not found
libpng16.so.16 => not found
libIlmImf-2_2.so.22 => not found
Flake.nix (note, the inclusion of the pkgs.ffmpeg_5 package).
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.05";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils}:
flake-utils.lib.eachDefaultSystem (system:
let pkgs = nixpkgs.legacyPackages.${system}; in
rec {
lib = pkgs.lib;
devShell = pkgs.mkShell rec
{
name ="OpenCV DevEnv";
buildInputs = with pkgs; [
# Tooling
pkgs.dotnet-sdk
# OpenCV dependencies
pkgs.libgeotiff
pkgs.xorg.libXt
pkgs.libusb1
pkgs.tesseract4
pkgs.libtiff
pkgs.stdenv.cc.cc.lib
pkgs.gtk2
pkgs.libpng12
pkgs.ffmpeg_5
];
};
});
}
We’ve tried a variety of different ffmpeg packages, but got tripped up on the same issue.