shimat / opencvsharp

OpenCV wrapper for .NET
Apache License 2.0
5.4k stars 1.15k forks source link

FEATURE - OpenCvSharp4.runtime.osx for recent version of OSX #1528

Open mramundo opened 1 year ago

mramundo commented 1 year ago

Hey @shimat hi!

I'm working on a .NET 6 UI cross-platform application with Avalonia and I'm using OpenCVSharp to do some really basic image/video processing.

Are you planning to update OpenCvSharp4 runtimes fo most recent versions of OSX? If yes, can we have an ETA?

Many thanks, Mirco

jeremyrumpf commented 1 year ago

I was having an issue on OSX 11.6.5 trying to run a DNN model (YOLOv5). Prototype code in python worked fine, but the same in OpenCvSharp4 did not. I traced it down to the extern runtime. It looks like a lot of the OpenCv runtime is statically linked into the libOpenCvSharpExtern.dylib and I think those OpenCV libs were too old.

I built a dynamically linked libOpenCvSharpExtern.dylib that was linked to a brew OpenCV install in /usr/local. I then removed the OSX runtime nuget dependency and simply had the build copy the compiled libOpenCvSharpExtern.dylib to the project output location. The managed OpenCvSharp4 found the extern in the base output location . This solved my issue of being able to use a more recent OpenCV release in OSX. The python code and the OpenCvSharp4 code now produce the same output (they are both referencing the same OpenCV installation with dynamic linking).

mramundo commented 1 year ago

Hey @jeremyrumpf,

I'm having some issues with libOpenCvSharpExtern.dylib on Macbooks that doesn't have opencv installed with brew. I think that's a dependencies problem and I wasn't able to track down missing dependencies.

You dinamically linked lib is an all-in-one dylib or still needs dependencies from the /usr/local target system?

What I'm trying to achieve is to have a single dylib dependecy added to my project and shipped within the build on target systems, without the need to install some dependecy on the target system side.

Sorry but I'm not very familiar with this type of problems, so I've probably written some inaccuracies.

Thanks!

jeremyrumpf commented 1 year ago

Mine was dynamically linked to OpenCV in /usr/local. It required the dependencies in /usr/local to be there.

It seems you want an all-in-one dylib like what @shimat originally supplied. I would of preferred to build my solution as an all-in-one as well, but the workflow on how @shimat accomplished that wasn't entirely clear.

shimat commented 1 year ago

Below is the GitHub Actions workflow definition that was once used to build OpenCvSharp4.runtime.osx. Sorry, but due to my limited spare capacity, I have discontinued support for this. https://github.com/shimat/opencvsharp/blob/master/.github/workflows/macos10.yml.disabled

jeremyrumpf commented 1 year ago

Using shimat's workflow, I was able to successfully build a static libOpenCvSharpExtern.dylib :

This was built against OpenCv 4.7.0 MacOS 11.7.6. The resulting file size is much bigger than the previously dynamic one:

-rwxr-xr-x   1 jrumpf wheel  63278040 Jun  6 17:13 libOpenCvSharpExtern.dylib
-rwxr-xr-x   1 jrumpf wheel   2955344 Feb  2 10:31 libOpenCvSharpExtern.dylib.dynamic

I have not tested it, so YMMV....

mramundo commented 1 year ago

Hey @jeremyrumpf, I tested your library but unfortunately it seems that it's not completely static, as it still searches for references for some libraries on the target system where it's executed.

I tested @shimat's workflow, changing the runner image to macos-13. Somehow the action works correctly (see the results), but the builded libOpenCvSharpExtern.dylib contained in the nupkg isn't static either.

So I created a project that contains similar commands from the action, and tried to build the library locally.

I checked the local builded library with otool and the only dependencies needed from target's system are system libraries.

Here's the otool's output, comparing my builded local library and yours: otool-libOpenCvSharpExtern

Then I tried to use the builded dylib in my projects and It's working!

I created a repository with all the scripts and a README for the instructions, here's the link: opencvsharpextern.

This is the library I builded and tested in my project: libOpenCvSharpExtern.dylib

To include the newly builded dylib in my project's publish directory I wrote an AfterPublish action in my csproj that copies the dylib in publish directory root.

I hope this helps someone

jeremyrumpf commented 1 year ago

Yea, looks like mine pulled in the OpenCV libs as static, but all the others remained dynamic (ffmpeg, etc). Your's certainly looks mint. This is excellent, thank you!

stale[bot] commented 10 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.