axegon / SkLite-dart

Transpile scikit-learn models to Flutter
MIT License
37 stars 14 forks source link

Unhandled Exception: type 'int' is not a subtype of type 'double' in type cast #9

Closed Istiak-Ahmed78 closed 6 months ago

Istiak-Ahmed78 commented 6 months ago

I'm encountering the error on executing predict function of svm model. Possibly predict function returning double but the function is set int as output.

 int result = svmModel.predict(input);

Error

Here is the error in console:

E/flutter (  681): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'int' is not a subtype of type 'double' in type cast
E/flutter (  681): #0      _CastListBase.[] (dart:_internal/cast.dart:99:46)
E/flutter (  681): #1      SVC.linear (package:sklite/SVM/SVM.dart:156:43)
E/flutter (  681): #2      SVC.predict (package:sklite/SVM/SVM.dart:51:17)
E/flutter (  681): #3      _ScannerScreenState.predictUsingSVM (package:ml_app/scanner_screen.dart:55:27)
E/flutter (  681): <asynchronous suspension>
E/flutter (  681): 

Some related methods related to the issue:

class ImageUtils{
  static Future<List<double>?> convertImageToData(File imageFile) async {
    final bytes = await imageFile.readAsBytes();
    image_lib.Image? image = image_lib.decodeImage(bytes);
    if (image == null) return null;

    // Convert Image to list of RGB values
    List<double> rgbValues = image.getBytes(format: image_lib.Format.rgb).toList().cast<double>();

    // Convert to (1, 67500) list
    List<List<double>> reshapedData = [rgbValues];

    return rgbValues;
  }
}
  Future<void> predictUsingSVM(File imageFile) async {
    String modelData = await loadModel('assets/peddy_model.json');
    List<double>? input = await ImageUtils.convertImageToData(imageFile);
    if (input == null) {
      print('Something is wrong with image resizing');
      return;
    }
    final svmModel = SVC.fromMap(json.decode(modelData));
    int result = svmModel.predict(input);
    print(result);
  }

How to solve this? Help me please