首页 > 解决方案 > Flutter 人脸检测

问题描述

我正在尝试flutter camera ml vision的示例应用程序 现在我想做的是仅在自定义画家的范围内拍摄面部照片并保存为png。

在默认示例中,自定义画家是一个带有粗红色边框的正方形,显示在检测到的面部周围,但现在我需要帮助的是在该正方形内拍摄面部照片。

标签: fluttercameraface-detectionmlvision

解决方案


基本上你想从原始图像中裁剪面部部分。所以使用这个 库来裁剪图像。所以我假设你在面部周围显示框时具有面部的坐标值。

导入此图像库并使用以下方法裁剪图像:

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);
}

推荐阅读