capacitor-plugin-faceantispoofing
v0.0.3
Published
Capacitor plugin for Passive Liveness Detection and Face Anti-Spoofing
Readme
capacitor-plugin-faceantispoofing
Capacitor plugin for Passive Liveness Detection and Face Anti-Spoofing using TensorFlow Lite models.
This plugin has been migrated from cordova-plugin-facespoofing.
Features
- Face Detection: Uses MTCNN (Multi-task Cascaded Convolutional Networks) for accurate face detection
- Anti-Spoofing Detection: Passive liveness detection using TensorFlow Lite models
- Blur Detection: Laplacian variance-based image quality assessment
- Cross-Platform: Support for both Android and iOS
Install
Using npm
npm install capacitor-plugin-faceantispoofing
npx cap syncUsing yarn
yarn add capacitor-plugin-faceantispoofing
npx cap syncUsage
import { FaceAntiSpoofing } from 'capacitor-plugin-faceantispoofing';
// Detect face liveness from base64 image
const detectLiveness = async (base64Image: string) => {
try {
const result = await FaceAntiSpoofing.detect({
image: base64Image // Base64 encoded image (without data:image/...;base64, prefix)
});
console.log('Error:', result.error);
console.log('Liveness:', result.liveness);
console.log('Score:', result.score);
console.log('Threshold:', result.threshold);
console.log('Message:', result.message);
if (result.liveness) {
console.log('Real face detected!');
} else {
console.log('Spoof detected or no face found!');
}
} catch (error) {
console.error('Detection failed:', error);
}
};API
detect(options)
Detects face liveness and performs anti-spoofing detection on the provided image.
| Param | Type | Description |
| ------------ | ----------------------------- | -------------------------------------------------- |
| options | <DetectOptions> | Options for the detection |
| image | <string> | Base64 encoded image data or file URI |
Returns: Promise<DetectionResult>
DetectOptions
| Prop | Type | Description |
| ------- | ------------------- | ---------------------------------------- |
| image | string | Base64 encoded image (without data:image/...;base64, prefix) or file URI starting with file:// |
DetectionResult
| Prop | Type | Description |
| --------- | ------------------- | ---------------------------------------- |
| error | boolean | Whether an error occurred |
| liveness | boolean | Whether the face is detected as real |
| score | string | The liveness score |
| threshold | string | The threshold used for classification |
| message | string | Additional information message |
Detection Flow
- Face Detection: Uses MTCNN (P-Net → R-Net → O-Net) to detect faces in the image
- Face Alignment: Aligns the detected face based on facial landmarks
- Blur Detection: Checks image clarity using Laplacian variance
- Anti-Spoofing: If image is clear enough, runs the anti-spoofing model to classify as real or fake
Response Values
- No faces detected:
error: true,message: "No faces detected" - Image too blurry:
error: false,liveness: false, score contains Laplacian value - Real face detected:
error: false,liveness: true, score < threshold - Spoof detected:
error: false,liveness: false, score >= threshold
Platform Support
| Platform | Supported | | -------- | --------- | | Android | ✅ Yes | | iOS | ✅ Yes | | Web | ❌ No* |
*Web is not supported as native TensorFlow Lite is required.
Dependencies
Android
- Google AI Edge LITErt (TensorFlow Lite) 1.4.0
- minSdkVersion: 24
- compileSdkVersion: 36
iOS
- TensorFlowLiteSwift ~ 2.17.0
- TensorFlowLiteC ~ 2.17.0
- iOS Deployment Target: 15.0
Model Files
This plugin includes the following TensorFlow Lite models:
FaceAntiSpoofing.tflite- Anti-spoofing classification modelpnet.tflite- MTCNN Proposal Networkrnet.tflite- MTCNN Refine Networkonet.tflite- MTCNN Output Network
Threshold Values
- Android: THRESHOLD = 0.5, LAPLACIAN_THRESHOLD = 0
- iOS: threshold = 0.5, laplacianThreshold = 0
Note: These values have been normalized from the original Cordova plugin.
Example Image Format
The plugin accepts images in the following formats:
- Base64 without prefix:
iVBORw0KGgoAAAANSUhEUgAA... - Base64 with data URI:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA... - File URI:
file:///path/to/image.png
Error Handling
try {
const result = await FaceAntiSpoofing.detect({ image: base64Data });
if (result.error) {
console.error('Detection error:', result.message);
return;
}
if (result.liveness) {
// Real face detected
} else {
// Spoof or no face detected
}
} catch (e) {
console.error('Plugin error:', e);
}License
MIT
Credits
Originally based on cordova-plugin-facespoofing by rakaraka029.
