Flutter Face Detection

1k Views Asked by At

I am trying out the example app for flutter camera ml vision Now what I want to do is take a picture of the face within the bounds of the custom painter only and save as a png.

With the default example the custom painter which is a square with a thick red border shows around the detected face but now what I need help with is taking a picture of the face within that square.

1

There are 1 best solutions below

0
On

Basically you want to crop face part from the original image.So use this library to crop image. So I.m assuming that you have the coordinates value of face as you showing box around face.

Import this image library and use below method to crop image:

import 'package:image/image.dart' as IMG;

Future<void> cropSquare(String srcFilePath, String destFilePath) async {
  final bytes = await File(srcFilePath).readAsBytes();
  IMG.Image src = IMG.decodeImage(bytes);

  // pass x and y(offset),width, height value of face bounding box you detected
  IMG.Image destImage = IMG.copyCrop(src, x, y, width, height);

  final png = IMG.encodePng(destImage);
  await File(destFilePath).writeAsBytes(png);
}