am15h / tflite_flutter_plugin

TensorFlow Lite Flutter Plugin
https://pub.dev/packages/tflite_flutter
Apache License 2.0
499 stars 352 forks source link

MacOS Incorrectly read static libraries with .so suffix #199

Open ash0080 opened 2 years ago

ash0080 commented 2 years ago

https://github.com/am15h/tflite_flutter_plugin/blob/659e6d5fbf95506f89cf487ae3e355dd4ff7bc2a/lib/src/bindings/dlib.dart#L12

[ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: Invalid argument(s): Failed to load dynamic library '~/Library/Containers/com.example.dogCat/Data/blobs/libtensorflowlite_c-mac.so': dlopen(~/Library/Containers/com.example.dogCat/Data/blobs/libtensorflowlite_c-mac.so, 0x0001): tried: '~/Library/Containers/com.example.dogCat/Data/blobs/libtensorflowlite_c-mac.so' (no such file)
#0      _open (dart:ffi-patch/ffi_dynamic_library_patch.dart:12:43)
#1      new DynamicLibrary.open (dart:ffi-patch/ffi_dynamic_library_patch.dart:23:12)
#2      tflitelib.<anonymous closure> (package:tflite_flutter/src/bindings/dlib.dart:39:27)
#3      tflitelib (package:tflite_flutter/src/bindings/dlib.dart:41:2)
#4      tflitelib (package:tflite_flutter/src/bindings/dlib.dart)
#5      tfLiteModelCreateFromBuffer (package:tflite_flutter/src/bindings/model.dart:10:35)
#6      tfLiteModelCreateFromBuffer (package:tflite_flutter/src/bindings/model.dart)
#7      new Model.fromBuffer (package:tflite_flutter/src/model.dart:36:19)
ash0080 commented 2 years ago

also, blobs/ is not packaged in build https://github.com/am15h/tflite_flutter_plugin/issues/181

CaptainDario commented 2 years ago

@ash0080 I have a fix for this and will open a pull request soon.

walsha2 commented 1 year ago

@CaptainDario any update on this?

@ash0080 do you have a workaround? Just modify the source or is the blob packaging issue in #181 still a blocker?

CaptainDario commented 1 year ago

@walsha2 I found a way to make it work and forgot to open the PR.

I will take a look what I did and update here.

But it is related to the way the dynamic library is loaded on macos. You need to check that it is actually trying to load from the path where the library is located.

walsha2 commented 1 year ago

@CaptainDario I actually got this to work by forking. First, the change to dlib.dart noted above is def a typo.

The else if (Platform.isMacOS) line should be the following. I can open an PR for that.

ext = 'dylib';

Second, #181 is also a problem and the easier way to "resolve" that in the interim was to simply copy the files manually into the expected location. This should be part of the build step as you noted in #181

cd <PROJECT ROOT>
cp -r blobs/ <PROJECT ROOT>/build/macos/Build/Products/Debug/flutter.app/Contents/MacOS/blobs

The second part is not a real solution, so if you have an actually solution please do open a PR and note it in #181.

Regarding this plugin, this is my first time using it, but it looks super useful and worked great for my use case. The code traffic and maintainer feedback on PR/issues seems like it has stagnated? Is this package still maintained?

FYI @am15h - please feel free to chime in. Thanks!

CaptainDario commented 1 year ago

@walsha2 take a look at my fork. For MacOS you need to add the dynamic library in Xcode to your project's resources. This will lead to the library being available

Directory(Platform.resolvedExecutable).parent.parent.path + '/resources/libtensorflowlite_c-mac.dylib'

For Linux and Windows you need to adapt the CMakeLists.txt (of your project not TF Lite plugin), at the end add (my project windows and lin)

install(
  FILES ${PROJECT_BUILD_DIR}/../blobs/libtensorflowlite_c-win.dll 
  DESTINATION ${INSTALL_BUNDLE_DATA_DIR}/../blobs/
)

this will result in the lib being available in the blobs folder. However, this solution is outside of this plugin that's, why I didn't open a PR. I think there is a way to include the blobs-dir in a better way...

I am following this repo already for ~1 1/2 years and there is nearly no activity, so do not expect an answer from the author. However, if this repo gains traction again, there are quite a few ways to make this more user-friendly. For example, I adapted this package so it auto downloads the libs on iOS / Android. If you are interested take a look at my fork.

walsha2 commented 1 year ago

...so do not expect an answer from the author

Well that's sad to hear. Thanks for following up, I will definitely take a look at your fork. Hopefully we can revive this plugin, or at least get the maintainers to open it up to other people to maintain.

@am15h is that something we can do? It is 100% understandable if the original maintainers no longer have the time to support the plugin. Can we open the package to other maintainers? tflite_flutter already has 250+ likes on pub.dev. It would be better to keep this package up to date rather than create clones with similar names and confuse users.