|
| 1 | +import 'package:camera/camera.dart'; |
| 2 | +import 'package:firebase_ml_vision/firebase_ml_vision.dart'; |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter_camera_ml_vision/flutter_camera_ml_vision.dart'; |
| 5 | + |
| 6 | +void main() => runApp(MyApp()); |
| 7 | + |
| 8 | +class MyApp extends StatelessWidget { |
| 9 | + @override |
| 10 | + Widget build(BuildContext context) { |
| 11 | + return MaterialApp( |
| 12 | + title: 'Flutter Demo', |
| 13 | + theme: ThemeData( |
| 14 | + primarySwatch: Colors.blue, |
| 15 | + ), |
| 16 | + home: MyHomePage(title: 'Flutter Demo Home Page'), |
| 17 | + ); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +class MyHomePage extends StatefulWidget { |
| 22 | + MyHomePage({Key key, this.title}) : super(key: key); |
| 23 | + |
| 24 | + final String title; |
| 25 | + |
| 26 | + @override |
| 27 | + _MyHomePageState createState() => _MyHomePageState(); |
| 28 | +} |
| 29 | + |
| 30 | +class _MyHomePageState extends State<MyHomePage> { |
| 31 | + List<Face> _faces = []; |
| 32 | + final _scanKey = GlobalKey<CameraMlVisionState>(); |
| 33 | + CameraLensDirection cameraLensDirection = CameraLensDirection.front; |
| 34 | + |
| 35 | + @override |
| 36 | + Widget build(BuildContext context) { |
| 37 | + return Scaffold( |
| 38 | + appBar: AppBar( |
| 39 | + title: Text(widget.title), |
| 40 | + ), |
| 41 | + body: SizedBox.expand( |
| 42 | + child: CameraMlVision<List<Face>>( |
| 43 | + key: _scanKey, |
| 44 | + cameraLensDirection: cameraLensDirection, |
| 45 | + detector: FirebaseVision.instance |
| 46 | + .faceDetector(FaceDetectorOptions( |
| 47 | + enableTracking: true, |
| 48 | + mode: FaceDetectorMode.accurate, |
| 49 | + )) |
| 50 | + .processImage, |
| 51 | + overlayBuilder: (c) { |
| 52 | + return CustomPaint( |
| 53 | + painter: FaceDetectorPainter( |
| 54 | + _scanKey.currentState.cameraValue.previewSize.flipped, _faces, |
| 55 | + reflection: cameraLensDirection == CameraLensDirection.front), |
| 56 | + ); |
| 57 | + }, |
| 58 | + onResult: (faces) { |
| 59 | + if (faces == null || faces.isEmpty || !mounted) { |
| 60 | + return; |
| 61 | + } |
| 62 | + setState(() { |
| 63 | + _faces = []..addAll(faces); |
| 64 | + }); |
| 65 | + }, |
| 66 | + ), |
| 67 | + ), |
| 68 | + ); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +class FaceDetectorPainter extends CustomPainter { |
| 73 | + FaceDetectorPainter(this.imageSize, this.faces, {this.reflection = false}); |
| 74 | + |
| 75 | + final bool reflection; |
| 76 | + final Size imageSize; |
| 77 | + final List<Face> faces; |
| 78 | + |
| 79 | + @override |
| 80 | + void paint(Canvas canvas, Size size) { |
| 81 | + final Paint paint = Paint() |
| 82 | + ..style = PaintingStyle.stroke |
| 83 | + ..strokeWidth = 2.0 |
| 84 | + ..color = Colors.red; |
| 85 | + |
| 86 | + for (Face face in faces) { |
| 87 | + final faceRect = |
| 88 | + _reflectionRect(reflection, face.boundingBox, imageSize.width); |
| 89 | + canvas.drawRect( |
| 90 | + _scaleRect( |
| 91 | + rect: faceRect, |
| 92 | + imageSize: imageSize, |
| 93 | + widgetSize: size, |
| 94 | + ), |
| 95 | + paint, |
| 96 | + ); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @override |
| 101 | + bool shouldRepaint(FaceDetectorPainter oldDelegate) { |
| 102 | + return oldDelegate.imageSize != imageSize || oldDelegate.faces != faces; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +Rect _reflectionRect(bool reflection, Rect boundingBox, double width) { |
| 107 | + if (!reflection) { |
| 108 | + return boundingBox; |
| 109 | + } |
| 110 | + final centerX = width / 2; |
| 111 | + final left = ((boundingBox.left - centerX) * -1) + centerX; |
| 112 | + final right = ((boundingBox.right - centerX) * -1) + centerX; |
| 113 | + return Rect.fromLTRB(left, boundingBox.top, right, boundingBox.bottom); |
| 114 | +} |
| 115 | + |
| 116 | +Rect _scaleRect({ |
| 117 | + @required Rect rect, |
| 118 | + @required Size imageSize, |
| 119 | + @required Size widgetSize, |
| 120 | +}) { |
| 121 | + final scaleX = widgetSize.width / imageSize.width; |
| 122 | + final scaleY = widgetSize.height / imageSize.height; |
| 123 | + |
| 124 | + final scaledRect = Rect.fromLTRB( |
| 125 | + rect.left.toDouble() * scaleX, |
| 126 | + rect.top.toDouble() * scaleY, |
| 127 | + rect.right.toDouble() * scaleX, |
| 128 | + rect.bottom.toDouble() * scaleY, |
| 129 | + ); |
| 130 | + return scaledRect; |
| 131 | +} |
0 commit comments