react-native-vision-camera-spoof-detector 1.0.18 → 1.0.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.npmignore +99 -0
- package/CHANGELOG.md +148 -0
- package/CONTRIBUTING.md +280 -0
- package/README.md +340 -35
- package/index.d.ts +422 -34
- package/package.json +30 -13
package/README.md
CHANGED
|
@@ -1,15 +1,34 @@
|
|
|
1
1
|
# react-native-vision-camera-spoof-detector
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://badge.fury.io/js/react-native-vision-camera-spoof-detector)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
[](https://github.com/jescon-tech/react-native-vision-camera-spoof-detector)
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
High-performance face anti-spoofing and liveness detection module for React Native Vision Camera. Features TensorFlow Lite with GPU acceleration, optimized YUV processing, and real-time blink detection for robust liveness verification.
|
|
6
8
|
|
|
7
|
-
|
|
8
|
-
- **Real-time Performance**: GPU-accelerated processing ensures smooth frame rates.
|
|
9
|
-
- **Easy Integration**: Seamlessly integrates with `react-native-vision-camera`.
|
|
10
|
-
- **YUV Processing**: Optimized for efficient image data handling.
|
|
9
|
+
## 🎯 Features
|
|
11
10
|
|
|
12
|
-
|
|
11
|
+
- **🚀 Real-time Performance**: GPU-accelerated TensorFlow Lite processing for smooth 60fps detection
|
|
12
|
+
- **🎯 High Accuracy**: Advanced ML models for distinguishing live faces from spoofing attempts
|
|
13
|
+
- **👁️ Blink Detection**: Native blink detection for enhanced liveness verification
|
|
14
|
+
- **📱 Optimized YUV Processing**: Efficient image data handling for React Native
|
|
15
|
+
- **🔧 Easy Integration**: Seamlessly integrates with `react-native-vision-camera`
|
|
16
|
+
- **⚡ Face Stability Tracking**: Automatic stable face detection with customizable thresholds
|
|
17
|
+
- **🛡️ Face Centering**: Intelligent face positioning validation in frame
|
|
18
|
+
- **📊 Anti-spoofing Confidence**: Detailed confidence scores with multiple detection models
|
|
19
|
+
- **🔄 Batched Updates**: Optimized state management with minimal re-renders
|
|
20
|
+
|
|
21
|
+
## 📋 Requirements
|
|
22
|
+
|
|
23
|
+
- React Native >= 0.60.0
|
|
24
|
+
- react-native-vision-camera >= 4.6.4
|
|
25
|
+
- react-native-reanimated >= 3.0.0
|
|
26
|
+
- react-native-worklets-core >= 1.0.0
|
|
27
|
+
- react-native-vision-camera-face-detector (optional, for enhanced features)
|
|
28
|
+
|
|
29
|
+
## 📦 Installation
|
|
30
|
+
|
|
31
|
+
### Step 1: Install the package
|
|
13
32
|
|
|
14
33
|
```bash
|
|
15
34
|
npm install react-native-vision-camera-spoof-detector
|
|
@@ -17,17 +36,33 @@ npm install react-native-vision-camera-spoof-detector
|
|
|
17
36
|
yarn add react-native-vision-camera-spoof-detector
|
|
18
37
|
```
|
|
19
38
|
|
|
20
|
-
|
|
39
|
+
### Step 2: Install peer dependencies
|
|
21
40
|
|
|
22
41
|
```bash
|
|
23
|
-
npm install react-native-vision-camera
|
|
42
|
+
npm install react-native-vision-camera react-native-reanimated react-native-worklets-core
|
|
24
43
|
# or
|
|
25
|
-
yarn add react-native-vision-camera
|
|
44
|
+
yarn add react-native-vision-camera react-native-reanimated react-native-worklets-core
|
|
26
45
|
```
|
|
27
46
|
|
|
28
|
-
|
|
47
|
+
### Step 3: Configure Android (if not auto-linked)
|
|
48
|
+
|
|
49
|
+
Add to `android/app/build.gradle`:
|
|
29
50
|
|
|
30
|
-
|
|
51
|
+
```gradle
|
|
52
|
+
dependencies {
|
|
53
|
+
implementation project(':react-native-vision-camera-spoof-detector')
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Step 4: Link native module (for React Native < 0.60)
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
react-native link react-native-vision-camera-spoof-detector
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## 🚀 Quick Start
|
|
64
|
+
|
|
65
|
+
### Simple Usage
|
|
31
66
|
|
|
32
67
|
```javascript
|
|
33
68
|
import React, { useEffect, useState } from 'react';
|
|
@@ -42,7 +77,6 @@ export default function App() {
|
|
|
42
77
|
const [spoofResult, setSpoofResult] = useState(null);
|
|
43
78
|
|
|
44
79
|
useEffect(() => {
|
|
45
|
-
// Initialize the module
|
|
46
80
|
initializeFaceAntiSpoof().then((success) => {
|
|
47
81
|
console.log('FaceAntiSpoof initialized:', success);
|
|
48
82
|
});
|
|
@@ -52,7 +86,7 @@ export default function App() {
|
|
|
52
86
|
'worklet';
|
|
53
87
|
const result = faceAntiSpoofFrameProcessor(frame);
|
|
54
88
|
if (result) {
|
|
55
|
-
|
|
89
|
+
runOnJS(setSpoofResult)(result);
|
|
56
90
|
}
|
|
57
91
|
}, []);
|
|
58
92
|
|
|
@@ -65,7 +99,7 @@ export default function App() {
|
|
|
65
99
|
device={device}
|
|
66
100
|
isActive={true}
|
|
67
101
|
frameProcessor={frameProcessor}
|
|
68
|
-
frameProcessorFps={5}
|
|
102
|
+
frameProcessorFps={5}
|
|
69
103
|
/>
|
|
70
104
|
{spoofResult && (
|
|
71
105
|
<View style={styles.resultContainer}>
|
|
@@ -76,7 +110,7 @@ export default function App() {
|
|
|
76
110
|
Score: {spoofResult.neuralNetworkScore?.toFixed(2)}
|
|
77
111
|
</Text>
|
|
78
112
|
<Text style={styles.resultText}>
|
|
79
|
-
|
|
113
|
+
Label: {spoofResult.label}
|
|
80
114
|
</Text>
|
|
81
115
|
</View>
|
|
82
116
|
)}
|
|
@@ -85,9 +119,7 @@ export default function App() {
|
|
|
85
119
|
}
|
|
86
120
|
|
|
87
121
|
const styles = StyleSheet.create({
|
|
88
|
-
container: {
|
|
89
|
-
flex: 1,
|
|
90
|
-
},
|
|
122
|
+
container: { flex: 1 },
|
|
91
123
|
resultContainer: {
|
|
92
124
|
position: 'absolute',
|
|
93
125
|
bottom: 50,
|
|
@@ -105,34 +137,307 @@ const styles = StyleSheet.create({
|
|
|
105
137
|
});
|
|
106
138
|
```
|
|
107
139
|
|
|
108
|
-
|
|
140
|
+
### Advanced Usage with Full Feature Set
|
|
141
|
+
|
|
142
|
+
```javascript
|
|
143
|
+
import { useCallback, useMemo, useEffect, useRef } from 'react';
|
|
144
|
+
import { Worklets } from 'react-native-worklets-core';
|
|
145
|
+
import { useFrameProcessor } from 'react-native-vision-camera';
|
|
146
|
+
import { useFaceDetector } from 'react-native-vision-camera-face-detector';
|
|
147
|
+
import {
|
|
148
|
+
faceAntiSpoofFrameProcessor,
|
|
149
|
+
initializeFaceAntiSpoof,
|
|
150
|
+
isFaceAntiSpoofAvailable,
|
|
151
|
+
} from 'react-native-vision-camera-spoof-detector';
|
|
152
|
+
|
|
153
|
+
const useFaceDetectionFrameProcessor = ({
|
|
154
|
+
onStableFaceDetected = () => { },
|
|
155
|
+
onFacesUpdate = () => { },
|
|
156
|
+
onLivenessUpdate = () => { },
|
|
157
|
+
onAntiSpoofUpdate = () => { },
|
|
158
|
+
showCodeScanner = false,
|
|
159
|
+
isLoading = false,
|
|
160
|
+
isActive = true,
|
|
161
|
+
livenessLevel = 0,
|
|
162
|
+
antispooflevel = 0.35,
|
|
163
|
+
}) => {
|
|
164
|
+
const { detectFaces } = useFaceDetector({
|
|
165
|
+
performanceMode: 'fast',
|
|
166
|
+
landmarkMode: 'none',
|
|
167
|
+
contourMode: 'none',
|
|
168
|
+
classificationMode: livenessLevel === 1 ? 'all' : 'none',
|
|
169
|
+
minFaceSize: 0.2,
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
const isMounted = useRef(true);
|
|
173
|
+
const antiSpoofInitialized = useRef(false);
|
|
174
|
+
|
|
175
|
+
const initializeAntiSpoof = useCallback(async () => {
|
|
176
|
+
if (antiSpoofInitialized.current) return true;
|
|
177
|
+
try {
|
|
178
|
+
const available = isFaceAntiSpoofAvailable?.();
|
|
179
|
+
if (!available) return false;
|
|
180
|
+
await initializeFaceAntiSpoof();
|
|
181
|
+
antiSpoofInitialized.current = true;
|
|
182
|
+
return true;
|
|
183
|
+
} catch (err) {
|
|
184
|
+
console.error('Anti-spoof initialization error:', err);
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
}, []);
|
|
188
|
+
|
|
189
|
+
useEffect(() => {
|
|
190
|
+
initializeAntiSpoof();
|
|
191
|
+
}, [initializeAntiSpoof]);
|
|
192
|
+
|
|
193
|
+
// Shared state for face tracking
|
|
194
|
+
const sharedState = useMemo(
|
|
195
|
+
() =>
|
|
196
|
+
Worklets.createSharedValue({
|
|
197
|
+
flags: {
|
|
198
|
+
captured: false,
|
|
199
|
+
showCodeScanner: showCodeScanner,
|
|
200
|
+
isActive: isActive,
|
|
201
|
+
hasSingleFace: false,
|
|
202
|
+
isFaceCentered: false,
|
|
203
|
+
},
|
|
204
|
+
antiSpoof: {
|
|
205
|
+
isLive: false,
|
|
206
|
+
confidence: 0,
|
|
207
|
+
consecutiveLiveFrames: 0,
|
|
208
|
+
},
|
|
209
|
+
}),
|
|
210
|
+
[]
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
const frameProcessor = useFrameProcessor(
|
|
214
|
+
(frame) => {
|
|
215
|
+
'worklet';
|
|
216
|
+
|
|
217
|
+
try {
|
|
218
|
+
const detected = detectFaces?.(frame);
|
|
219
|
+
|
|
220
|
+
if (!detected || detected.length === 0) {
|
|
221
|
+
onFacesUpdate?.({ count: 0, progress: 0 });
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (detected.length === 1 && !sharedState.value.flags.captured) {
|
|
226
|
+
const antiSpoofResult = faceAntiSpoofFrameProcessor?.(frame);
|
|
227
|
+
|
|
228
|
+
if (antiSpoofResult?.isLive) {
|
|
229
|
+
sharedState.value.antiSpoof.isLive = true;
|
|
230
|
+
sharedState.value.antiSpoof.confidence = antiSpoofResult.combinedScore;
|
|
231
|
+
onAntiSpoofUpdate?.({
|
|
232
|
+
isLive: true,
|
|
233
|
+
confidence: antiSpoofResult.combinedScore,
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
onFacesUpdate?.({ count: 1, progress: 50 });
|
|
238
|
+
} else {
|
|
239
|
+
onFacesUpdate?.({ count: detected.length, progress: 0 });
|
|
240
|
+
}
|
|
241
|
+
} catch (err) {
|
|
242
|
+
console.error('Frame processing error:', err);
|
|
243
|
+
} finally {
|
|
244
|
+
frame.release?.();
|
|
245
|
+
}
|
|
246
|
+
},
|
|
247
|
+
[detectFaces, isLoading]
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
return {
|
|
251
|
+
frameProcessor,
|
|
252
|
+
sharedState,
|
|
253
|
+
initializeAntiSpoof,
|
|
254
|
+
};
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
export default useFaceDetectionFrameProcessor;
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
## 📚 API Reference
|
|
109
261
|
|
|
110
262
|
### `initializeFaceAntiSpoof()`
|
|
111
263
|
|
|
112
264
|
Initializes the face anti-spoofing module. Must be called before using the frame processor.
|
|
113
265
|
|
|
114
|
-
|
|
266
|
+
```javascript
|
|
267
|
+
const success = await initializeFaceAntiSpoof();
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
**Returns**: `Promise<boolean>` - True if successful
|
|
271
|
+
|
|
272
|
+
---
|
|
273
|
+
|
|
274
|
+
### `isFaceAntiSpoofAvailable()`
|
|
275
|
+
|
|
276
|
+
Checks if the module is available on the device.
|
|
277
|
+
|
|
278
|
+
```javascript
|
|
279
|
+
const available = isFaceAntiSpoofAvailable();
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
**Returns**: `boolean`
|
|
283
|
+
|
|
284
|
+
---
|
|
115
285
|
|
|
116
286
|
### `faceAntiSpoofFrameProcessor(frame)`
|
|
117
287
|
|
|
118
|
-
|
|
288
|
+
Process frame and get anti-spoofing result.
|
|
289
|
+
|
|
290
|
+
```javascript
|
|
291
|
+
const result = faceAntiSpoofFrameProcessor(frame);
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
**Parameters**: `frame` (Vision Camera Frame)
|
|
295
|
+
|
|
296
|
+
**Returns**: `FaceAntiSpoofingResult | null`
|
|
297
|
+
|
|
298
|
+
### `FaceAntiSpoofingResult`
|
|
299
|
+
|
|
300
|
+
```typescript
|
|
301
|
+
interface FaceAntiSpoofingResult {
|
|
302
|
+
isLive: boolean; // Real face (true) or spoof (false)
|
|
303
|
+
label: string; // "Live Face" or "Spoof Face"
|
|
304
|
+
neuralNetworkScore: number; // 0.0-1.0 confidence
|
|
305
|
+
laplacianScore: number; // Image quality score
|
|
306
|
+
combinedScore: number; // Weighted average
|
|
307
|
+
error?: string; // Error message if any
|
|
308
|
+
}
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
## 🔧 Configuration
|
|
312
|
+
|
|
313
|
+
```javascript
|
|
314
|
+
// Anti-spoofing sensitivity (0.0-1.0, lower = more lenient)
|
|
315
|
+
const antispooflevel = 0.35;
|
|
316
|
+
|
|
317
|
+
// Liveness verification mode
|
|
318
|
+
// 0: Anti-spoofing only
|
|
319
|
+
// 1: Anti-spoofing + blink detection
|
|
320
|
+
const livenessLevel = 1;
|
|
321
|
+
|
|
322
|
+
// Customizable thresholds
|
|
323
|
+
const FACE_STABILITY_THRESHOLD = 3; // Frames for stable face
|
|
324
|
+
const FACE_MOVEMENT_THRESHOLD = 15; // Max pixel movement
|
|
325
|
+
const BLINK_THRESHOLD = 0.3; // Eye closure probability
|
|
326
|
+
const REQUIRED_BLINKS = 3; // Blinks for liveness
|
|
327
|
+
const REQUIRED_CONSECUTIVE_LIVE_FRAMES = 3; // Consecutive live frames
|
|
328
|
+
const REAL_LAPLACIAN_THRESHOLD = 3500; // Image quality threshold
|
|
329
|
+
const FACE_CENTER_THRESHOLD_X = 0.2; // X-axis tolerance
|
|
330
|
+
const FACE_CENTER_THRESHOLD_Y = 0.15; // Y-axis tolerance
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
## 🎮 Complete Examples
|
|
334
|
+
|
|
335
|
+
Check the [examples](./examples) folder for:
|
|
336
|
+
- Basic anti-spoofing detection
|
|
337
|
+
- Face detection with liveness
|
|
338
|
+
- Complete capture flow
|
|
339
|
+
- UI components and feedback
|
|
340
|
+
|
|
341
|
+
## 🔍 Attack Detection Capabilities
|
|
342
|
+
|
|
343
|
+
The module detects and prevents:
|
|
344
|
+
- ✅ Print attacks (photos)
|
|
345
|
+
- ✅ Display attacks (screens/tablets)
|
|
346
|
+
- ✅ Mask attacks (with blink detection)
|
|
347
|
+
- ✅ Replay attacks (videos)
|
|
348
|
+
|
|
349
|
+
Performance depends on:
|
|
350
|
+
- Image quality
|
|
351
|
+
- Lighting conditions
|
|
352
|
+
- Face angle and positioning
|
|
353
|
+
- Device camera specs
|
|
354
|
+
|
|
355
|
+
## ⚙️ Performance Tips
|
|
356
|
+
|
|
357
|
+
1. Use `performanceMode: 'fast'` in Face Detector
|
|
358
|
+
2. Module automatically batches state updates
|
|
359
|
+
3. Adjust `FRAME_PROCESSOR_MIN_INTERVAL_MS` as needed
|
|
360
|
+
4. GPU acceleration is used automatically when available
|
|
361
|
+
5. Proper frame release prevents memory leaks
|
|
362
|
+
|
|
363
|
+
## 📱 Platform Support
|
|
364
|
+
|
|
365
|
+
| Platform | Status | GPU | Notes |
|
|
366
|
+
|----------|--------|-----|-------|
|
|
367
|
+
| Android | ✅ Supported | Yes | Fully optimized |
|
|
368
|
+
| iOS | ⏳ In Progress | Yes | Coming soon |
|
|
369
|
+
| Web | ❌ No | N/A | Not applicable |
|
|
370
|
+
|
|
371
|
+
## 🐛 Troubleshooting
|
|
372
|
+
|
|
373
|
+
**Module won't initialize**
|
|
374
|
+
```javascript
|
|
375
|
+
const available = isFaceAntiSpoofAvailable();
|
|
376
|
+
if (!available) {
|
|
377
|
+
console.log('Not available on this device');
|
|
378
|
+
}
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
**Low accuracy**
|
|
382
|
+
- Check lighting conditions
|
|
383
|
+
- Ensure face is centered
|
|
384
|
+
- Adjust `antispooflevel` parameter
|
|
385
|
+
- Verify TensorFlow Lite models are bundled
|
|
386
|
+
|
|
387
|
+
**Performance issues**
|
|
388
|
+
- Reduce frame processing frequency
|
|
389
|
+
- Use lower camera resolution
|
|
390
|
+
- Enable fast performance mode
|
|
391
|
+
- Check device temperature
|
|
392
|
+
|
|
393
|
+
**Face detection fails**
|
|
394
|
+
- Ensure clear face visibility
|
|
395
|
+
- Check camera permissions
|
|
396
|
+
- Verify sufficient lighting
|
|
397
|
+
- Check minimum face size threshold
|
|
398
|
+
|
|
399
|
+
## 📖 Documentation
|
|
400
|
+
|
|
401
|
+
- [Complete API Documentation](./.docs/API.md)
|
|
402
|
+
- [Migration Guide](./.docs/MIGRATION.md)
|
|
403
|
+
- [Best Practices](./.docs/BEST_PRACTICES.md)
|
|
404
|
+
- [Comprehensive Example](./examples/CompleteExampleApp.tsx)
|
|
405
|
+
|
|
406
|
+
## 🤝 Contributing
|
|
407
|
+
|
|
408
|
+
Contributions welcome! See [CONTRIBUTING.md](./CONTRIBUTING.md) for guidelines.
|
|
409
|
+
|
|
410
|
+
## 📄 License
|
|
411
|
+
|
|
412
|
+
MIT License - see [LICENSE](./LICENSE) file for details.
|
|
413
|
+
|
|
414
|
+
## 👨💼 Author
|
|
415
|
+
|
|
416
|
+
**PRAFULDAS M M**
|
|
417
|
+
- Company: JESCON TECHNOLOGIES PVT LTD
|
|
418
|
+
- Location: Thrissur, Kerala, India
|
|
419
|
+
- Email: jescontechnologies@gmail.com
|
|
420
|
+
|
|
421
|
+
## 🔗 Quick Links
|
|
119
422
|
|
|
120
|
-
-
|
|
121
|
-
-
|
|
423
|
+
- [NPM Package](https://www.npmjs.com/package/react-native-vision-camera-spoof-detector)
|
|
424
|
+
- [GitHub Repository](https://github.com/jescon-tech/react-native-vision-camera-spoof-detector)
|
|
425
|
+
- [React Native Vision Camera](https://react-native-vision-camera.com)
|
|
426
|
+
- [TensorFlow Lite](https://www.tensorflow.org/lite)
|
|
122
427
|
|
|
123
|
-
|
|
428
|
+
## 📞 Support & Community
|
|
124
429
|
|
|
125
|
-
-
|
|
126
|
-
-
|
|
127
|
-
-
|
|
128
|
-
- `laplacianScore` (number): Image quality score based on Laplacian variance.
|
|
129
|
-
- `combinedScore` (number): Weighted average score.
|
|
130
|
-
- `error` (string, optional): Error message if detection failed.
|
|
430
|
+
- 🐛 [Report Issues](https://github.com/jescon-tech/react-native-vision-camera-spoof-detector/issues)
|
|
431
|
+
- 💬 [GitHub Discussions](https://github.com/jescon-tech/react-native-vision-camera-spoof-detector/discussions)
|
|
432
|
+
- 📧 Email: jescontechnologies@gmail.com
|
|
131
433
|
|
|
132
|
-
##
|
|
434
|
+
## 🙏 Acknowledgments
|
|
133
435
|
|
|
134
|
-
|
|
436
|
+
Built with:
|
|
437
|
+
- [TensorFlow Lite](https://www.tensorflow.org/lite)
|
|
438
|
+
- [React Native Vision Camera](https://react-native-vision-camera.com)
|
|
439
|
+
- [React Native Worklets](https://docs.swmansion.com/react-native-worklets/)
|
|
135
440
|
|
|
136
|
-
|
|
441
|
+
---
|
|
137
442
|
|
|
138
|
-
JESCON TECHNOLOGIES PVT LTD
|
|
443
|
+
Made with ❤️ by JESCON TECHNOLOGIES PVT LTD
|