FaceDetection.dart 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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(source: ImageSource.gallery,imageQuality: 100,maxWidth: MediaQuery.of(context).size.width);
  18. var availableImage=filePath;
  19. // final FirebaseVisionImageMetadata metadata = FirebaseVisionImageMetadata(
  20. // rawFormat: availableImage.format.raw,
  21. // size: Size(
  22. // availableImage.width.toDouble(), availableImage.height.toDouble()),
  23. // planeData: availableImage.planes
  24. // .map((currentPlane) => FirebaseVisionImagePlaneMetadata(
  25. // bytesPerRow: currentPlane.bytesPerRow,
  26. // height: currentPlane.height,
  27. // width: currentPlane.width))
  28. // .toList(),
  29. // rotation: ImageRotation.rotation90);
  30. final FirebaseVisionImage visionImage =
  31. FirebaseVisionImage.fromFile(filePath);
  32. final FaceDetector detector = FirebaseVision.instance.faceDetector();
  33. final List<Face> faces = await detector.processImage(visionImage);
  34. print(faces[0].boundingBox);
  35. setState(() {
  36. filePath=filePath;
  37. });
  38. }
  39. @override
  40. Widget build(BuildContext context) {
  41. return Stack(
  42. children: [
  43. RaisedButton(child: Text("选择图片"),onPressed: (){chooseImage();},),
  44. filePath==null?Container():
  45. Container(
  46. child: Image.file(filePath,fit: BoxFit.fitWidth,),
  47. )
  48. ]
  49. );
  50. }
  51. }
  52. class FaceMain extends StatelessWidget {
  53. const FaceMain({Key key}) : super(key: key);
  54. @override
  55. Widget build(BuildContext context) {
  56. return Scaffold(
  57. body: FaceDetectionView(),
  58. );
  59. }
  60. }