face_auth_kit 0.1.0
face_auth_kit: ^0.1.0 copied to clipboard
Offline Flutter face authentication: 1:1 face matching and silent liveness detection for Android and iOS. UI-agnostic wrapper around face_verification and face_anti_spoofing_detector.
face_auth_kit #
离线 Flutter 人脸认证包,整合:
face_verification— 人脸特征提取 + 1:1 验证face_anti_spoofing_detector— 静默活体检测
仅支持 Android / iOS,完全离线运行,不包含 UI,MIT 协议。
源码:github.com/caohy7/face_auth_kit
特性 #
- 单一入口 API,底层依赖内聚(宿主无需重复声明 ML Kit / TFLite 等)
- 注册、照片 1:1 验证、相机静默活体 + 验证
- 可配置阈值、活体帧间隔、连续活体帧数等
- iOS 图片方向自动校正,提升跨端匹配成功率
- 预览流取帧验证,无系统快门闪屏(见下方示例)
平台要求 #
| 平台 | 最低版本 |
|---|---|
| Android | API 21+(随 Flutter 默认) |
| iOS | 15.5+(google_mlkit_* 要求) |
| Dart | >=3.5.0 <4.0.0 |
| Flutter | >=3.10.0 |
不支持 Web / Desktop。
安装 #
dependencies:
face_auth_kit: ^0.1.0
camera: ^0.12.0 # 相机活体流程需要
image_picker: ^1.1.2 # 相册注册时需要(可选)
flutter pub get
宿主 App 配置 #
本包为纯 Dart 封装,权限与 iOS 部署目标需在 你的 App 中配置。
Android AndroidManifest.xml #
<uses-permission android:name="android.permission.CAMERA" />
iOS Info.plist #
<key>NSCameraUsageDescription</key>
<string>需要使用相机进行人脸活体检测</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>需要访问相册以注册人脸</string>
iOS Podfile(必须) #
platform :ios, '15.5'
快速开始 #
import 'package:face_auth_kit/face_auth_kit.dart';
await FaceAuthKit.instance.initialize(
config: FaceAuthConfig.platformDefault(),
);
推荐使用 [FaceAuthConfig.platformDefault()]:iOS 自动使用单线程 TFLite,更稳定。
核心 API #
| API | 说明 |
|---|---|
FaceAuthKit.initialize() |
初始化识别 + 活体引擎 |
registerUser() |
注册人脸特征 |
verifyPhoto() |
照片 1:1 验证 |
checkLiveness() |
单帧静默活体检测 |
verifyWithLiveness() |
活体 + 验证组合 |
LiveVerificationController |
相机流活体辅助(可选) |
createFrontCameraController() |
创建前置相机(可选) |
encodeStreamFrameForVerification() |
预览流取帧(无快门闪屏) |
注册 #
final result = await FaceAuthKit.instance.registerUser(
userId: 'user_001',
imagePath: '/path/to/photo.jpg',
name: '张三',
);
照片 1:1 验证 #
final result = await FaceAuthKit.instance.verifyPhoto(
imagePath: '/path/to/check.jpg',
targetUserId: 'user_001',
);
相机活体 + 验证(推荐:预览流取帧,无快门闪屏) #
import 'dart:io';
import 'package:camera/camera.dart';
import 'package:face_auth_kit/face_auth_kit.dart';
import 'package:path_provider/path_provider.dart';
final liveController = LiveVerificationController();
final controller = await createFrontCameraController();
var busy = false;
await controller.startImageStream((image) async {
if (busy) return;
final shouldCapture = await liveController.onCameraFrame(
CameraFrameInput(
image: image,
controller: controller,
previewSize: Size(
controller.value.previewSize!.width,
controller.value.previewSize!.height,
),
),
);
if (!shouldCapture) return;
busy = true;
try {
// 必须在第一个 await 之前同步编码,避免 CameraImage 缓冲区被覆盖
final jpeg = encodeStreamFrameForVerification(
image: image,
controller: controller,
);
if (jpeg == null) return;
final dir = await getTemporaryDirectory();
final path =
'${dir.path}/capture_${DateTime.now().microsecondsSinceEpoch}.jpg';
await File(path).writeAsBytes(jpeg);
final result = await liveController.verifyCapturedPhoto(
imagePath: path,
targetUserId: 'user_001',
);
// 处理 result…
} finally {
busy = false;
liveController.reset();
}
});
完整可运行 Demo 见 example/:
git clone https://github.com/caohy7/face_auth_kit.git
cd face_auth_kit/example
flutter pub get
flutter run
与同类包的区别 #
| face_auth_kit | face_recognition_kit | |
|---|---|---|
| UI | 无,自行集成 | 内置扫描 UI |
| 活体 | 静默活体(无动作指令) | 视实现而定 |
| 定位 | 底层能力封装 | 开箱即用 SDK |
许可证 #
本包采用 MIT,版权归 caohy 所有。
底层模型与依赖请遵循各自许可证:
- FaceNet(
face_verification内置) - Silent-Face-Anti-Spoofing / MiniFASNet(Apache 2.0)
- Google ML Kit(免费专有 SDK,需保留版权声明)
商用前请保留相应版权声明。