检测面部和作物面部图像抖动火力场ml

时间:2020-08-18 12:23:07

标签: firebase flutter firebase-mlkit

在这里,我需要裁剪检测到的脸部并另存为图像/文件。 我能够检测到带有圆角矩形的脸。如何裁切检测到的脸部区域? 我正在使用flutter_firebase_ml_kit来检测面部。 这是我的代码:

getImageAndDetectFaces() async {
    setState(() {
      isLoading = true;
    });
    final image = FirebaseVisionImage.fromFile(widget.cardImage);
    final faceDetector = FirebaseVision.instance.faceDetector(
        FaceDetectorOptions(
            mode: FaceDetectorMode.fast,
            enableLandmarks: true
        )
    );
    List<Face> faces = await faceDetector.processImage(image);
    if (mounted) {
      setState(() {
        _faces = faces;
        _loadImage(widget.cardImage);
      });
    }
  }

  _loadImage(File file) async {
    final data = await file.readAsBytes();
    await decodeImageFromList(data).then(
          (value) => setState(() {
        img = value;
        isLoading = false;
      }),
    );
  }


class FacePainter extends CustomPainter {
  final ui.Image image;
  final List<Face> faces;
  final List<Rect> rects = [];


  FacePainter(this.image, this.faces) {
    for (var i = 0; i < faces.length; i++) {
      rects.add(faces[i].boundingBox);
    }
  }

  @override
  void paint(ui.Canvas canvas, ui.Size size) {
    final Paint paint = Paint()
      ..style = PaintingStyle.stroke
      ..strokeWidth = 15.0
      ..color = Colors.blue;

    canvas.drawImage(image, Offset.zero, Paint());
    for (var i = 0; i < faces.length; i++) {
      canvas.drawRect(rects[i], paint);
    }
  }


  @override
  bool shouldRepaint(FacePainter oldDelegate) {
    return image != oldDelegate.image || faces != oldDelegate.faces;
  }

}

需要有关如何裁剪检测到的面部区域的解决方案。 预先感谢。

2 个答案:

答案 0 :(得分:1)

使用 List<Face> faces = await faceDetector.processImage(image); 运行 faceDetector 后,您需要从每个检测到的人脸中提取信息。由于我们只对边界框感兴趣,您可以使用以下内容:

List<Map<String, int>> faceMaps = [];
for (Face face in faces) {
      int x = face.boundingBox.left.toInt();
      int y = face.boundingBox.top.toInt();
      int w = face.boundingBox.width.toInt();
      int h = face.boundingBox.height.toInt();
      Map<String, int> thisMap = {'x': x, 'y': y, 'w': w, 'h':h};
      faceMaps.add(thisMap);
    }

上面将创建一个 Map<String, int> 列表,然后用每个检测到的人脸的边界框信息填充它。

然后可以使用Image packagecopyCrop函数从边界框中提取人脸

// add dart package as
import 'package:image/image.dart' as img;
// create an img.Image from your original image file for processing
img.Image originalImage = img.decodeImage(File(_imageFile.path).readAsBytesSync());
// now crop out only the detected face boundry, below will crop out the first face from the list
img.Image faceCrop = img.copyCrop(originalImage, faceMaps[0]['x'], faceMaps[0]['y'], faceMaps[0]['w'], faceMaps[0]['h']);

答案 1 :(得分:0)

一旦有了边界框,就应该可以使用它裁剪脸部。不熟悉颤动,但以下是有关使用Dart裁剪图像的一些讨论:How do I crop an image in Flutter?