Flutter TFLite Mobile
A Flutter package for face detection, liveness verification, and document scanning using TensorFlow Lite and Google ML Kit. Supports identity verification with face capture sequences and DNI document scanning for iOS and Android.
Features
- Face Detection: Real-time face detection using Google ML Kit
- Liveness Verification: Captures face positions (center, up, down, left, right) to verify user presence
- Document Scanning: DNI (ID document) front and back capture with guided frames
- Selfie Capture: Close and far distance selfies for biometric validation
- State Management: Built-in BLoC pattern for managing capture states
- Cross-platform: Supports both iOS and Android
Installation
Add this to your pubspec.yaml:
dependencies:
flutter_tflite_mobile: ^2.0.0
Then run:
flutter pub get
Platform Setup
Android
Add the following permissions to your android/app/src/main/AndroidManifest.xml:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Set minimum SDK version in android/app/build.gradle:
android {
defaultConfig {
minSdkVersion 21
}
}
iOS
Add the following to your ios/Runner/Info.plist:
<key>NSCameraUsageDescription</key>
<string>This app requires camera access for face detection and document scanning.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>This app requires photo library access to save captured images.</string>
Usage
Basic Setup
import 'package:flutter_tflite_mobile/flutter_tflite_mobile.dart';
void main() {
runApp(MyApp());
}
Face Detection View
import 'package:flutter_tflite_mobile/flutter_tflite_mobile.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class MyFaceDetectionScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => ReconocimientoBloc(apiRest: yourApiRestInstance),
child: FaceDetectorView(),
);
}
}
Document Scanning
import 'package:flutter_tflite_mobile/flutter_tflite_mobile.dart';
class MyDocumentScanScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return CamaraParaDocumento(
redireccion: '/home',
);
}
}
Using the BLoC
// Initialize recognition
context.read<ReconocimientoBloc>().add(OnInitReconocimiento());
// Capture front document image
context.read<ReconocimientoBloc>().add(OnCapturaImagenFrontal(image: imageBytes));
// Capture back document image
context.read<ReconocimientoBloc>().add(OnCapturaImagenTrasero(image: imageBytes));
// Upload images
context.read<ReconocimientoBloc>().add(OnUploadImage(isSelfie: false));
// Listen to state changes
BlocListener<ReconocimientoBloc, ReconocimientoState>(
listener: (context, state) {
if (state.error.isNotEmpty) {
// Handle error
}
if (state.accion == ReconocimientoBloc.onUploadImageDocumento) {
// Document uploaded successfully
}
},
child: YourWidget(),
)
Configuration
Before using the package, configure your environment in lib/bloc/global/environment.dart:
class Environment {
static const String clientId = 'YOUR_CLIENT_ID';
static const String clientSecret = 'YOUR_CLIENT_SECRET';
static const String originColeccion = 'YOUR_ORIGIN_COLLECTION';
}
And in lib/private/env.dart:
class Env {
static const String endpointApi = 'YOUR_API_ENDPOINT';
static const String endpointAuth = 'YOUR_AUTH_ENDPOINT';
static const String endpointSocket = 'YOUR_SOCKET_ENDPOINT';
}
Face Detection Flow
- Mirror Detection: User rotates head to establish camera orientation
- Sequence Capture: Captures 5 face positions (center, up, down, left, right)
- Liveness Verification: Takes close and far selfies
- Validation: Uploads images to backend API for biometric verification
Dependencies
This package depends on the following packages:
google_mlkit_face_detection: Face detection with ML Kitcamera: Camera integrationflutter_bloc: State managementvibration: Haptic feedbackpermission_handler: Permission managementimage_picker: Manual image capture fallback
Platforms
| Platform | Support |
|---|---|
| Android | ✅ |
| iOS | ✅ |
| Web | ❌ |
| Windows | ❌ |
| macOS | ❌ |
| Linux | ❌ |
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome!
Issues
If you find any issues, please report them on the pub.flutter-io.cn issue tracker.
Libraries
- bloc/bloc_observer
- bloc/global/environment
- bloc/reconocimiento/reconocimiento_bloc
- flutter_tflite_mobile
- main
- model/detection_result
- model/isolate_data
- private/env
- utils/procesamiento_imagen
- utils/validacion_imagen
- view/camara_view
- view/document_detection/camara_para_documento
- view/face_detection/camera_view
- view/face_detection/face_detection_widget
- view/face_detection/face_detector_view
- view/home_page
- view/inicio
- widget/btn_siguiente
- widget/contador_captura
- widget/error_widget
- widget/face_detection/painter/coordinates_translator
- widget/face_detection/painter/face_detector_painter
- widget/face_detection/painter/face_progress_oval
- widget/instrucciones_carga_widget
- widget/zoom_widget