Skip to main content

flutter_liveness 2.0.0 copy "flutter_liveness: ^2.0.0" to clipboard
flutter_liveness: ^2.0.0 copied to clipboard

On-device face liveness detection for Flutter using a MobileNetV2 model. Detect spoof attacks from photos and videos, no internet required.

flutter_liveness #

Model: MobileNetV2 Dataset: Spoof/Real License: MIT

Lightweight on-device face liveness detection for Flutter, powered by MobileNetV2 from open liveness research.

✨ Features #

  • On-device liveness detection — no server required
  • Ships with pre-trained MobileNetV2 model
  • Runs on LiteRT (formerly TensorFlow Lite) through flutter_litert, optimized for real-time
  • Simple API — plug & play
  • Blur & clarity detector (Laplacian filter)

🧠 Model Information #

This package uses a pre-trained MobileNetV2 face liveness model trained as described in:

Using MobileNetV2 (Zawar, et al., 2023)

From the open-source project: biometric-technologies/liveness-detection-model

Model licensed under MIT, included with attribution.

Class meaning:

  • 0 = real
  • 1 = spoof

🚀 Usage #

Install #

analyze() takes an Image from the image package, so add both:

dependencies:
  flutter_liveness: ^2.0.0
  image: ^4.5.4

The model runs on LiteRT, which bundles its native libraries, so there is no platform setup beyond these minimums:

Platform Minimum
Flutter 3.38 (Dart 3.10)
Android minSdk 24, the Flutter default
iOS 15.0: set platform :ios, '15.0' in ios/Podfile and the Runner's iOS deployment target
macOS 10.15

Initialize #

import 'package:flutter_liveness/flutter_liveness.dart';
import 'package:image/image.dart' as imglib;

late final FlutterLiveness liveness;

Future<void> initLiveness() async {
  liveness = await FlutterLiveness.create();
}

Analyze a face image #

/// The model input shape is (1, 224, 224, 3)
/// - 1: batch size (single image)
/// - 224: image height
/// - 224: image width
/// - 3: RGB color channels
/// Any size and pixel format is accepted: the image is stretched to
/// 224x224 RGB, so pass a square crop around the face.
Future<void> check(Uint8List faceBytes) async {
  final img = imglib.decodeImage(faceBytes);
  if (img == null) throw Exception('Invalid image');

  final result = await liveness.analyze(img);

  print('Live: ${result.isLive}');
  print('Score (P(spoof)): ${result.score}');
  print('Laplacian: ${result.laplacian}');
  print('Duration: ${result.duration.inMilliseconds} ms');
}

Cleanup #

@override
void dispose() {
  liveness.dispose();
  super.dispose();
}

📊 Output Example #

Live: true
Score (P(spoof)): 0.023
Laplacian: 10239
Duration: 66 ms

⚙️ Settings #

All options, with their defaults:

final liveness = await FlutterLiveness.create(
  options: const LivenessOptions(
    threshold: 0.5, // minimum P(live) = 1 - score; higher is stricter
    applyLaplacianGate: true, // reject blurry or flat images first
    laplacianThreshold: 3000, // edge pixels needed to pass the gate
    laplacePixelThreshold: 50, // Laplacian response that counts as an edge
    useGpu: false, // Android and iOS, if the GPU matches the CPU; see below
    cpuThreads: 1,
  ),
);
  • isLive is true when 1 - score >= threshold. score is the spoof probability, so threshold: 0.9 accepts a face only when score <= 0.1.
  • An image rejected by the Laplacian gate returns isLive: false and score: 1 without running the model. laplacian is the number of edge pixels in the 224x224 image (about 49,000 in total), so compare it with laplacianThreshold to tell a blurry image from a spoof.

GPU #

With useGpu: true on Android or iOS, create() compiles the model for the GPU and scores three test images on both the GPU and the CPU. The GPU is used only if every score matches the CPU's within 0.01, because some GPU drivers return wrong results without an error. liveness.usesGpu tells you which one was chosen.

  • create() blocks the calling isolate while the model compiles for the GPU. In testing that took 2 seconds on an iOS simulator and 14 seconds on an Android emulator, so measure it on your target devices.
  • On Android, each GPU inference also runs on the isolate that called create(), usually the UI isolate, because some Android GPU drivers only work from the thread that compiled the model. CPU inference, and GPU inference on iOS, run on a background isolate.
  • The GPU computes in 32-bit floats. 16-bit floats are faster, but moved this model's scores by up to 0.23 in testing.

❗ Usage Notes #

  • Feed cropped face images (not full camera frames)
  • Create one FlutterLiveness and reuse it: each instance loads the model into native memory
  • Calls are fast (~60–70ms on modern devices)
  • analyze() calls run one at a time, so calling it again before the previous call completes is safe. On a camera stream, skip frames while a call is running instead of queueing every frame
  • Throttle analysis: ~3–5 FPS recommended
  • Do not consider this as the only security mechanism for KYC/financial apps — use it as one signal in a trust pipeline

📱 Integration Tips #

  • Use camera plugin to stream frames
  • Detect faces with MLKit or any detector
  • Crop → send to liveness.analyze()
  • Combine with:
    • Face presence
    • Eye blink / movement (optional future enhancement)
    • Anti-tampering / screen detection

🔐 Disclaimer #

This library provides liveness estimation, not identity verification. Use responsibly and evaluate for your use-case security requirements.

🤝 Acknowledgements #

  • TensorFlow Lite team
  • Biometric Technologies OÜ for the model & research
  • Flutter community
14
likes
160
points
185
downloads

Documentation

API reference

Publisher

verified publisherknottx.dev

Weekly Downloads

On-device face liveness detection for Flutter using a MobileNetV2 model. Detect spoof attacks from photos and videos, no internet required.

Repository (GitHub)
View/report issues

Topics

#liveness #detection #face #tflite #litert

Funding

Consider supporting this project:

github.com

License

MIT (license)

Dependencies

flutter, flutter_litert, image

More

Packages that depend on flutter_liveness