The flutter plugin for image-to-image-transformation using PyTorch-mobile (android) and CoreML (ios)
Add lines like below under dependencies
in your package's pubspec.yaml and run flutter pub get
.
dependencies:
# ...
flutter_ml_image_transformation:
git:
url: https://github.com/ryu38/flutter-ML-imageTransformation.git
ref: master
- Create a assets folder to import models of PyTorch and/or CoreML. Add lines like below in pubspec.yaml.
assets:
- assets/pytorch_model/
- assets/coreml_model/
- Store models in the assets folder created.
- Copy your model to a documents directory.
About Documents Directory
import 'package:flutter/services.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
// Use a different model for IOS or android.
final String assetModelPath;
final String appDirModelName;
if (Platform.isAndroid) {
assetModelPath = 'assets/pytorch_model/MyModel.ptl';
appDirModelName = 'MyModel.ptl';
} else if (Platform.isIOS) {
assetModelPath = 'assets/coreml_model/MyModel.mlmodel';
appDirModelName = 'MyModel.mlmodel';
} else {
throw Exception();
}
// Get a path to a documents directory and define a path to a model to be copied there.
final appDir = await getApplicationDocumentsDirectory();
final appDirModelPath = join(appDir.path, appDirModelName);
// Copy your model in the assets folder there.
final byteData = await rootBundle.load(assetModelPath);
final bytes = byteData.buffer
.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes);
await File(appDirModelPath).writeAsBytes(bytes);
- Import the library.
import 'package:flutter_ml_image_transformation/flutter_ml_image_transformation.dart';
- Load your model by
MLImageTransformer.setModel()
before executing Image Transformation.
inputWidth
andinputHeight
are set to256
by default.
final result = await MLImageTransformer.setModel(
modelPath: modelPath, inputWidth: 256, inputHeight: 256,
);
if (result != null) throw Exception(result);
- Execute Image Transformation by
MLImageTransformer.transformImage()
.
imagePath
andoutputPath
need to be readable by the app (Documents Directory or Temporary Directory).
final result = await MLImageTransformer.transformImage(
imagePath: imagePath, outputPath: outputPath
);
if (result != null) throw Exception(result);
You can also learn more about how to use the package from /example.