opencvsharp: OpenCvSharp4.runtime.linux-arm doesn't work on raspberry pi 4

Summary of issue

Hello everyone. First of all thank you for your effort to make this lib. Trying to use opencvsharp on an image capture app on a raspberry pi. Installed the OpenCvSharp4.runtime.linux-arm nuget package thinking of this is what I need. Although I get the error bellow. Is there something I can do to make it work?

Environment

Raspberry pi model 4 4GB OS: Raspbian 10 buster (armv7l) Project framework: .net 5

Example code:

using OpenCvSharp;

namespace OpencvTest.Console
{
    class Program
    {
        static void Main(string[] args) {
            var capture = new VideoCapture(0);
            var window = new Window("Test");
            var image = new Mat();
            while (true) {
                capture.Read(image);
                if (image.Empty()) {
                    break;
                }
                
                window.ShowImage(image);
                if (Cv2.WaitKey(1) == 113) {
                    break;
                }
            }
            System.Console.WriteLine("Hello World!");
        }
    }
}

Output:

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: wrong ELF class: ELFCLASS64
   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.videoio_VideoCapture_new3(Int32 device, Int32 apiPreference, IntPtr& returnValue)
   at OpenCvSharp.VideoCapture..ctor(Int32 index, VideoCaptureAPIs apiPreference)
   at OpencvTest.Console.Program.Main(String[] args)

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 31 (10 by maintainers)

Most upvoted comments

A bit late to the discussion but recently I experienced a similar issue too. If using Docker to run on Raspberry Pi, building OpenCV and OpenCvSharp as a multi architecture Docker build and then copy the native libraries to the target image when building the application.

Here is an example: https://github.com/syamaner/docker-multi-arch-opencvsharp/tree/main/docker And a bit more in the post: https://dev.to/syamaner/docker-multi-architecture-net-60-and-opencvsharp-1okd

Essentially all it does is to build both libraries, copy libOpenCvSharpExtern.so and all dependencies to /artifacts directory. Then when building an application image, use it as a stage to copy contents of the /artifacts into the target image. the build image is only 94 to 125 mb depending on target architecture:

https://hub.docker.com/repository/docker/syamaner/opencvsharp-build/tags

This way I only need to add OpenCvSharp4 as a package reference and native bindings will be handled at Docker build time when building the application image. I have tested using 32 and 64 bit OS on Raspberry pi including official Rabpbian as well as Ubuntu 20.04 and 22.04.

Hope it helps.