detector_painters.dart 4.1 KB

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