flutter-tflite icon indicating copy to clipboard operation
flutter-tflite copied to clipboard

[Feature] Desktop Platform

Open moshOntong-IT opened this issue 2 years ago • 11 comments

Please add support for windows desktop platform

moshOntong-IT avatar Oct 06 '23 12:10 moshOntong-IT

This is fairly easy to achieve. Follow this tutorial here: https://www.tensorflow.org/lite/guide/build_cmake BUT instead of just using the command cmake ../tensorflow_src/tensorflow/lite/c, use it with flags cmake -DCMAKE_WINDOWS_EXPORT_ALL_SYMBOLS=TRUE -DCMAKE_BUILD_TYPE=Debug ..\tensorflow\tensorflow\lite\c. After running cmake --build . -j the .dll has exported symbols and can be loaded as a dynamic library in flutter.

tizian-bitschi avatar Oct 13 '23 09:10 tizian-bitschi

how to load the dll? I am not familiar with C

moshOntong-IT avatar Oct 13 '23 09:10 moshOntong-IT

I guess somebody has to properly include it in the project but i can give you a very very instable hack, that loads it for windows:

  1. Find, where the tflite_flutter package is located. You can do this in VSC by importing it in a dart-file and then CTRL + Right Click on the package:tflite_flutter/tflite_flutter.dart. In the upper part you can now see the path to the installed location of the package.
  2. In this path, click on tflite_flutter-0.10.3 (or whatever version, but i did it for that version). Now a small file browser opens. Open the tflite folder, then lib > src > bindings > bindings.dart
  3. Modify the final DynamicLibrary _dylib:
final DynamicLibrary _dylib = () {
  if (Platform.isAndroid) {
    return DynamicLibrary.open('libtensorflowlite_jni.so');
  }

  if (Platform.isIOS) {
    return DynamicLibrary.process();
  }

  if (Platform.isMacOS) {
    return DynamicLibrary.open(
        '${Directory(Platform.resolvedExecutable).parent.parent.path}/resources/libtensorflowlite_c-mac.dylib');
  }

  if (Platform.isWindows) {
    return DynamicLibrary.open(
        '${Directory.current.path}/windows/tensorflowlite_c.dll');
  }

  throw UnsupportedError('Unknown platform: ${Platform.operatingSystem}');
}();

As you can see if the platform is Windows, it searches for the dll in the windows folder of your flutter project. So you have to place the built dll in your flutter app > windows > tensorflowlite_c.dll.

  1. Now you should be able to Import and initialize the Interpreter from the tflite_flutter package as shown here.

If you are not able to build the dll by yourself i can provide one that i built by myself.

tizian-bitschi avatar Oct 13 '23 11:10 tizian-bitschi

@PaulTR @CaptainDario I see you are the top contributors recently, maybe you know how this can be added to the package easily?

tizian-bitschi avatar Oct 13 '23 11:10 tizian-bitschi

I will work on this once I am back from vacation. It's technically not that difficult but takes some time.

While the hack that @tizian-bitschi outlines will work during development, I guess it will fail once packaged as msix.

CaptainDario avatar Oct 13 '23 12:10 CaptainDario

@tizian-bitschi Progress for linux support is tracked in #162

CaptainDario avatar Oct 22 '23 18:10 CaptainDario

@tizian-bitschi Progress for Windows support is tracked in #164

CaptainDario avatar Oct 23 '23 20:10 CaptainDario

@CaptainDario Wow, great stuff. Really appreciate it!

tizian-bitschi avatar Oct 23 '23 20:10 tizian-bitschi

@tizian-bitschi @moshOntong-IT this is done and available in the latest version.

CaptainDario avatar Dec 17 '23 23:12 CaptainDario

I refer this as well. https://pub.dev/packages/tflite_flutter


image

hello112334 avatar Aug 15 '24 02:08 hello112334

I encounter this error:

"flutter: Error loading Blazeface model: Invalid argument(s): Failed to lookup symbol 'TfLiteModelCreate': error code 127"

and this is my code:

Future<void> loadModel() async {
    try {
      if (Platform.isWindows) {
        final executableDir = Directory.current.path;
        final path = '$executableDir/blobs/libtensorflowlite_c-win.dll';
        DynamicLibrary.open(path);

      }
      _interpreter = await Interpreter.fromAsset('assets/face_detection_back.tflite');
    } catch (e) {
      print('Error loading Blazeface model: $e');
      // Handle model loading error
       }
 }

I used this command to generate the .dll file

bazel build //tensorflow/lite:libtensorflowlite_c.dll --config=windows

any help please?

Innuva-IT-Solutions avatar Aug 18 '24 16:08 Innuva-IT-Solutions