Here I need to crop detected faces and save as image/file. I am able to detecting face with rounded rectangle. How to crop the area of detected face? I am using flutter_firebase_ml_kit to detect faces. Here is my code:
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;
}
}
Need solution on how to crop the detected face area. Thanks in advance.