tensorflow / flutter-tflite

Apache License 2.0
554 stars 130 forks source link

[Feature] Desktop Platform #152

Open moshOntong-IT opened 1 year ago

moshOntong-IT commented 1 year ago

Please add support for windows desktop platform

tizian-bitschi commented 1 year ago

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 commented 1 year ago

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

tizian-bitschi commented 1 year ago

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.

  4. 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 commented 1 year ago

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

CaptainDario commented 1 year ago

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 commented 1 year ago

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

CaptainDario commented 1 year ago

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

tizian-bitschi commented 1 year ago

@CaptainDario Wow, great stuff. Really appreciate it!

CaptainDario commented 11 months ago

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

hello112334 commented 3 months ago

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


image

Innuva-IT-Solutions commented 3 months ago

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?