Flutter人脸检测功能插件flutter_feature_face_detection的使用

Flutter人脸检测功能插件flutter_face_detection的使用

flutter_face_detection 是一个专为开发者设计的Flutter包,旨在简化人脸检测和活体检测。它结合了强大的Flutter相机库和Google ML Kit的功能,使相机应用中的集成变得无缝。该包优化了面部特征(如眼睛睁开状态和微笑)的检测,并提供了可配置的阈值,使其非常适合活体验证场景。

关键特性:

  • 人脸检测:利用Google ML Kit实时准确地检测人脸。
  • 活体检测:通过分析面部动作(如眼睛睁开和微笑)支持高级活体检测。
  • 阈值定制:开发人员可以轻松调整检测眼睛睁开状态和微笑的阈值以适应各种用例。
  • 相机集成:与 flutter_feature_camera 库结合,处理图像流和摄像头切换等功能。

依赖项:

  • flutter_feature_camera:用于实时人脸检测所需的相机功能。

此库非常适合需要精确且实时人脸和活体检测的应用程序,例如生物识别验证、安全和用户认证。

功能展示

活体面部检测

检测左眼闭合右眼张开

通过检查左眼是否闭合而右眼是否张开来检测活体。

Future<void> onImageStream(CameraImage cameraImage,
    int sensorOrientation,
    DeviceOrientation deviceOrientation,
    CameraLensDirection cameraLensDirection,) async {
  final inputImage = FlutterFaceDetection.inputImageFromCameraImage(
    cameraImage,
    sensorOrientation: sensorOrientation,
    deviceOrientation: deviceOrientation,
    cameraLensDirection: cameraLensDirection,
  );
  if (inputImage != null) {
    final result = await detectLivenessLeftEyeOpenAndRightEyeClose(inputImage);
    // 处理结果
  }
}
检测右眼闭合左眼张开

通过检查右眼是否闭合而左眼是否张开来检测活体。

Future<void> onImageStream(CameraImage cameraImage,
    int sensorOrientation,
    DeviceOrientation deviceOrientation,
    CameraLensDirection cameraLensDirection,) async {
  final inputImage = FlutterFaceDetection.inputImageFromCameraImage(
    cameraImage,
    sensorOrientation: sensorOrientation,
    deviceOrientation: deviceOrientation,
    cameraLensDirection: cameraLensDirection,
  );
  if (inputImage != null) {
    final result = await detectLivenessRightEyeOpenAndLeftEyeClose(inputImage);
    // 处理结果
  }
}
检测微笑

通过检查人是否在微笑来检测活体。

Future<void> onImageStream(CameraImage cameraImage,
    int sensorOrientation,
    DeviceOrientation deviceOrientation,
    CameraLensDirection cameraLensDirection,) async {
  final inputImage = FlutterFaceDetection.inputImageFromCameraImage(
    cameraImage,
    sensorOrientation: sensorOrientation,
    deviceOrientation: deviceOrientation,
    cameraLensDirection: cameraLensDirection,
  );
  if (inputImage != null) {
    final result = await detectSmiling(inputImage);
    // 处理结果
  }
}

开始使用

更改最小SDK版本

android/app/build.gradle 中,将 minSdkVersion 改为 21

// ..
android {
    // ..
    defaultConfig {
        // ..
        minSdkVersion 21 // 更改SDK版本
        // ..
    }
}
导入Flutter功能相机

pubspec.yaml 文件中添加以下依赖项:

flutter_feature_camera: ^0.0.6-beta
扩展基础混入类

扩展 StatefulWidget 并使用 BaseMixinFlutterFaceDetectionCameraBaseMixinFeatureCameraV2

使用方法

初始化流式摄像头
[@override](/user/override)
void initState() {
  super.initState();
  initializeStreamingCamera(
    cameraLensDirection: CameraLensDirection.front,
    onCameraInitialized: onCameraInitialized,
  );
}

void onCameraInitialized(_) {
  setState(() {});

  initializeLivenessFaceDetection();
}
开始流式图像
void streamingCameraTap() {
  startImageStream(
    onImageStream: onImageStream,
  );
}

Future<void> onImageStream(
    CameraImage cameraImage,
    int sensorOrientation,
    DeviceOrientation deviceOrientation,
    CameraLensDirection cameraLensDirection,
    ) async {
  final inputImage = FlutterFaceDetection.inputImageFromCameraImage(
    cameraImage,
    sensorOrientation: sensorOrientation,
    deviceOrientation: deviceOrientation,
    cameraLensDirection: cameraLensDirection,
  );
  if (inputImage != null) {
    // 在这里处理检测活体
  }
}

完整示例代码

以下是一个完整的示例代码,展示了如何使用 flutter_face_detection 插件。

import 'package:flutter/material.dart';
import 'package:flutter_face_detection/flutter_face_detection.dart';
import 'package:flutter_feature_camera/flutter_feature_camera.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MainPage(),
    );
  }
}

class MainPage extends StatefulWidget {
  const MainPage({super.key});

  [@override](/user/override)
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> with BaseMixinFlutterFaceDetectionCamera, BaseMixinFeatureCameraV2 {
  [@override](/user/override)
  void initState() {
    super.initState();
    initializeStreamingCamera(
      cameraLensDirection: CameraLensDirection.front,
      onCameraInitialized: onCameraInitialized,
    );
  }

  void onCameraInitialized(_) {
    setState(() {});
    initializeLivenessFaceDetection();
  }

  void streamingCameraTap() {
    startImageStream(
      onImageStream: onImageStream,
    );
  }

  Future<void> onImageStream(
      CameraImage cameraImage,
      int sensorOrientation,
      DeviceOrientation deviceOrientation,
      CameraLensDirection cameraLensDirection,
      ) async {
    final inputImage = FlutterFaceDetection.inputImageFromCameraImage(
      cameraImage,
      sensorOrientation: sensorOrientation,
      deviceOrientation: deviceOrientation,
      cameraLensDirection: cameraLensDirection,
    );
    if (inputImage != null) {
      // 处理检测活体
      final result = await detectSmiling(inputImage);
      print("Result: $result");
    }
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Face Detection'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: streamingCameraTap,
          child: Text('Start Camera Stream'),
        ),
      ),
    );
  }
}

更多关于Flutter人脸检测功能插件flutter_feature_face_detection的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter人脸检测功能插件flutter_feature_face_detection的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在 Flutter 中实现人脸检测功能,可以使用 flutter_face_detection 插件。不过,截至我的知识截止日期(2023年10月),flutter_face_detection 并不是一个广泛使用的插件。可能你指的是 google_ml_kit 插件,它提供了人脸检测功能。

google_ml_kit 是 Google 提供的一个 Flutter 插件,包含了多种机器学习功能,包括人脸检测、文本识别、图像标注等。以下是如何使用 google_ml_kit 实现人脸检测的步骤:

1. 添加依赖

首先,在 pubspec.yaml 文件中添加 google_ml_kit 依赖:

dependencies:
  flutter:
    sdk: flutter
  google_ml_kit: ^0.10.0

然后运行 flutter pub get 来安装依赖。

2. 初始化人脸检测器

在你的 Dart 文件中,导入 google_ml_kit 并初始化人脸检测器:

import 'package:google_ml_kit/google_ml_kit.dart';

final faceDetector = GoogleMlKit.vision.faceDetector();

3. 处理图像

你可以从相机、相册或网络获取图像,并将其转换为 InputImage 对象。以下是从文件系统中加载图像的示例:

import 'package:image_picker/image_picker.dart';
import 'dart:io';

final picker = ImagePicker();

Future<void> pickImage() async {
  final pickedFile = await picker.getImage(source: ImageSource.gallery);

  if (pickedFile != null) {
    final file = File(pickedFile.path);
    final inputImage = InputImage.fromFile(file);
    processImage(inputImage);
  }
}

4. 检测人脸

使用 faceDetector 处理 InputImage 并检测人脸:

Future<void> processImage(InputImage inputImage) async {
  final List<Face> faces = await faceDetector.processImage(inputImage);

  for (Face face in faces) {
    final rect = face.boundingBox;
    final left = rect.left;
    final top = rect.top;
    final right = rect.right;
    final bottom = rect.bottom;

    print('Face detected at ($left, $top, $right, $bottom)');
  }
}

5. 释放资源

在使用完人脸检测器后,记得释放资源:

[@override](/user/override)
void dispose() {
  faceDetector.close();
  super.dispose();
}
回到顶部