Flutter人脸识别插件flutter_face_huawei_vision__beta的使用

Huawei Vision 用于通过华为服务进行视频流中的人脸检测。

不支持 Google Mobile Services (GMS) 的设备(如华为设备)需要包含 Huawei Vision 库。这意味着要在非 GMS 设备(例如华为设备或任何禁用了 Google Mobile Services 的设备)上运行 Face SDK,必须集成 Huawei Vision 库。在这种情况下,会自动识别华为或荣耀设备,并使用 Huawei Vision。

setForceToUseHuaweiVision 标志在配置中用于强制使用 Huawei Vision 库,仅在连接了该库时生效。因此,在非华为设备且未启用 Google Mobile Services 的情况下,Face SDK 可以通过华为服务正常工作。

文档

文档可以在此处找到:https://docs.regulaforensics.com/develop/doc-reader-sdk/mobile/flutter

示例应用

示例应用可以在此处找到:https://github.com/regulaforensics/flutter_face_api


完整示例代码

以下是一个完整的示例代码,展示如何在 Flutter 中使用 flutter_face_huawei_vision__beta 插件来实现人脸检测功能。

示例代码:main.dart

import 'package:flutter/material.dart';
import 'package:flutter_face_huawei_vision__beta/flutter_face_huawei_vision__beta.dart';

void main() {
  // 初始化 Huawei Vision
  WidgetsFlutterBinding.ensureInitialized();
  FaceHuaweiVision.setForceToUseHuaweiVision(true);

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: FaceDetectionScreen(),
    );
  }
}

class FaceDetectionScreen extends StatefulWidget {
  [@override](/user/override)
  _FaceDetectionScreenState createState() => _FaceDetectionScreenState();
}

class _FaceDetectionScreenState extends State<FaceDetectionScreen> {
  bool _isDetecting = false;
  String _detectionResult = "等待人脸检测...";

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Huawei Vision 人脸检测"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: () async {
                setState(() {
                  _isDetecting = true;
                });

                try {
                  // 模拟人脸检测结果
                  final result = await FaceHuaweiVision.detectFaces();
                  setState(() {
                    _detectionResult = "检测到 ${result.length} 张人脸";
                  });
                } catch (e) {
                  setState(() {
                    _detectionResult = "检测失败: $e";
                  });
                } finally {
                  setState(() {
                    _isDetecting = false;
                  });
                }
              },
              child: Text(_isDetecting ? "检测中..." : "开始检测"),
            ),
            SizedBox(height: 20),
            Text(
              _detectionResult,
              style: TextStyle(fontSize: 18),
            )
          ],
        ),
      ),
    );
  }
}

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

1 回复

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


flutter_face_huawei_vision_beta 是一个基于华为HMS(Huawei Mobile Services)的Flutter插件,用于在Flutter应用中实现人脸识别功能。这个插件目前处于Beta阶段,因此在使用时可能会遇到一些问题或限制。

以下是如何在Flutter项目中使用 flutter_face_huawei_vision_beta 插件的基本步骤:

1. 添加依赖

首先,在 pubspec.yaml 文件中添加 flutter_face_huawei_vision_beta 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  flutter_face_huawei_vision_beta: ^0.0.1 # 请使用最新版本

然后运行 flutter pub get 来获取依赖。

2. 配置华为HMS

在使用华为HMS服务之前,你需要在华为开发者联盟网站上注册并创建一个项目,然后启用人脸识别服务。

  1. 注册华为开发者账号:访问 华为开发者联盟 并注册一个账号。
  2. 创建项目:在开发者控制台中创建一个新项目。
  3. 启用人脸识别服务:在项目中启用 HUAWEI Vision 服务。
  4. 获取 agconnect-services.json 文件:在项目中下载 agconnect-services.json 文件,并将其放置在Flutter项目的 android/app 目录下。

3. 配置Android项目

android/app/build.gradle 文件中,确保已经添加了华为HMS的依赖:

dependencies {
    implementation 'com.huawei.hms:ml-computer-vision-face:2.0.5.300' // 请使用最新版本
    implementation 'com.huawei.hms:ml-computer-vision-face-model:2.0.5.300' // 请使用最新版本
}

4. 初始化插件

在Flutter应用的 main.dart 文件中,初始化 flutter_face_huawei_vision_beta 插件:

import 'package:flutter/material.dart';
import 'package:flutter_face_huawei_vision_beta/flutter_face_huawei_vision_beta.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await FlutterFaceHuaweiVisionBeta.init();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: FaceRecognitionScreen(),
    );
  }
}

5. 使用人脸识别功能

你可以在应用中使用 flutter_face_huawei_vision_beta 插件提供的人脸识别功能。以下是一个简单的示例,展示如何使用插件进行人脸检测:

import 'package:flutter/material.dart';
import 'package:flutter_face_huawei_vision_beta/flutter_face_huawei_vision_beta.dart';

class FaceRecognitionScreen extends StatefulWidget {
  @override
  _FaceRecognitionScreenState createState() => _FaceRecognitionScreenState();
}

class _FaceRecognitionScreenState extends State<FaceRecognitionScreen> {
  List<Face> _faces = [];

  Future<void> _detectFaces() async {
    final imagePath = 'assets/sample_image.jpg'; // 替换为你的图片路径
    final faces = await FlutterFaceHuaweiVisionBeta.detectFaces(imagePath);
    setState(() {
      _faces = faces;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Face Recognition'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: _detectFaces,
              child: Text('Detect Faces'),
            ),
            SizedBox(height: 20),
            Text('Detected Faces: ${_faces.length}'),
          ],
        ),
      ),
    );
  }
}

6. 处理权限

在使用摄像头或访问设备存储时,确保你已经请求了相应的权限。你可以在 AndroidManifest.xml 文件中添加以下权限:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
回到顶部