Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Desktop Platform #152

Open
moshOntong-IT opened this issue Oct 6, 2023 · 11 comments
Open

[Feature] Desktop Platform #152

moshOntong-IT opened this issue Oct 6, 2023 · 11 comments

Comments

@moshOntong-IT
Copy link

Please add support for windows desktop platform

@tizian-bitschi
Copy link

tizian-bitschi commented Oct 13, 2023

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.

@moshOntong-IT
Copy link
Author

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

@tizian-bitschi
Copy link

tizian-bitschi commented Oct 13, 2023

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
Copy link

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

@CaptainDario
Copy link
Contributor

CaptainDario commented Oct 13, 2023

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
Copy link
Contributor

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

@CaptainDario
Copy link
Contributor

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

@tizian-bitschi
Copy link

@CaptainDario Wow, great stuff. Really appreciate it!

@CaptainDario
Copy link
Contributor

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

@hello112334
Copy link

hello112334 commented Aug 15, 2024

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


image

@Innuva-IT-Solutions
Copy link

Innuva-IT-Solutions commented Aug 18, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants