High-performance face anti-spoofing and liveness detection module for React Native Vision Camera. Uses TensorFlow Lite with GPU acceleration and optimized YUV processing.
npm install react-native-vision-camera-spoof-detectorbash
npm install react-native-vision-camera-spoof-detector
or
yarn add react-native-vision-camera-spoof-detector
`
$3
This module requires the following peer dependencies:
`bash
npm install react-native-vision-camera react-native-reanimated react-native-worklets-core
`
Quick Start
$3
`javascript
import { initializeFaceAntiSpoof } from 'react-native-vision-camera-spoof-detector';
// In your app initialization
useEffect(() => {
initializeFaceAntiSpoof()
.then(() => console.log('Face anti-spoof initialized'))
.catch(err => console.error('Init failed:', err));
}, []);
`
$3
`javascript
import { useFrameProcessor } from 'react-native-vision-camera';
import { faceAntiSpoofFrameProcessor } from 'react-native-vision-camera-spoof-detector';
export function CameraScreen() {
const frameProcessor = useFrameProcessor((frame) => {
'worklet';
const result = runAsync(frame, () => {
'worklet';
return faceAntiSpoofFrameProcessor(frame);
});
// Use the result
if (result?.isLive) {
console.log('Live face detected:', result.label);
}
}, []);
return (
device={device}
frameProcessor={frameProcessor}
// ... other props
/>
);
}
`
API Reference
$3
Initializes the face anti-spoofing module. Must be called before using the frame processor.
Returns: Promise - true if initialization was successful
`javascript
const isInitialized = await initializeFaceAntiSpoof();
`
$3
Process a camera frame for face anti-spoofing detection.
Parameters:
- frame (Frame) - Vision Camera frame object
Returns:
`typescript
{
isLive: boolean; // true if face is live
label: string; // "Live Face" or "Spoof Face"
neuralNetworkScore: number; // 0.0-1.0 (lower = more likely live)
laplacianScore: number; // Image quality metric
combinedScore: number; // 0.0-1.0 weighted score
error?: string; // Error message if any
}
`
$3
Check if the native module is available.
`javascript
const available = isFaceAntiSpoofAvailable();
`
Performance
$3
- Multi-Threading: Heavy processing runs on a dedicated background thread, keeping camera smooth
- GPU Acceleration: Uses TensorFlow Lite GPU delegate when available
- NNAPI Support: Falls back to NNAPI for hardware acceleration on older devices
- YUV Direct Processing: No Bitmap conversion - works directly with camera frame data
- Reusable Buffers: Allocates buffers once, reuses for each frame
- Single-Frame Processing: Only processes one frame at a time to prevent queue overflow
$3
| Metric | Value |
|--------|-------|
| Processing Time | ~50-100ms per frame |
| Memory Usage | <30 MB (runtime) |
| Package Size | 20.92 MB |
| Support | Android 21+ |
Architecture
`
VisionCamera (30fps callback)
↓
faceAntiSpoofFrameProcessor()
↓
Submit to background executor
↓
Non-blocking return
↓
Latest result available
`
Processing happens on:
- Single-threaded Executors.newSingleThreadExecutor()
- Daemon thread for proper lifecycle
- Non-blocking callback returns immediately
Configuration
$3
The module automatically selects the best available accelerator:
1. GPU - TensorFlow Lite GPU delegate (fastest)
2. NNAPI - Android Neural Networks API
3. CPU - Fallback for older devices
Check which accelerator is in use:
`javascript
import FaceAntiSpoof from 'react-native-vision-camera-spoof-detector';
FaceAntiSpoof.checkModelStatus()
.then(status => console.log('Accelerator:', status.accelerator));
`
Troubleshooting
$3
`javascript
if (!isFaceAntiSpoofAvailable()) {
console.error('Face anti-spoof not available on this device');
}
`
$3
`javascript
try {
await initializeFaceAntiSpoof();
} catch (error) {
console.error('Init error:', error.message);
}
``