loadModel method

  1. @override
Future<int?> loadModel()
override

Load the whole model by folder.

Implementation

@override
Future<int?> loadModel() async {
  final directory = await getApplicationDocumentsDirectory();

  String modelPath = 'packages/flutter_ocr_sdk/lib/model/';

  bool isDesktop = false;
  if (Platform.isWindows || Platform.isLinux) {
    isDesktop = true;
  }

  int? ret = 0;
  var fileNames = ["MRZ"];
  for (var i = 0; i < fileNames.length; i++) {
    var fileName = fileNames[i];

    var prototxtName = '$fileName.prototxt';
    var prototxtBufferPath = join(modelPath, prototxtName);
    ByteData prototxtBuffer = await loadAssetBytes(prototxtBufferPath);

    var txtBufferName = '$fileName.txt';
    var txtBufferPath = join(modelPath, txtBufferName);
    ByteData txtBuffer = await loadAssetBytes(txtBufferPath);

    var characterModelName = '$fileName.caffemodel';
    var characterModelBufferPath = join(modelPath, characterModelName);
    ByteData characterModelBuffer =
        await loadAssetBytes(characterModelBufferPath);

    if (isDesktop) {
      List<int> bytes = prototxtBuffer.buffer.asUint8List();
      await File(join(directory.path, prototxtName)).writeAsBytes(bytes);

      bytes = txtBuffer.buffer.asUint8List();
      await File(join(directory.path, txtBufferName)).writeAsBytes(bytes);

      bytes = characterModelBuffer.buffer.asUint8List();
      await File(join(directory.path, characterModelName))
          .writeAsBytes(bytes);
    } else {
      loadModelFiles(
          fileName,
          prototxtBuffer.buffer.asUint8List(),
          txtBuffer.buffer.asUint8List(),
          characterModelBuffer.buffer.asUint8List());
    }
  }

  var templateName = 'MRZ.json';
  var templatePath = join(modelPath, templateName);
  String template = await loadAssetString(templatePath);
  if (isDesktop) {
    var templateMap = json.decode(template);
    templateMap['CharacterModelArray'][0]['DirectoryPath'] = directory.path;

    ByteData templateBuffer = await loadAssetBytes(templatePath);
    List<int> bytes = templateBuffer.buffer.asUint8List();
    await File(join(directory.path, templateName)).writeAsBytes(bytes);
    await methodChannel.invokeMethod('loadModel',
        {'path': directory.path, 'template': json.encode(templateMap)});
  } else {
    ret = await loadTemplate(template);
  }

  return ret;
}