@masumdev/rn-camera-ai-tflite 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +374 -2
- package/lib/commonjs/detector/TFLiteFaceRecognizer.js +690 -0
- package/lib/commonjs/detector/TFLiteFaceRecognizer.js.map +1 -0
- package/lib/commonjs/detector/TFLiteObjectDetector.js +392 -0
- package/lib/commonjs/detector/TFLiteObjectDetector.js.map +1 -0
- package/lib/commonjs/detector/index.js +39 -0
- package/lib/commonjs/detector/index.js.map +1 -0
- package/lib/commonjs/detector/modelLoader.js +139 -0
- package/lib/commonjs/detector/modelLoader.js.map +1 -0
- package/lib/commonjs/index.js +39 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/parser/blazeFaceParser.js +771 -0
- package/lib/commonjs/parser/blazeFaceParser.js.map +1 -0
- package/lib/commonjs/parser/index.js +28 -0
- package/lib/commonjs/parser/index.js.map +1 -0
- package/lib/commonjs/parser/outputParser.js +196 -0
- package/lib/commonjs/parser/outputParser.js.map +1 -0
- package/lib/commonjs/types/index.js +17 -0
- package/lib/commonjs/types/index.js.map +1 -0
- package/lib/commonjs/types/resize-plugin.d.ts +20 -0
- package/lib/commonjs/types/tflite.js +6 -0
- package/lib/commonjs/types/tflite.js.map +1 -0
- package/lib/module/detector/TFLiteFaceRecognizer.js +684 -0
- package/lib/module/detector/TFLiteFaceRecognizer.js.map +1 -0
- package/lib/module/detector/TFLiteObjectDetector.js +386 -0
- package/lib/module/detector/TFLiteObjectDetector.js.map +1 -0
- package/lib/module/detector/index.js +6 -0
- package/lib/module/detector/index.js.map +1 -0
- package/lib/module/detector/modelLoader.js +133 -0
- package/lib/module/detector/modelLoader.js.map +1 -0
- package/lib/module/index.js +6 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/parser/blazeFaceParser.js +756 -0
- package/lib/module/parser/blazeFaceParser.js.map +1 -0
- package/lib/module/parser/index.js +5 -0
- package/lib/module/parser/index.js.map +1 -0
- package/lib/module/parser/outputParser.js +192 -0
- package/lib/module/parser/outputParser.js.map +1 -0
- package/lib/module/types/index.js +4 -0
- package/lib/module/types/index.js.map +1 -0
- package/lib/module/types/resize-plugin.d.ts +20 -0
- package/lib/module/types/tflite.js +4 -0
- package/lib/module/types/tflite.js.map +1 -0
- package/lib/typescript/commonjs/detector/TFLiteFaceRecognizer.d.ts +59 -0
- package/lib/typescript/commonjs/detector/TFLiteFaceRecognizer.d.ts.map +1 -0
- package/lib/typescript/commonjs/detector/TFLiteObjectDetector.d.ts +41 -0
- package/lib/typescript/commonjs/detector/TFLiteObjectDetector.d.ts.map +1 -0
- package/lib/typescript/commonjs/detector/index.d.ts +4 -0
- package/lib/typescript/commonjs/detector/index.d.ts.map +1 -0
- package/lib/typescript/commonjs/detector/modelLoader.d.ts +23 -0
- package/lib/typescript/commonjs/detector/modelLoader.d.ts.map +1 -0
- package/lib/typescript/commonjs/index.d.ts +4 -0
- package/lib/typescript/commonjs/index.d.ts.map +1 -0
- package/lib/typescript/commonjs/package.json +1 -0
- package/lib/typescript/commonjs/parser/blazeFaceParser.d.ts +68 -0
- package/lib/typescript/commonjs/parser/blazeFaceParser.d.ts.map +1 -0
- package/lib/typescript/commonjs/parser/index.d.ts +3 -0
- package/lib/typescript/commonjs/parser/index.d.ts.map +1 -0
- package/lib/typescript/commonjs/parser/outputParser.d.ts +17 -0
- package/lib/typescript/commonjs/parser/outputParser.d.ts.map +1 -0
- package/lib/typescript/commonjs/types/index.d.ts +2 -0
- package/lib/typescript/commonjs/types/index.d.ts.map +1 -0
- package/lib/typescript/commonjs/types/tflite.d.ts +233 -0
- package/lib/typescript/commonjs/types/tflite.d.ts.map +1 -0
- package/lib/typescript/module/detector/TFLiteFaceRecognizer.d.ts +59 -0
- package/lib/typescript/module/detector/TFLiteFaceRecognizer.d.ts.map +1 -0
- package/lib/typescript/module/detector/TFLiteObjectDetector.d.ts +41 -0
- package/lib/typescript/module/detector/TFLiteObjectDetector.d.ts.map +1 -0
- package/lib/typescript/module/detector/index.d.ts +4 -0
- package/lib/typescript/module/detector/index.d.ts.map +1 -0
- package/lib/typescript/module/detector/modelLoader.d.ts +23 -0
- package/lib/typescript/module/detector/modelLoader.d.ts.map +1 -0
- package/lib/typescript/module/index.d.ts +4 -0
- package/lib/typescript/module/index.d.ts.map +1 -0
- package/lib/typescript/module/package.json +1 -0
- package/lib/typescript/module/parser/blazeFaceParser.d.ts +68 -0
- package/lib/typescript/module/parser/blazeFaceParser.d.ts.map +1 -0
- package/lib/typescript/module/parser/index.d.ts +3 -0
- package/lib/typescript/module/parser/index.d.ts.map +1 -0
- package/lib/typescript/module/parser/outputParser.d.ts +17 -0
- package/lib/typescript/module/parser/outputParser.d.ts.map +1 -0
- package/lib/typescript/module/types/index.d.ts +2 -0
- package/lib/typescript/module/types/index.d.ts.map +1 -0
- package/lib/typescript/module/types/tflite.d.ts +233 -0
- package/lib/typescript/module/types/tflite.d.ts.map +1 -0
- package/package.json +121 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ma'sum
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,375 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @masumdev/rn-camera-ai-tflite
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
High-performance TensorFlow Lite adapter for `@masumdev/rn-camera-ai` powered by `react-native-fast-tflite` (Nitro C++).
|
|
4
|
+
|
|
5
|
+
Provides worklet-safe, GPU-accelerated on-device machine learning inference for real-time **Object Detection** (MobileNet SSD / EfficientDet-Lite) and a state-of-the-art 3-model **Face Recognition & 468-Point 3D Face Mesh** pipeline.
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/@masumdev/rn-camera-ai-tflite)
|
|
8
|
+
[](https://github.com/masumrpg/nodejs-lib/blob/main/LICENSE)
|
|
9
|
+
[](https://www.typescriptlang.org/)
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Key Features
|
|
14
|
+
|
|
15
|
+
- **⚡ Hardware Acceleration with Graceful CPU Fallback**:
|
|
16
|
+
- Automatically requests GPU delegates (`android-gpu` OpenCL/OpenGL ES/NNAPI on Android, `core-ml` on iOS).
|
|
17
|
+
- Gracefully falls back to optimized multi-threaded CPU execution without crashing if hardware delegates fail to initialize.
|
|
18
|
+
- **📦 Multi-Model Support**:
|
|
19
|
+
1. **Object Detection**: MobileNet SSD, EfficientDet-Lite, and custom 4-tensor architectures (`boxes`, `classes`, `scores`, `num_detections`).
|
|
20
|
+
2. **Face Detection**: MediaPipe BlazeFace (192×192 full-range or 128×128 short-range) with 896 custom SSD anchors.
|
|
21
|
+
3. **3D Face Mesh**: MediaPipe 468-point 3D landmark regression (`face_landmark.tflite`, 192×192).
|
|
22
|
+
4. **Face Recognition**: MobileFaceNet (112×112) generating 192-dimensional L2-normalized embeddings with Cosine Similarity identity matching.
|
|
23
|
+
- **👤 1-Tap Face Enrollment & Identification**:
|
|
24
|
+
- Register new faces directly from the live camera stream (`enrollLastDetectedFace('Name')`).
|
|
25
|
+
- Real-time identity matching against stored embedding profiles with configurable cosine similarity threshold.
|
|
26
|
+
- **🔄 Universal Asset Resolution**:
|
|
27
|
+
- Supports Metro asset bundles (`require('./model.tflite')`), local filesystem URIs (`file:///...`), and remote URLs (`https://...`).
|
|
28
|
+
- **🛡️ Worklet-Safe Execution**:
|
|
29
|
+
- Implements the `CameraProcessor` interface from `@masumdev/rn-camera-ai`.
|
|
30
|
+
- Runs in dedicated background worklet threads with zero garbage collection spikes and 0% JS-thread lag.
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
## Installation
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
bun add @masumdev/rn-camera-ai @masumdev/rn-camera-ai-tflite
|
|
38
|
+
# or
|
|
39
|
+
npm install @masumdev/rn-camera-ai @masumdev/rn-camera-ai-tflite
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Peer Dependencies
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
bun add react-native-vision-camera react-native-vision-camera-worklets react-native-worklets react-native-nitro-modules react-native-nitro-image react-native-fast-tflite react-native-reanimated
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Metro Asset Configuration (`metro.config.js`)
|
|
49
|
+
|
|
50
|
+
Ensure Metro is configured to bundle `.tflite` model files:
|
|
51
|
+
|
|
52
|
+
```javascript
|
|
53
|
+
const { getDefaultConfig } = require('expo/metro-config');
|
|
54
|
+
|
|
55
|
+
const config = getDefaultConfig(__dirname);
|
|
56
|
+
config.resolver.assetExts.push('tflite');
|
|
57
|
+
|
|
58
|
+
module.exports = config;
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
## Usage 1: Real-Time Object Detection
|
|
64
|
+
|
|
65
|
+
```tsx
|
|
66
|
+
import React, { useEffect, useState } from 'react';
|
|
67
|
+
import { StyleSheet, View, Text } from 'react-native';
|
|
68
|
+
import { CameraAI } from '@masumdev/rn-camera-ai';
|
|
69
|
+
import {
|
|
70
|
+
createTFLiteObjectDetector,
|
|
71
|
+
type TFLiteObjectDetector,
|
|
72
|
+
} from '@masumdev/rn-camera-ai-tflite';
|
|
73
|
+
|
|
74
|
+
const COCO_LABELS = [
|
|
75
|
+
'person', 'bicycle', 'car', 'motorcycle', 'airplane',
|
|
76
|
+
'bus', 'train', 'truck', 'boat', 'traffic light',
|
|
77
|
+
];
|
|
78
|
+
|
|
79
|
+
export function ObjectDetectionScreen() {
|
|
80
|
+
const [detector, setDetector] = useState<TFLiteObjectDetector | null>(null);
|
|
81
|
+
|
|
82
|
+
useEffect(() => {
|
|
83
|
+
let isMounted = true;
|
|
84
|
+
let instance: TFLiteObjectDetector | null = null;
|
|
85
|
+
|
|
86
|
+
async function initDetector() {
|
|
87
|
+
const created = await createTFLiteObjectDetector({
|
|
88
|
+
model: require('../../assets/models/detect.tflite'),
|
|
89
|
+
labels: COCO_LABELS,
|
|
90
|
+
confidenceThreshold: 0.55,
|
|
91
|
+
maxResults: 8,
|
|
92
|
+
useGpu: true,
|
|
93
|
+
targetFps: 20,
|
|
94
|
+
onDelegateFallback: (requested, fallback) => {
|
|
95
|
+
console.warn(`[TFLite] Delegate ${requested} unavailable, using ${fallback}`);
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
if (isMounted) {
|
|
100
|
+
instance = created;
|
|
101
|
+
setDetector(created);
|
|
102
|
+
} else {
|
|
103
|
+
created.dispose?.();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
initDetector();
|
|
108
|
+
|
|
109
|
+
return () => {
|
|
110
|
+
isMounted = false;
|
|
111
|
+
instance?.dispose?.();
|
|
112
|
+
};
|
|
113
|
+
}, []);
|
|
114
|
+
|
|
115
|
+
if (!detector) return null;
|
|
116
|
+
|
|
117
|
+
return (
|
|
118
|
+
<View style={styles.container}>
|
|
119
|
+
<CameraAI
|
|
120
|
+
processor={detector}
|
|
121
|
+
cameraPosition="back"
|
|
122
|
+
resizeMode="cover"
|
|
123
|
+
boxVariant="corners"
|
|
124
|
+
boxColor="#3da441"
|
|
125
|
+
/>
|
|
126
|
+
</View>
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const styles = StyleSheet.create({
|
|
131
|
+
container: { flex: 1, backgroundColor: '#000' },
|
|
132
|
+
});
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
---
|
|
136
|
+
|
|
137
|
+
## Usage 2: Face Recognition & 468-Point 3D Face Mesh
|
|
138
|
+
|
|
139
|
+
The face recognizer uses a cooperative 3-model pipeline:
|
|
140
|
+
1. **BlazeFace (192×192)** detects faces, bounding boxes, and initial landmarks.
|
|
141
|
+
2. **Face Mesh (192×192)** predicts 468 precise 3D facial mesh points (eyes, lips, brows, contours).
|
|
142
|
+
3. **MobileFaceNet (112×112)** generates 192-D biometric embeddings and performs Cosine Similarity comparison against enrolled profiles.
|
|
143
|
+
|
|
144
|
+
```tsx
|
|
145
|
+
import React, { useEffect, useState } from 'react';
|
|
146
|
+
import { StyleSheet, View, Text, Pressable, TextInput } from 'react-native';
|
|
147
|
+
import { CameraAI } from '@masumdev/rn-camera-ai';
|
|
148
|
+
import {
|
|
149
|
+
createTFLiteFaceRecognizer,
|
|
150
|
+
type TFLiteFaceRecognizer,
|
|
151
|
+
type EnrolledFaceProfile,
|
|
152
|
+
} from '@masumdev/rn-camera-ai-tflite';
|
|
153
|
+
|
|
154
|
+
export function FaceRecognitionScreen() {
|
|
155
|
+
const [recognizer, setRecognizer] = useState<TFLiteFaceRecognizer | null>(null);
|
|
156
|
+
const [enrolledName, setEnrolledName] = useState('');
|
|
157
|
+
const [profiles, setProfiles] = useState<EnrolledFaceProfile[]>([]);
|
|
158
|
+
|
|
159
|
+
useEffect(() => {
|
|
160
|
+
let isMounted = true;
|
|
161
|
+
let instance: TFLiteFaceRecognizer | null = null;
|
|
162
|
+
|
|
163
|
+
async function initRecognizer() {
|
|
164
|
+
const created = await createTFLiteFaceRecognizer({
|
|
165
|
+
// 1. Face Detector (BlazeFace 192x192 full range)
|
|
166
|
+
model: require('../../assets/models/face_detection_full_range.tflite'),
|
|
167
|
+
// 2. 468-Point 3D Face Mesh
|
|
168
|
+
faceMeshModel: require('../../assets/models/face_landmark.tflite'),
|
|
169
|
+
// 3. MobileFaceNet 192-D Face Embedder
|
|
170
|
+
embeddingModel: require('../../assets/models/mobilefacenet.tflite'),
|
|
171
|
+
confidenceThreshold: 0.55,
|
|
172
|
+
similarityThreshold: 0.65,
|
|
173
|
+
inputWidth: 192,
|
|
174
|
+
inputHeight: 192,
|
|
175
|
+
targetFps: 24,
|
|
176
|
+
useGpu: true,
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
if (isMounted) {
|
|
180
|
+
instance = created;
|
|
181
|
+
setRecognizer(created);
|
|
182
|
+
} else {
|
|
183
|
+
created.dispose?.();
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
initRecognizer();
|
|
188
|
+
|
|
189
|
+
return () => {
|
|
190
|
+
isMounted = false;
|
|
191
|
+
instance?.dispose?.();
|
|
192
|
+
};
|
|
193
|
+
}, []);
|
|
194
|
+
|
|
195
|
+
// 1-Tap Face Enrollment
|
|
196
|
+
const handleRegisterFace = () => {
|
|
197
|
+
if (!recognizer || !enrolledName.trim()) return;
|
|
198
|
+
const profile = recognizer.enrollLastDetectedFace(enrolledName.trim());
|
|
199
|
+
if (profile) {
|
|
200
|
+
setProfiles(recognizer.getEnrolledFaces());
|
|
201
|
+
setEnrolledName('');
|
|
202
|
+
} else {
|
|
203
|
+
alert('No face clearly visible to register.');
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
if (!recognizer) return null;
|
|
208
|
+
|
|
209
|
+
return (
|
|
210
|
+
<View style={styles.container}>
|
|
211
|
+
<CameraAI
|
|
212
|
+
processor={recognizer}
|
|
213
|
+
cameraPosition="front" // Front selfie camera
|
|
214
|
+
resizeMode="cover"
|
|
215
|
+
boxVariant="corners"
|
|
216
|
+
showMeshLines={true}
|
|
217
|
+
showTesselation={true}
|
|
218
|
+
overlayTheme={{
|
|
219
|
+
boxColor: '#3da441',
|
|
220
|
+
unrecognizedBoxColor: '#f59e0b',
|
|
221
|
+
}}
|
|
222
|
+
/>
|
|
223
|
+
|
|
224
|
+
{/* Floating Enrollment Bar */}
|
|
225
|
+
<View style={styles.floatingBar}>
|
|
226
|
+
<TextInput
|
|
227
|
+
style={styles.input}
|
|
228
|
+
placeholder="Enter Person Name..."
|
|
229
|
+
placeholderTextColor="#999"
|
|
230
|
+
value={enrolledName}
|
|
231
|
+
onChangeText={setEnrolledName}
|
|
232
|
+
/>
|
|
233
|
+
<Pressable style={styles.button} onPress={handleRegisterFace}>
|
|
234
|
+
<Text style={styles.buttonText}>Register Face</Text>
|
|
235
|
+
</Pressable>
|
|
236
|
+
</View>
|
|
237
|
+
</View>
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const styles = StyleSheet.create({
|
|
242
|
+
container: { flex: 1, backgroundColor: '#000' },
|
|
243
|
+
floatingBar: {
|
|
244
|
+
position: 'absolute',
|
|
245
|
+
bottom: 32,
|
|
246
|
+
left: 16,
|
|
247
|
+
right: 16,
|
|
248
|
+
flexDirection: 'row',
|
|
249
|
+
backgroundColor: 'rgba(0,0,0,0.85)',
|
|
250
|
+
padding: 12,
|
|
251
|
+
borderRadius: 12,
|
|
252
|
+
gap: 8,
|
|
253
|
+
},
|
|
254
|
+
input: {
|
|
255
|
+
flex: 1,
|
|
256
|
+
backgroundColor: '#222',
|
|
257
|
+
color: '#fff',
|
|
258
|
+
paddingHorizontal: 12,
|
|
259
|
+
borderRadius: 8,
|
|
260
|
+
},
|
|
261
|
+
button: {
|
|
262
|
+
backgroundColor: '#3da441',
|
|
263
|
+
paddingHorizontal: 16,
|
|
264
|
+
justifyContent: 'center',
|
|
265
|
+
borderRadius: 8,
|
|
266
|
+
},
|
|
267
|
+
buttonText: { color: '#fff', fontWeight: 'bold' },
|
|
268
|
+
});
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
---
|
|
272
|
+
|
|
273
|
+
## API Reference
|
|
274
|
+
|
|
275
|
+
### `createTFLiteObjectDetector(config)`
|
|
276
|
+
|
|
277
|
+
Creates an object detector instance for MobileNet SSD / EfficientDet-Lite models.
|
|
278
|
+
|
|
279
|
+
#### Configuration Options (`TFLiteObjectDetectorConfig`)
|
|
280
|
+
|
|
281
|
+
| Option | Type | Default | Description |
|
|
282
|
+
| :--- | :--- | :--- | :--- |
|
|
283
|
+
| `model` | `TFLiteModelSource` | *Required* | Bundle require ID, file path, or `{ url: string }`. |
|
|
284
|
+
| `labels` | `string[] \| Record<number, string>` | `undefined` | Map of class IDs to class names. |
|
|
285
|
+
| `labelOffset` | `number` | `0` | Offset applied to class index (e.g. `-1` if index 0 is background). |
|
|
286
|
+
| `confidenceThreshold` | `number` | `0.50` | Minimum score threshold in `[0, 1]`. |
|
|
287
|
+
| `maxResults` | `number` | `10` | Maximum number of top detections returned per frame. |
|
|
288
|
+
| `useGpu` | `boolean` | `true` | Requests GPU hardware delegate. |
|
|
289
|
+
| `targetFps` | `number` | `15` | Frame rate throttling limit. |
|
|
290
|
+
| `inputWidth` | `number` | `300` | Input tensor width in pixels. |
|
|
291
|
+
| `inputHeight` | `number` | `300` | Input tensor height in pixels. |
|
|
292
|
+
| `onDelegateFallback` | `(requested, fallback) => void` | `undefined` | Callback invoked on fallback to CPU. |
|
|
293
|
+
|
|
294
|
+
---
|
|
295
|
+
|
|
296
|
+
### `createTFLiteFaceRecognizer(config)`
|
|
297
|
+
|
|
298
|
+
Creates a face detection, 468-point 3D face mesh, and biometric face recognition processor.
|
|
299
|
+
|
|
300
|
+
#### Configuration Options (`TFLiteFaceRecognizerConfig`)
|
|
301
|
+
|
|
302
|
+
| Option | Type | Default | Description |
|
|
303
|
+
| :--- | :--- | :--- | :--- |
|
|
304
|
+
| `model` | `TFLiteModelSource` | *Required* | BlazeFace detection model (`face_detection_full_range.tflite`). |
|
|
305
|
+
| `faceMeshModel` | `TFLiteModelSource` | `undefined` | MediaPipe 468-point 3D Face Mesh model (`face_landmark.tflite`). |
|
|
306
|
+
| `embeddingModel` | `TFLiteModelSource` | `undefined` | MobileFaceNet 192-D face embedding model (`mobilefacenet.tflite`). |
|
|
307
|
+
| `antiSpoofModel` | `TFLiteModelSource` | `undefined` | Optional passive anti-spoofing TFLite model (`MiniFASNet`). |
|
|
308
|
+
| `enableLiveness` | `boolean` | `true` | Computes hybrid Active (EAR, MAR, 3D Head Pose) + Passive (`3D Z-Relief` + `Anti-Moiré Texture`) liveness telemetry (`detection.liveness`). |
|
|
309
|
+
| `livenessThreshold` | `number` | `0.62` | Minimum passive liveness score in `[0, 1]` to classify a face as live (`isLive = true`). |
|
|
310
|
+
| `confidenceThreshold` | `number` | `0.55` | Minimum face detection score in `[0, 1]`. |
|
|
311
|
+
| `similarityThreshold` | `number` | `0.65` | Minimum cosine similarity to match an enrolled identity. |
|
|
312
|
+
| `iouThreshold` | `number` | `0.30` | Non-Maximum Suppression (NMS) IoU overlap limit. |
|
|
313
|
+
| `maxResults` | `number` | `5` | Maximum number of faces to detect simultaneously. |
|
|
314
|
+
| `inputWidth` | `number` | `192` | Detector input width in pixels (192 for full-range, 128 for short-range). |
|
|
315
|
+
| `inputHeight` | `number` | `192` | Detector input height in pixels. |
|
|
316
|
+
| `targetFps` | `number` | `24` | Target frame rate limit in FPS. |
|
|
317
|
+
| `useGpu` | `boolean` | `true` | Requests GPU hardware delegate. |
|
|
318
|
+
| `initialFaces` | `EnrolledFaceProfile[]` | `[]` | Pre-loaded list of enrolled face profiles. |
|
|
319
|
+
|
|
320
|
+
#### Methods on `TFLiteFaceRecognizer`
|
|
321
|
+
|
|
322
|
+
- **`enrollLastDetectedFace(name: string): EnrolledFaceProfile | null`**
|
|
323
|
+
Captures the most recent high-confidence face from the live stream, computes its 192-D embedding, and registers it under `name`.
|
|
324
|
+
- **`enrollFace(name: string, embedding: Float32Array | number[]): EnrolledFaceProfile`**
|
|
325
|
+
Registers a profile with an explicit 192-dimensional embedding vector.
|
|
326
|
+
- **`removeEnrolledFace(id: string): boolean`**
|
|
327
|
+
Removes an enrolled face profile by its unique ID.
|
|
328
|
+
- **`getEnrolledFaces(): EnrolledFaceProfile[]`**
|
|
329
|
+
Returns all currently enrolled biometric profiles.
|
|
330
|
+
- **`clearEnrolledFaces(): void`**
|
|
331
|
+
Deletes all enrolled profiles.
|
|
332
|
+
- **`setConfidenceThreshold(threshold: number): void`**
|
|
333
|
+
Dynamically updates the face detection confidence cutoff.
|
|
334
|
+
- **`setSimilarityThreshold(threshold: number): void`**
|
|
335
|
+
Dynamically updates the cosine similarity recognition threshold.
|
|
336
|
+
- **`setLivenessThreshold(threshold: number): void`**
|
|
337
|
+
Dynamically updates the passive anti-spoofing liveness threshold.
|
|
338
|
+
|
|
339
|
+
---
|
|
340
|
+
|
|
341
|
+
## Hybrid Face Liveness & Anti-Spoofing (`detection.liveness`)
|
|
342
|
+
|
|
343
|
+
When `enableLiveness: true` (default), every face detection includes `detection.liveness` (`FaceLivenessMetrics`), computed 100% on the **Camera Worklet Thread**:
|
|
344
|
+
|
|
345
|
+
- **Active Geometric Liveness (from 468-Point 3D Face Mesh)**:
|
|
346
|
+
- `eyeAspectRatio`, `leftEyeEAR`, `rightEyeEAR`, `isBlinking`: Real-time Eye Aspect Ratio (EAR) blink detection.
|
|
347
|
+
- `mouthAspectRatio`, `isMouthOpen`, `smileRatio`, `isSmiling`: Mouth opening and smile curvature detection.
|
|
348
|
+
- `yawDeg`, `pitchDeg`, `rollDeg`, `headDirection` (`'center' | 'left' | 'right' | 'up' | 'down'`): 3D head pose estimation.
|
|
349
|
+
- **Passive Anti-Spoofing**:
|
|
350
|
+
- `depthReliefScore`: 3D $Z$-depth nose-to-cheek protrusion check rejecting flat 2D photos/screens.
|
|
351
|
+
- `textureScore`: RGB anti-Moiré screen subpixel and specular glare analysis.
|
|
352
|
+
- `passiveScore`, `isLive`, `spoofRisk` (`'low' | 'medium' | 'high'`).
|
|
353
|
+
|
|
354
|
+
---
|
|
355
|
+
|
|
356
|
+
## Low-Level Parsing & Math Functions
|
|
357
|
+
|
|
358
|
+
Exported for building custom inference loops or testing:
|
|
359
|
+
|
|
360
|
+
- **`parseDetectionOutputs(tensors, options): DetectionResult[]`**
|
|
361
|
+
Decodes standard 4-tensor object detection outputs with NMS and coordinate clamping.
|
|
362
|
+
- **`decodeBlazeFaceOutputs(boxesRaw, scoresRaw, anchors, options): DetectionResult[]`**
|
|
363
|
+
Decodes BlazeFace SSD bounding boxes and 6 keypoints.
|
|
364
|
+
- **`decodeFaceMeshOutputs(meshRaw, box): { landmarks: FaceLandmark[]; tightBox: BoundingBox } | null`**
|
|
365
|
+
Decodes 468 3D landmarks ($x, y, z$) from raw tensor output into normalized coordinate space.
|
|
366
|
+
- **`computeFaceLivenessMetrics(rgbBuffer, width, height, box, landmarks, threshold, externalSpoofScore): FaceLivenessMetrics`**
|
|
367
|
+
Computes hybrid Active + Passive face liveness and anti-spoofing telemetry inside a worklet.
|
|
368
|
+
- **`cosineSimilarity(vectorA, vectorB): number`**
|
|
369
|
+
Computes the dot product of two L2-normalized embedding vectors in range `[-1.0, 1.0]`.
|
|
370
|
+
|
|
371
|
+
---
|
|
372
|
+
|
|
373
|
+
## License
|
|
374
|
+
|
|
375
|
+
MIT © [Ma'sum](https://github.com/masumrpg) — [masumdev.id](https://lib.masumdev.id)
|