face_bounding_box.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import 'dart:math';
  2. import 'package:flutter/material.dart';
  3. import 'package:get/get.dart';
  4. import 'package:google_mlkit_face_detection/google_mlkit_face_detection.dart';
  5. import '../index.dart';
  6. class FaceBoundingBox extends GetView<FacialRecognitionController> {
  7. const FaceBoundingBox({Key? key}) : super(key: key);
  8. @override
  9. Widget build(BuildContext context) {
  10. return Obx(() {
  11. if (!controller.state.isShowFaceRecognitionResult) {
  12. return Container();
  13. }
  14. return Container(
  15. padding: const EdgeInsets.all(10),
  16. child: GetBuilder<FacialRecognitionController>(
  17. id: 'face_bounding_box',
  18. builder: (context) {
  19. return CustomPaint(
  20. size: Size.infinite,
  21. painter: _FaceBoundingBoxPainter(
  22. faces: controller.kFrameFacesResult,
  23. sourceImageSize: controller.kFrameImageSize,
  24. isMirror: true,
  25. ),
  26. );
  27. }),
  28. );
  29. });
  30. }
  31. }
  32. class _FaceBoundingBoxPainter extends CustomPainter {
  33. final List<Face> faces;
  34. /// 是否镜像
  35. final bool isMirror;
  36. /// 原始图片大小
  37. final Size sourceImageSize;
  38. _FaceBoundingBoxPainter(
  39. {required this.faces,
  40. required this.isMirror,
  41. required this.sourceImageSize});
  42. @override
  43. void paint(Canvas canvas, Size size) {
  44. // 根据原始图片大小和当前画布大小,计算缩放比例
  45. final scaleX = size.width / sourceImageSize.width;
  46. final scaleY = size.height / sourceImageSize.height;
  47. if (isMirror) {
  48. canvas.scale(-scaleX, scaleY);
  49. canvas.translate(-sourceImageSize.width, 0);
  50. } else {
  51. canvas.scale(scaleX, scaleY);
  52. canvas.translate(0, 0);
  53. }
  54. for (final face in faces) {
  55. /// 绘制关键点
  56. // for (final FaceContour? contour in face.contours.values) {
  57. // if (contour == null) continue;
  58. // if (contour.points.isEmpty) continue;
  59. // if (contour.type == FaceContourType.face) {
  60. // // _drawContourPointsPath(canvas, size, contour.points);
  61. // } else {
  62. // // _drawContourPoints(canvas, size, contour.points);
  63. // }
  64. // }
  65. /// 绘制人脸框
  66. _drawFaceRect(canvas, size, face.boundingBox);
  67. }
  68. /// 全屏画绿色
  69. // final paint2 = Paint()
  70. // ..color = const Color.fromARGB(255, 36, 255, 36)
  71. // ..strokeWidth = 5
  72. // ..style = PaintingStyle.stroke;
  73. // canvas.drawRect(
  74. // Rect.fromLTWH(0, 0, sourceImageSize.width, sourceImageSize.height),
  75. // paint2,
  76. // );
  77. }
  78. /// 绘制contour连线
  79. void _drawContourPointsPath(
  80. Canvas canvas, Size size, List<Point<int>> points) {
  81. final paint = Paint()
  82. ..color = Colors.green
  83. ..strokeWidth = 4
  84. ..style = PaintingStyle.stroke;
  85. final path = Path();
  86. for (int i = 0; i < points.length; i++) {
  87. final point = points[i];
  88. if (i == 0) {
  89. path.moveTo(
  90. point.x.toDouble(),
  91. point.y.toDouble(),
  92. );
  93. continue;
  94. }
  95. path.lineTo(
  96. point.x.toDouble(),
  97. point.y.toDouble(),
  98. );
  99. }
  100. path.close();
  101. canvas.drawPath(path, paint);
  102. }
  103. /// 绘制contour点集
  104. void _drawContourPoints(Canvas canvas, Size size, List<Point<int>> points) {
  105. final paint = Paint()
  106. ..color = Colors.red
  107. ..strokeWidth = 2
  108. ..style = PaintingStyle.fill;
  109. for (final point in points) {
  110. canvas.drawCircle(
  111. Offset(
  112. point.x.toDouble(),
  113. point.y.toDouble(),
  114. ),
  115. 2,
  116. paint,
  117. );
  118. }
  119. }
  120. /// 绘制人脸框
  121. void _drawFaceRect(Canvas canvas, Size size, Rect rect) {
  122. final paint = Paint()
  123. ..color = Colors.green
  124. ..strokeWidth = 3
  125. ..style = PaintingStyle.stroke;
  126. canvas.drawRect(rect, paint);
  127. }
  128. @override
  129. bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
  130. }