Created
August 4, 2020 13:43
-
-
Save ravindu9701/26337015a073e9f0f7a3df0b4bc25649 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final _canvasCullRect = Rect.fromPoints( | |
Offset(0, 0), | |
Offset(Constants.imageSize, Constants.imageSize), | |
); | |
final _whitePaint = Paint() | |
..strokeCap = StrokeCap.round | |
..color = Colors.white | |
..strokeWidth = Constants.strokeWidth; | |
final _bgPaint = Paint() | |
..color = Colors.black; | |
class Recognizer { | |
Future loadModel() { | |
Tflite.close(); | |
return Tflite.loadModel( | |
model: "assets/mnist.tflite", | |
labels: "assets/mnist.txt", | |
); | |
} | |
dispose() { | |
Tflite.close(); | |
} | |
Future<Uint8List> previewImage(List<Offset> points) async { | |
final picture = _pointsToPicture(points); | |
final image = await picture.toImage(Constants.mnistImageSize, Constants.mnistImageSize); | |
var pngBytes = await image.toByteData(format: ImageByteFormat.png); | |
return pngBytes.buffer.asUint8List(); | |
} | |
Future recognize(List<Offset> points) async { | |
final picture = _pointsToPicture(points); | |
Uint8List bytes = await _imageToByteListUint8( | |
picture, Constants.mnistImageSize); | |
return _predict(bytes); | |
} | |
Future _predict(Uint8List bytes) { | |
return Tflite.runModelOnBinary(binary: bytes); | |
} | |
Future<Uint8List> _imageToByteListUint8(Picture pic, int size) async { | |
final img = await pic.toImage(size, size); | |
final imgBytes = await img.toByteData(); | |
final resultBytes = Float32List(size * size); | |
final buffer = Float32List.view(resultBytes.buffer); | |
int index = 0; | |
for (int i = 0; i < imgBytes.lengthInBytes; i += 4) { | |
final r = imgBytes.getUint8(i); | |
final g = imgBytes.getUint8(i + 1); | |
final b = imgBytes.getUint8(i + 2); | |
buffer[index++] = (r + g + b) / 3.0 / 255.0; | |
} | |
return resultBytes.buffer.asUint8List(); | |
} | |
Picture _pointsToPicture(List<Offset> points) { | |
final recorder = PictureRecorder(); | |
final canvas = Canvas(recorder, _canvasCullRect) | |
..scale(Constants.mnistImageSize / Constants.canvasSize); | |
canvas.drawRect( | |
Rect.fromLTWH(0, 0, Constants.imageSize, Constants.imageSize), | |
_bgPaint); | |
for (int i = 0; i < points.length - 1; i++) { | |
if (points[i] != null && points[i + 1] != null) { | |
canvas.drawLine(points[i], points[i + 1], _whitePaint); | |
} | |
} | |
return recorder.endRecording(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment