opencvsharp icon indicating copy to clipboard operation
opencvsharp copied to clipboard

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

Open dseferlis opened this issue 3 years ago • 19 comments

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)

dseferlis avatar May 13 '21 05:05 dseferlis

you should check dependency of native library as follows ldd ./lib/OpenCvSharpExtern.so
Do use full path of library, find all marked "not found" then install . but the best way is building OpenCvSharpExtern library on Pi 4.

AvenSun avatar May 13 '21 16:05 AvenSun

Thanks for reply. Project build produces a libOpenCvSharpExtern.so. Running ldd libOpenCvSharpExtern.so (even with full path) i get the error "not a dynamic executable". Am I missing something?

EDIT: using file libOpenCvSharpExtern.so i get: libOpenCvSharpExtern.so: ELF 64-bit LSB shared object, x86-64, version 1 (GNU/Linux), dynamically linked, BuildID[sha1]=2feb0d05276a0f30a7e6c2669610ca40685c2dca, not stripped

dseferlis avatar May 14 '21 07:05 dseferlis

@bitdeli-chef

EDIT: using file libOpenCvSharpExtern.so i get: libOpenCvSharpExtern.so: ELF 64-bit LSB shared object, x86-64, version 1 (GNU/Linux), dynamically linked, BuildID[sha1]=2feb0d05276a0f30a7e6c2669610ca40685c2dca, not stripped

It seems that libOpenCvSharpExtern.so (64 bit) you're referred is not a valid library for current OS . Is your OS 32bit ?

AvenSun avatar May 16 '21 09:05 AvenSun

Is your OS 32bit ?

Yes. It's the default for raspberry i think. At least in the official imager.

dseferlis avatar May 16 '21 19:05 dseferlis

that's the problem 32-bit OS can not invoke 64-bit library .

AvenSun avatar May 17 '21 02:05 AvenSun

So I should build library on pi i guess. Is there plans to release lib for arm32?

dseferlis avatar May 18 '21 07:05 dseferlis

If you would like to use OpenCvSharp on arm32 yes, you have to build it on your own. actually Pi 4 is 64 bit SOC, you can also try 64-bit OS.

AvenSun avatar May 18 '21 07:05 AvenSun

Thank you!!! I hadn't realize Pi can run 64bit OS...

dseferlis avatar May 18 '21 09:05 dseferlis

What I don't get is that building project for linux-arm, which produces 32bit executable, libOpenCvSharpExtern.so is included in output directory, but when building for linux-arm64, which produces 64bit executable, libOpenCvSharpExtern.so is not included.

dseferlis avatar May 18 '21 10:05 dseferlis

I can build it successfully on Jetson family (arm64). you should check the log

AvenSun avatar May 23 '21 13:05 AvenSun

libOpenCvSharpExtern.so: ELF 64-bit LSB shared object, x86-64, version 1 (GNU/Linux), dynamically linked, BuildID[sha1]=2feb0d05276a0f30a7e6c2669610ca40685c2dca, not stripped

It seems that libOpenCvSharpExtern.so (64 bit) you're referred is not a valid library for current OS .

I'd just note that the latest version of the OpenCvSharp4.runtime.linux-arm nuget 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 :)

jblam avatar Jun 23 '21 05:06 jblam

image

try this (https://github.com/GitNao/opencvsharp/commit/080b3551310429b9a2d83c83a2ee516238457751#diff-f260d80d63498fdd7fb8dab34c17d5002bdf2249912fcbd1c7af7825f81b7ba7)

image

use Pi3B+ image

libOpenCvSharpExtern.so (https://github.com/GitNao/opencvsharp/suites/3203466114/artifacts/74081383) rename .nupkg -> .zip

GitNao avatar Jul 10 '21 03:07 GitNao

libavcodec.so.57 => not found
libavformat.so.57 => not found
libavutil.so.55 => not found
libswscale.so.4 => not found

libOpenCvSharpExtern.so (https://github.com/GitNao/opencvsharp/actions/runs/1017218277)

libavcodec.so.58 => /lib/arm-linux-gnueabihf/neon/vfp/libavcodec.so.58 (0x738f4000)
libavformat.so.58 => /lib/arm-linux-gnueabihf/neon/vfp/libavformat.so.58 (0x736ca000)
libavutil.so.56 => /lib/arm-linux-gnueabihf/neon/vfp/libavutil.so.56 (0x73641000)
libswscale.so.5 => /lib/arm-linux-gnueabihf/neon/vfp/libswscale.so.5 (0x735c6000)

GitNao avatar Jul 10 '21 06:07 GitNao

@GitNao this looks great! Unfortunately I've since removed my dependency on OpenCV so I'm not in a position to verify the change.

jblam avatar Jul 16 '21 00:07 jblam

I encountered the same problem on arm64,and cross-compile this library. it took effect.

starrynightok avatar Aug 02 '21 01:08 starrynightok

I encountered the same problem on arm64, and cross-compile this library. it took effect.

Did you compile on an Apple M1?

obie73 avatar Sep 25 '21 16:09 obie73

Hello,

because there is an "arm64" nuget too, I ran into the same problem, because I would like to use the "original" raspberry os. I have to access the camera through mmal, and under 64bit debian I cannot access video frames larger than 2mp, but I have a 12mp camera attached)... I think that is because mmal(sharp) is not 100% officially supported under debian (64bit), so I would like to go back to 32bite rapbian...

I 32-bit nuget would be great, because than ir would be much mor easy to switch to a newer version.

thanks

carl

CobraCalle avatar Dec 16 '21 12:12 CobraCalle

libOpenCvSharpExtern.so: ELF 64-bit LSB shared object, x86-64, version 1 (GNU/Linux), dynamically linked, BuildID[sha1]=2feb0d05276a0f30a7e6c2669610ca40685c2dca, not stripped

It seems that libOpenCvSharpExtern.so (64 bit) you're referred is not a valid library for current OS .

I'd just note that the latest version of the OpenCvSharp4.runtime.linux-arm nuget 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 :)

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.

timstokman avatar Jan 11 '22 00:01 timstokman

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.

syamaner avatar May 18 '22 19:05 syamaner

有可供下载使用的文件吗,不想从头编译了

shenlongju avatar Oct 30 '22 13:10 shenlongju

@shenlongju the attached file I built works well on Jetson, but it's not the latest version. It should work on all arm64/aarch64 platform, you can have a try. libOpenCvSharpExtern for Jetson(arm64/aarch64)

AvenSun avatar Oct 31 '22 07:10 AvenSun

thank you, but I need arm32 version. it will run in orangepi zero , ubuntu 18.04 arm

------------------ 原始邮件 ------------------ 发件人: "shimat/opencvsharp" @.>; 发送时间: 2022年10月31日(星期一) 下午3:08 @.>; @.@.>; 主题: Re: [shimat/opencvsharp] OpenCvSharp4.runtime.linux-arm doesn't work on raspberry pi 4 (#1263)

@shenlongju the attached file I built works well on Jetson, but it's not the latest version. It should work on all arm64/aarch64 platform, you can have a try. libOpenCvSharpExtern for Jetson(arm64/aarch64)

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

shenlongju avatar Oct 31 '22 07:10 shenlongju

@shenlongju我构建的附件在 Jetson 上运行良好,但它不是最新版本。它应该适用于所有arm64 / aarch64平台,您可以尝试一下。libOpenCvSharpExtern for Jetson(arm64/aarch64)

image Raspberry arm64 tips

mimimiaomiao1 avatar Nov 30 '22 01:11 mimimiaomiao1

@OaimIsGnay you can try install it via apt install libjpeg-turbo8

AvenSun avatar Nov 30 '22 02:11 AvenSun

@OaimIsGnay you can try install it via apt install libjpeg-turbo8

hello . can't find

mimimiaomiao1 avatar Nov 30 '22 03:11 mimimiaomiao1

Can you tell me the source?

mimimiaomiao1 avatar Nov 30 '22 03:11 mimimiaomiao1

apt update first then try it again

AvenSun avatar Nov 30 '22 06:11 AvenSun

apt update first then try it again

Does not work Please give me your source address

mimimiaomiao1 avatar Nov 30 '22 09:11 mimimiaomiao1

I use this on my Raspi.

 $ ldd libOpenCvSharpExtern.so | grep jpeg
        libjpeg.so.62 => /lib/aarch64-linux-gnu/libjpeg.so.62 

https://github.com/shimat/opencvsharp/issues/1497#issuecomment-1317998861

vnetb avatar Nov 30 '22 17:11 vnetb

apt update first then try it again

Does not work Please give me your source address

https://github.com/libjpeg-turbo/libjpeg-turbo

AvenSun avatar Dec 05 '22 06:12 AvenSun