FaceDetection.dart 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import 'dart:io';
  2. import 'package:firebase_ml_vision/firebase_ml_vision.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:image_picker/image_picker.dart';
  5. class FaceDetectionView extends StatefulWidget {
  6. FaceDetectionView({Key key}) : super(key: key);
  7. @override
  8. _FaceDetectionViewState createState() => _FaceDetectionViewState();
  9. }
  10. class _FaceDetectionViewState extends State<FaceDetectionView> {
  11. File filePath;
  12. @override
  13. void initState() {
  14. super.initState();
  15. }
  16. chooseImage() async {
  17. filePath = await ImagePicker.pickImage(
  18. source: ImageSource.gallery,
  19. imageQuality: 100,
  20. maxWidth: MediaQuery.of(context).size.width);
  21. var availableImage = filePath;
  22. // final FirebaseVisionImageMetadata metadata = FirebaseVisionImageMetadata(
  23. // rawFormat: availableImage.format.raw,
  24. // size: Size(
  25. // availableImage.width.toDouble(), availableImage.height.toDouble()),
  26. // planeData: availableImage.planes
  27. // .map((currentPlane) => FirebaseVisionImagePlaneMetadata(
  28. // bytesPerRow: currentPlane.bytesPerRow,
  29. // height: currentPlane.height,
  30. // width: currentPlane.width))
  31. // .toList(),
  32. // rotation: ImageRotation.rotation90);
  33. final FirebaseVisionImage visionImage =
  34. FirebaseVisionImage.fromFile(filePath);
  35. final FaceDetector detector = FirebaseVision.instance.faceDetector();
  36. final List<Face> faces = await detector.processImage(visionImage);
  37. print(faces[0].boundingBox);
  38. setState(() {
  39. filePath = filePath;
  40. });
  41. }
  42. @override
  43. Widget build(BuildContext context) {
  44. return Stack(children: [
  45. RaisedButton(
  46. child: Text("选择图片"),
  47. onPressed: () {
  48. chooseImage();
  49. },
  50. ),
  51. filePath == null
  52. ? Container()
  53. : Container(
  54. child: Image.file(
  55. filePath,
  56. fit: BoxFit.fitWidth,
  57. ),
  58. )
  59. ]);
  60. }
  61. }
  62. class FaceMain extends StatelessWidget {
  63. const FaceMain({Key key}) : super(key: key);
  64. @override
  65. Widget build(BuildContext context) {
  66. return Scaffold(
  67. body: FaceDetectionView(),
  68. );
  69. }
  70. }