detectFaceCropSaveImage method

Future<Image?> detectFaceCropSaveImage(
  1. dynamic imageFile
)

Implementation

Future<img.Image?> detectFaceCropSaveImage(var imageFile) async {
  /// method detect face from iamge crop face and save to
  /// location provided to function
  List<Rect> rectArray = [];
  List<Map<String, int>> faceMaps = [];
  // dynamic data = {};
  // dynamic finalResult = Multimap<String, Face>();
  final inputImage = InputImage.fromFile(File(imageFile.path));

  final faceDetector = GoogleMlKit.vision.faceDetector();
  // printer('process image starts');
  final List<Face> faces = await faceDetector.processImage(inputImage);
  img.Image? faceCrop;
  // printer('process image end');

  if (faces.isNotEmpty) {
    for (Face face in faces) {
      rectArray.add(face.boundingBox);

      int w = face.boundingBox.width.toInt().round();
      w = w + w ~/ 5;
      int h = face.boundingBox.height.toInt().round() +
          face.boundingBox.height.toInt().round() ~/ 2.5;
      int x = face.boundingBox.left.toInt().round() - w ~/ 10;
      int y = face.boundingBox.top.toInt().round() - h ~/ 5;
      Map<String, int> thisMap = {'x': x, 'y': y, 'w': w, 'h': h};
      faceMaps.add(thisMap);
    }

    img.Image? originalImage =
        img.decodeImage(File(imageFile.path).readAsBytesSync());
    faceCrop = img.copyCrop(
      originalImage!,
      faceMaps[0]['x']!,
      faceMaps[0]['y']!,
      faceMaps[0]['w']!,
      faceMaps[0]['h']!,
    );
    // List<int>? imageWithHeader = img.encodeNamedImage(path, '.bmp');

    // img.Image faceCrop = img.copyCrop(
    //     origionalImage!,
    //     faceMaps[0]['x']!,
    //     faceMaps[0]['y']!,
    //     faceMaps[0]['w']!,
    //     faceMaps[0]['w']!,);
    // faceCrop = img.copyResizeCropSquare(faceCrop, 112);
    // if (member != '_') {
    //   // await createDirectory(path + member);
    //   printer('in if save');
    //   File('$path$member/croped.jpg')
    //       .writeAsBytesSync(img.encodeJpg(faceCrop));
    // } else {
    //   printer('in else save');
    //   File('$path/temp_croped.jpg')
    //       .writeAsBytesSync(img.encodeJpg(faceCrop));
    // }
    // File(getFullPath() + 'temp_croped.jpg')
    //     .writeAsBytesSync(img.encodeJpg(faceCrop));

    // List<int> file = img.writeJpg(faceCrop);

    // var bytes = await rootBundle.;
  }
  return faceCrop;
}