I'm using this template/package to draw in Flutter https://pub.dev/packages/scribble/example.
Now I want to convert the drawn image to a File
. So that I can use it in my tflite
model. To predict the image the runModelOnImage
(tflite
package) function requires a path to the image.
The used scribble
package, provides the saveImage
function to return the drawn image as byteData
:
// saveImage
Future<void> _saveImage(BuildContext context) async {
final image = await notifier.renderImage();
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text("Your Image"),
content: Image.memory(image.buffer.asUint8List()),
),
);
}
How can I transform the byteData image to a File so I can use it in my model?
CodePudding user response:
ByteData is an abstraction for:
A fixed-length, random-access sequence of bytes that also provides random and unaligned access to the fixed-width integers and floating point numbers represented by those bytes. As Gunter mentioned in the comments, you can use File.writeAsBytes. It does require a bit of API work to get from ByteData to a List, however.
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
Future<void> writeToFile(ByteData data, String path) {
final buffer = data.buffer;
return new File(path).writeAsBytes(
buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));
}
You can read more about this on here