detector_painters.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import 'dart:ui' as ui;
  2. import 'package:firebase_ml_vision/firebase_ml_vision.dart';
  3. import 'package:flutter/foundation.dart';
  4. import 'package:flutter/material.dart';
  5. enum Detector { barcode, face, label, cloudLabel, text }
  6. class BarcodeDetectorPainter extends CustomPainter {
  7. BarcodeDetectorPainter(this.imageSize, this.barcodes);
  8. final Size imageSize;
  9. final List<Barcode> barcodes;
  10. @override
  11. void paint(Canvas canvas, Size size) {
  12. final Paint paint = Paint()
  13. ..style = PaintingStyle.stroke
  14. ..strokeWidth = 2.0;
  15. for (Barcode barcode in barcodes) {
  16. paint.color = Colors.green;
  17. canvas.drawRect(
  18. _scaleRect(
  19. rect: barcode.boundingBox,
  20. imageSize: imageSize,
  21. widgetSize: size,
  22. ),
  23. paint,
  24. );
  25. }
  26. }
  27. @override
  28. bool shouldRepaint(BarcodeDetectorPainter oldDelegate) {
  29. return oldDelegate.imageSize != imageSize ||
  30. oldDelegate.barcodes != barcodes;
  31. }
  32. }
  33. class FaceDetectorPainter extends CustomPainter {
  34. FaceDetectorPainter(this.imageSize, this.faces);
  35. final Size imageSize;
  36. final List<Face> faces;
  37. @override
  38. void paint(Canvas canvas, Size size) {
  39. final Paint paint = Paint()
  40. ..style = PaintingStyle.stroke
  41. ..strokeWidth = 2.0
  42. ..color = Colors.red;
  43. for (Face face in faces) {
  44. canvas.drawRect(
  45. _scaleRect(
  46. rect: face.boundingBox,
  47. imageSize: imageSize,
  48. widgetSize: size,
  49. ),
  50. paint,
  51. );
  52. }
  53. }
  54. @override
  55. bool shouldRepaint(FaceDetectorPainter oldDelegate) {
  56. return oldDelegate.imageSize != imageSize || oldDelegate.faces != faces;
  57. }
  58. }
  59. class LabelDetectorPainter extends CustomPainter {
  60. LabelDetectorPainter(this.imageSize, this.labels);
  61. final Size imageSize;
  62. final List<dynamic> labels;
  63. @override
  64. void paint(Canvas canvas, Size size) {
  65. final ui.ParagraphBuilder builder = ui.ParagraphBuilder(
  66. ui.ParagraphStyle(
  67. textAlign: TextAlign.left,
  68. fontSize: 23.0,
  69. textDirection: TextDirection.ltr),
  70. );
  71. builder.pushStyle(ui.TextStyle(color: Colors.green));
  72. // for (Label label in labels) {
  73. // builder.addText('Label: ${label.label}, '
  74. // 'Confidence: ${label.confidence.toStringAsFixed(2)}\n');
  75. // }
  76. builder.pop();
  77. canvas.drawParagraph(
  78. builder.build()
  79. ..layout(ui.ParagraphConstraints(
  80. width: size.width,
  81. )),
  82. const Offset(0.0, 0.0),
  83. );
  84. }
  85. @override
  86. bool shouldRepaint(LabelDetectorPainter oldDelegate) {
  87. return oldDelegate.imageSize != imageSize || oldDelegate.labels != labels;
  88. }
  89. }
  90. // Paints rectangles around all the text in the image.
  91. class TextDetectorPainter extends CustomPainter {
  92. TextDetectorPainter(this.imageSize, this.visionText);
  93. final Size imageSize;
  94. final VisionText visionText;
  95. @override
  96. void paint(Canvas canvas, Size size) {
  97. final Paint paint = Paint()
  98. ..style = PaintingStyle.stroke
  99. ..strokeWidth = 2.0;
  100. Rect _getRect(TextContainer container) {
  101. return _scaleRect(
  102. rect: container.boundingBox,
  103. imageSize: imageSize,
  104. widgetSize: size,
  105. );
  106. }
  107. for (TextBlock block in visionText.blocks) {
  108. for (TextLine line in block.lines) {
  109. for (TextElement element in line.elements) {
  110. paint.color = Colors.green;
  111. canvas.drawRect(_getRect(element), paint);
  112. }
  113. paint.color = Colors.yellow;
  114. canvas.drawRect(_getRect(line), paint);
  115. }
  116. paint.color = Colors.red;
  117. canvas.drawRect(_getRect(block), paint);
  118. }
  119. }
  120. @override
  121. bool shouldRepaint(TextDetectorPainter oldDelegate) {
  122. return oldDelegate.imageSize != imageSize ||
  123. oldDelegate.visionText != visionText;
  124. }
  125. }
  126. Rect _scaleRect({
  127. @required Rect rect,
  128. @required Size imageSize,
  129. @required Size widgetSize,
  130. }) {
  131. final double scaleX = widgetSize.width / imageSize.width;
  132. final double scaleY = widgetSize.height / imageSize.height;
  133. return Rect.fromLTRB(
  134. rect.left.toDouble() * scaleX,
  135. rect.top.toDouble() * scaleY,
  136. rect.right.toDouble() * scaleX,
  137. rect.bottom.toDouble() * scaleY,
  138. );
  139. }