1

I'm trying to do image classification in Flutter using tflite_flutter package, the code is from tflite_flutter github but I got this error

E/Parcel  (29971): Reading a NULL string not supported here.
I/flutter (29971): afah aa
I/flutter (29971): afah ab
E/flutter (29971): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'List<double>' is not a subtype of type 'List<int>' of 'value'
E/flutter (29971): #0      List.[]= (dart:core-patch/growable_array.dart)
E/flutter (29971): #1      Tensor._duplicateList (package:tflite_flutter/src/tensor.dart:236:10)
E/flutter (29971): #2      Tensor.copyTo (package:tflite_flutter/src/tensor.dart:202:7)
E/flutter (29971): #3      Interpreter.runForMultipleInputs (package:tflite_flutter/src/interpreter.dart:183:24)
E/flutter (29971): #4      Interpreter.run (package:tflite_flutter/src/interpreter.dart:172:5)
E/flutter (29971): #5      _HomePage.runInference (package:thesis_app/homepage.dart:119:17)
E/flutter (29971): #6      _HomePage.processImage (package:thesis_app/homepage.dart:104:7)
E/flutter (29971): #7      _HomePage.openGallery.<anonymous closure> (package:thesis_app/homepage.dart:282:7)
E/flutter (29971): #8      State.setState (package:flutter/src/widgets/framework.dart:1139:30)
E/flutter (29971): #9      _HomePage.openGallery (package:thesis_app/homepage.dart:281:5)
E/flutter (29971): <asynchronous suspension>

I tried to print the function and the error was in interpreter.run, I just have no idea how to solve this. I already searched everywhere tried to cast the input to as List<List<List<int>>> and I got this

E/flutter (29971): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'List<List<List<List<num>>>>' is not a subtype of type 'List<List<List<int>>>' in type cast

Here's the code:

// Load model
Future<void> loadModel() async {
  final options = InterpreterOptions();

  // Use XNNPACK Delegate
  if (Platform.isAndroid) {
    options.addDelegate(XNNPackDelegate());
  }

  if (Platform.isIOS) {
    options.addDelegate(GpuDelegate());
  }

  // Load model from assets
  interpreter = await Interpreter.fromAsset(modelPath, options: options);
  // Get tensor input shape [1, 224, 224, 3]
  inputTensor = interpreter.getInputTensors().first;
  // Get tensor output shape [1, 1001]
  outputTensor = interpreter.getOutputTensors().first;
  setState(() {});

  log('Interpreter loaded successfully');
}

// Load labels from assets
Future<void> loadLabels() async {
  final labelTxt = await rootBundle.loadString(labelsPath);
  labels = labelTxt.split('\n');
}

Future<void> processImage() async {
  if (imagePath != null) {
    // Read image bytes from file
    final imageData = File(imagePath!).readAsBytesSync();

    // Decode image using package:image/image.dart (https://pub.dev/image)
    image = img.decodeImage(imageData);
    setState(() {});

    // Resize image for model input (Mobilenet use [224, 224])
    final imageInput = img.copyResize(
      image!,
      width: 224,
      height: 224,
    );

    // Get image matrix representation [224, 224, 3]
    final imageMatrix = List.generate(
      imageInput.height,
      (y) => List.generate(
        imageInput.width,
        (x) {
          final pixel = imageInput.getPixel(x, y);
          return [pixel.r, pixel.g, pixel.b];
        },
      ),
    );

    // Run model inference
    runInference(imageMatrix);
  }
}

// Run inference
Future<void> runInference(
  List<List<List<num>>> imageMatrix,
) async {
  print("afah aa");
  // Set tensor input [1, 224, 224, 3]
  final input = [imageMatrix];
  // Set tensor output [1, 1001]
  final output = [List<int>.filled(6, 1)];
  print("afah ab");
  // Run inference
  interpreter.run(input, output);
  print("afah ac");
  // Get first output tensor
  final result = output.first;
  print("afah ad");
  // Set classification map {label: points}
  classification = <String, int>{};

  print("afah af");

  for (var i = 0; i < result.length; i++) {
    if (result[i] != 0) {
      // Set label: points
      classification![labels[i]] = result[i];
    }
  }

  setState(() {});
}

Can someone help me with this?(:

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Ali Mahfud
  • 43
  • 4

0 Answers0