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 6 years ago
- Comments: 33 (10 by maintainers)
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.
Noticed the same thing. They do have a workflow for linux-arm, I wonder what is going on there: https://github.com/shimat/opencvsharp/blob/master/.github/workflows/linux-arm.yml. Might be it’s just attaching the wrong artifact to the linux-arm package. It’s building the 32 bits version for arm, I’d expect a 64 bits version somewhere as well, but I can only see I arm64 cuda version that seems a little behind version-wise.
I’d just note that the latest version of the
OpenCvSharp4.runtime.linux-armnuget package (currently 4.5.2.20210507) includes the x84-64 binary, not an ARM binary. So the package is guaranteed never to work, as I understand it.I’d suggest unlisting that package, to avoid having other people encounter this problem and leave comments on this issue 😃