react-native-vision-camera-spoof-detector 1.5.0 → 1.5.1
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/README.md +33 -24
- package/VisionCameraSpoofDetector.podspec +12 -2
- package/ios/HybridSpoofDetectorOutput.swift +72 -157
- package/ios/NcnnLivenessDetector.h +24 -0
- package/ios/NcnnLivenessDetector.mm +248 -0
- package/ios/ncnn.xcframework/Info.plist +27 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/allocator.h +433 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/benchmark.h +28 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/blob.h +33 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/c_api.h +401 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/command.h +111 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/cpu.h +177 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/datareader.h +111 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/expression.h +17 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/gpu.h +566 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/layer.h +212 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/layer_shader_type.h +18 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/layer_shader_type_enum.h +5 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/layer_type.h +19 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/layer_type_enum.h +115 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/mat.h +1868 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/modelbin.h +69 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/ncnn_export.h +43 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/net.h +252 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/option.h +151 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/paramdict.h +66 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/pipeline.h +101 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/pipelinecache.h +71 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/platform.h +372 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/simplemath.h +98 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/simpleocv.h +492 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/simpleomp.h +42 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/simplestl.h +598 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/simplevk.h +2196 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers/ncnn/vulkan_header_fix.h +1674 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Resources/Info.plist +18 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/allocator.h +433 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/benchmark.h +28 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/blob.h +33 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/c_api.h +401 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/command.h +111 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/cpu.h +177 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/datareader.h +111 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/expression.h +17 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/gpu.h +566 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/layer.h +212 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/layer_shader_type.h +18 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/layer_shader_type_enum.h +5 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/layer_type.h +19 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/layer_type_enum.h +115 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/mat.h +1868 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/modelbin.h +69 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/ncnn_export.h +43 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/net.h +252 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/option.h +151 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/paramdict.h +66 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/pipeline.h +101 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/pipelinecache.h +71 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/platform.h +372 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/simplemath.h +98 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/simpleocv.h +492 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/simpleomp.h +42 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/simplestl.h +598 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/simplevk.h +2196 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Headers/ncnn/vulkan_header_fix.h +1674 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/Resources/Info.plist +18 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/A/ncnn +0 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/allocator.h +433 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/benchmark.h +28 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/blob.h +33 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/c_api.h +401 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/command.h +111 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/cpu.h +177 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/datareader.h +111 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/expression.h +17 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/gpu.h +566 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/layer.h +212 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/layer_shader_type.h +18 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/layer_shader_type_enum.h +5 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/layer_type.h +19 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/layer_type_enum.h +115 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/mat.h +1868 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/modelbin.h +69 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/ncnn_export.h +43 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/net.h +252 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/option.h +151 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/paramdict.h +66 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/pipeline.h +101 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/pipelinecache.h +71 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/platform.h +372 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/simplemath.h +98 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/simpleocv.h +492 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/simpleomp.h +42 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/simplestl.h +598 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/simplevk.h +2196 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Headers/ncnn/vulkan_header_fix.h +1674 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/Resources/Info.plist +18 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Versions/Current/ncnn +0 -0
- package/ios/ncnn.xcframework/ios-arm64/ncnn.framework/ncnn +0 -0
- package/ios/openmp.xcframework/Info.plist +27 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Headers/omp.h +521 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Headers/ompx.h +165 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Resources/Info.plist +18 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Versions/A/Headers/omp.h +521 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Versions/A/Headers/ompx.h +165 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Versions/A/Resources/Info.plist +18 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Versions/A/openmp +0 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Versions/Current/Headers/omp.h +521 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Versions/Current/Headers/ompx.h +165 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Versions/Current/Resources/Info.plist +18 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/Versions/Current/openmp +0 -0
- package/ios/openmp.xcframework/ios-arm64/openmp.framework/openmp +0 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,15 +3,15 @@
|
|
|
3
3
|
[](https://badge.fury.io/js/react-native-vision-camera-spoof-detector)
|
|
4
4
|
[](https://github.com/dpraful/react-native-vision-camera-spoof-detector)
|
|
5
5
|
|
|
6
|
-
Face anti-spoofing and liveness scoring for React Native Vision Camera 5. A Nitro-backed native camera output detects eligible faces and reports liveness scores to JavaScript. Android uses the
|
|
6
|
+
Face anti-spoofing and liveness scoring for React Native Vision Camera 5. A Nitro-backed native camera output detects eligible faces and reports liveness scores to JavaScript. Android uses the dual-model NCNN detector with CameraX YUV input; iOS uses the same dual-model NCNN scoring approach with Vision face detection and BGRA camera frames.
|
|
7
7
|
|
|
8
8
|
## Features
|
|
9
9
|
|
|
10
10
|
- Native Android and iOS detection pipeline
|
|
11
11
|
- Vision Camera 5 `CameraOutput` integration via Nitro hybrid objects
|
|
12
|
-
- Dual-model NCNN inference on Android and
|
|
12
|
+
- Dual-model NCNN inference on Android and iOS
|
|
13
13
|
- Native face detection (ML Kit on Android, Vision on iOS); no separate face-detector output is needed
|
|
14
|
-
- Face size, frame-edge, pose, landmark, brightness
|
|
14
|
+
- Face size, frame-edge, pose, landmark, and brightness checks before inference
|
|
15
15
|
- Optional callbacks for scores, detected faces, stable-face capture, and errors
|
|
16
16
|
- Output reset without recreating the camera output
|
|
17
17
|
- Configurable liveness thresholds; uses Vision Camera's selected analysis resolution
|
|
@@ -25,6 +25,7 @@ Face anti-spoofing and liveness scoring for React Native Vision Camera 5. A Nitr
|
|
|
25
25
|
- `react-native-vision-camera-worklets` `^5.0.0`
|
|
26
26
|
- iOS 15.5 or later
|
|
27
27
|
- Android support through the package's native modules and bundled NCNN models
|
|
28
|
+
- iOS NCNN inference requires a physical arm64 device; the bundled NCNN frameworks do not include simulator slices
|
|
28
29
|
|
|
29
30
|
## Installation
|
|
30
31
|
|
|
@@ -54,6 +55,10 @@ pod install
|
|
|
54
55
|
cd ..
|
|
55
56
|
```
|
|
56
57
|
|
|
58
|
+
The camera output uses CPU-based dual-model NCNN inference on physical arm64 iOS devices. The supplied NCNN and OpenMP frameworks do not include simulator slices; simulator builds omit NCNN support and cannot run liveness inference.
|
|
59
|
+
|
|
60
|
+
This applies to the Vision Camera output. The separate legacy `FaceAntiSpoof` NativeModule API remains TensorFlow Lite-backed.
|
|
61
|
+
|
|
57
62
|
Add camera permission text to the app Info.plist:
|
|
58
63
|
|
|
59
64
|
```xml
|
|
@@ -136,7 +141,7 @@ const styles = StyleSheet.create({
|
|
|
136
141
|
});
|
|
137
142
|
```
|
|
138
143
|
|
|
139
|
-
The output is processed natively; the hook returns a `CameraOutput` for Vision Camera's `outputs` array. `
|
|
144
|
+
The output is processed natively; the hook returns a `CameraOutput` for Vision Camera's `outputs` array. `onFacesDetected` is called after face detection for each analyzed frame and receives the eligible faces (possibly an empty array). `onResult` is called only when inference runs: the frame must pass the brightness check and contain exactly one eligible face. It receives a liveness score from `0.0` (spoof) to `1.0` (live). `onStableFaceDetected` fires once when its face-stability and consecutive-live-score checks pass and receives the latest score; reset it with `resetFaceAntiSpoofFrameOutput` to allow another stable-face callback.
|
|
140
145
|
|
|
141
146
|
## API reference
|
|
142
147
|
|
|
@@ -173,21 +178,21 @@ interface UseFaceAntiSpoofFrameOutputOptions {
|
|
|
173
178
|
}
|
|
174
179
|
```
|
|
175
180
|
|
|
176
|
-
The detector
|
|
181
|
+
The detector runs inference only when exactly one eligible face is present. Android uses ML Kit; iOS uses Apple Vision. Both require the face to be at least 18% of the frame width and height, inside a 2% frame margin, within 25 degrees of frontal yaw and pitch, and to have the required eye, nose, and mouth landmarks. Frames rejected by the brightness or face checks clear the consecutive-live-score streak, and a score below `antispoofLevel` resets that streak. The two NCNN models each use a differently expanded face-bounds crop before resizing it to their configured 80 × 80 input dimensions; the crop is not eye-aligned.
|
|
177
182
|
|
|
178
|
-
`onFacesDetected` is optional and receives eligible faces as `{ bounds: { x, y, width, height } }
|
|
183
|
+
`onFacesDetected` is optional and receives eligible faces as `{ bounds: { x, y, width, height } }`, in camera-frame coordinates. `onResult` receives the mean live-class score from the two NCNN models, between `0.0` (spoof) and `1.0` (live); it is a model score, not a calibrated probability. Use it with a policy calibrated for your application. `onStableFaceDetected` is the event that the configured face-stability and consecutive-live-score checks passed, and receives the face bounds and latest score. This event is not a guarantee against presentation attacks.
|
|
179
184
|
|
|
180
185
|
Default output options on both platforms:
|
|
181
186
|
|
|
182
187
|
| Option | Default | Meaning |
|
|
183
188
|
|---|---:|---|
|
|
184
|
-
| `antispoofLevel` | `0.
|
|
189
|
+
| `antispoofLevel` | `0.5` | Minimum liveness score for a frame to count toward the live-frame streak |
|
|
185
190
|
| `stableFrames` | `3` | Eligible face frames required before stable-face notification |
|
|
186
191
|
| `requiredLiveFrames` | `3` | Consecutive score-passing frames required before notification |
|
|
187
192
|
|
|
188
|
-
Vision Camera selects the analysis resolution. Android uses YUV frames and ML Kit; iOS uses BGRA frames and Vision.
|
|
193
|
+
Vision Camera selects the analysis resolution. Android uses YUV frames and ML Kit; iOS uses BGRA frames and Vision. Both camera outputs use the bundled dual-model NCNN liveness detector, but camera input formats and face-detection implementations differ. Calibrate thresholds on representative devices and presentation-attack data rather than assuming identical score distributions.
|
|
189
194
|
|
|
190
|
-
The brightness
|
|
195
|
+
The brightness check is an image-quality gate, not proof of liveness. The default score threshold and frame counts are starting points, not validated biometric security thresholds. Evaluate the complete capture flow against bona fide and attack presentations before relying on it in production.
|
|
191
196
|
|
|
192
197
|
To clear the stable-face state and allow another `onStableFaceDetected` callback:
|
|
193
198
|
|
|
@@ -199,19 +204,19 @@ resetFaceAntiSpoofFrameOutput(antiSpoofOutput);
|
|
|
199
204
|
|
|
200
205
|
### `initializeFaceAntiSpoof()`
|
|
201
206
|
|
|
202
|
-
Optional
|
|
207
|
+
Optional initializer for the legacy iOS `FaceAntiSpoof` React Native module. The Vision Camera output uses the Nitro native output and loads its NCNN models when inference is first run, so this function is not required before creating the output. The legacy module is separate from the camera output and is not available on Android.
|
|
203
208
|
|
|
204
209
|
```ts
|
|
205
210
|
const initialized = await initializeFaceAntiSpoof();
|
|
206
211
|
```
|
|
207
212
|
|
|
208
|
-
Returns `Promise<boolean
|
|
213
|
+
Returns `Promise<boolean>`; it rejects if the legacy module is unavailable or initialization fails.
|
|
209
214
|
|
|
210
215
|
---
|
|
211
216
|
|
|
212
217
|
### `isFaceAntiSpoofAvailable()`
|
|
213
218
|
|
|
214
|
-
Checks whether the legacy `FaceAntiSpoof` React Native module is registered. It does not check camera permission, whether a camera is available, or whether the Nitro camera output can process a frame.
|
|
219
|
+
Checks whether the legacy iOS `FaceAntiSpoof` React Native module is registered. It does not check camera permission, whether a camera is available, or whether the Nitro camera output can process a frame. It returns `false` on platforms where the legacy module is not present; this does not indicate whether the Nitro camera output is available.
|
|
215
220
|
|
|
216
221
|
```ts
|
|
217
222
|
const available = isFaceAntiSpoofAvailable();
|
|
@@ -223,7 +228,7 @@ Returns `boolean`.
|
|
|
223
228
|
|
|
224
229
|
### `FaceAntiSpoofingResult`
|
|
225
230
|
|
|
226
|
-
`FaceAntiSpoofingResult` is a number between `0.0` (spoof) and `1.0` (live).
|
|
231
|
+
`FaceAntiSpoofingResult` is a number between `0.0` (spoof) and `1.0` (live). For the Vision Camera output on both platforms, it is the mean live-class score from the dual-model NCNN liveness detector. Values are model scores, not calibrated probabilities. The separate legacy iOS `FaceAntiSpoof` module remains TensorFlow Lite-backed.
|
|
227
232
|
|
|
228
233
|
## Android and iOS implementation notes
|
|
229
234
|
|
|
@@ -231,27 +236,30 @@ Returns `boolean`.
|
|
|
231
236
|
|
|
232
237
|
- Uses CameraX `ImageAnalysis`, ML Kit face detection, and the bundled dual-model NCNN liveness detector
|
|
233
238
|
- Packs CameraX YUV planes into NV21 for the native liveness detector
|
|
234
|
-
- Uses CPU inference on `arm64-v8a` and `armeabi-v7a
|
|
239
|
+
- Uses CPU inference on `arm64-v8a` and `armeabi-v7a`; no GPU inference is enabled
|
|
235
240
|
- Uses YUV input, a brightness gate, face-size/frame-margin/pose/landmark checks, and the NCNN detector's preprocessing
|
|
236
241
|
- Uses `MirrorMode.AUTO` and Vision Camera's selected analysis resolution
|
|
237
242
|
|
|
238
243
|
### iOS
|
|
239
244
|
|
|
240
|
-
- Uses `AVCaptureVideoDataOutput`, Apple Vision face/landmark detection, and the bundled
|
|
241
|
-
- Uses
|
|
242
|
-
- Uses BGRA input, a brightness gate, face-size/frame-margin/pose/landmark checks, and
|
|
245
|
+
- Uses `AVCaptureVideoDataOutput`, Apple Vision face/landmark detection, and the bundled dual-model NCNN detector
|
|
246
|
+
- Uses CPU inference; GPU/Vulkan inference is disabled
|
|
247
|
+
- Uses BGRA input, a brightness gate, face-size/frame-margin/pose/landmark checks, and two expanded face-bounds crops resized to the model input dimensions
|
|
243
248
|
- Automatically adjusts video mirroring and uses Vision Camera's selected analysis resolution
|
|
249
|
+
- NCNN inference is available on physical arm64 iOS devices. The included NCNN and OpenMP frameworks have no simulator slice, so simulator builds cannot run the camera liveness inference.
|
|
244
250
|
|
|
245
251
|
## Troubleshooting
|
|
246
252
|
|
|
247
|
-
###
|
|
253
|
+
### Legacy `FaceAntiSpoof` module not available
|
|
248
254
|
|
|
249
255
|
```ts
|
|
250
256
|
if (!isFaceAntiSpoofAvailable()) {
|
|
251
|
-
console.warn('
|
|
257
|
+
console.warn('The optional legacy FaceAntiSpoof module is unavailable.');
|
|
252
258
|
}
|
|
253
259
|
```
|
|
254
260
|
|
|
261
|
+
This check applies only to the legacy iOS NativeModule API. The Vision Camera output uses Nitro and does not require this module.
|
|
262
|
+
|
|
255
263
|
### iOS issues
|
|
256
264
|
|
|
257
265
|
- Ensure `NSCameraUsageDescription` exists in the app's Info.plist
|
|
@@ -267,11 +275,12 @@ if (!isFaceAntiSpoofAvailable()) {
|
|
|
267
275
|
|
|
268
276
|
## Platform support
|
|
269
277
|
|
|
270
|
-
| Platform |
|
|
271
|
-
|
|
272
|
-
| Android |
|
|
273
|
-
| iOS
|
|
274
|
-
|
|
|
278
|
+
| Platform | Camera-output inference | GPU acceleration | Notes |
|
|
279
|
+
|----------|-------------------------|-------------------|-------|
|
|
280
|
+
| Android | ✅ Supported | ❌ No | Dual-model NCNN CPU inference on `arm64-v8a` and `armeabi-v7a` |
|
|
281
|
+
| iOS physical arm64 device | ✅ Supported | ❌ No | Dual-model NCNN CPU inference |
|
|
282
|
+
| iOS simulator | ❌ Not supported | ❌ No | The bundled NCNN and OpenMP frameworks have no simulator slice |
|
|
283
|
+
| Web | ❌ Not supported | ❌ No | Native camera processing only |
|
|
275
284
|
|
|
276
285
|
## License
|
|
277
286
|
|
|
@@ -14,7 +14,14 @@ Pod::Spec.new do |s|
|
|
|
14
14
|
s.swift_version = '5.0'
|
|
15
15
|
s.pod_target_xcconfig = {
|
|
16
16
|
'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64',
|
|
17
|
-
'HEADER_SEARCH_PATHS' => '$(inherited) ${PODS_TARGET_SRCROOT}/nitrogen/generated/shared/c++ ${PODS_TARGET_SRCROOT}/nitrogen/generated/ios ${PODS_TARGET_SRCROOT}/nitrogen/generated/ios/c++'
|
|
17
|
+
'HEADER_SEARCH_PATHS' => '$(inherited) ${PODS_TARGET_SRCROOT}/nitrogen/generated/shared/c++ ${PODS_TARGET_SRCROOT}/nitrogen/generated/ios ${PODS_TARGET_SRCROOT}/nitrogen/generated/ios/c++',
|
|
18
|
+
'HEADER_SEARCH_PATHS[sdk=iphoneos*]' => '$(inherited) "${PODS_TARGET_SRCROOT}/ios/ncnn.xcframework/ios-arm64/ncnn.framework/Headers"',
|
|
19
|
+
'GCC_PREPROCESSOR_DEFINITIONS[sdk=iphoneos*]' => '$(inherited) HAS_NCNN=1',
|
|
20
|
+
'GCC_PREPROCESSOR_DEFINITIONS[sdk=iphonesimulator*]' => '$(inherited) HAS_NCNN=0',
|
|
21
|
+
'FRAMEWORK_SEARCH_PATHS[sdk=iphoneos*]' => '$(inherited) "${PODS_TARGET_SRCROOT}/ios/ncnn.xcframework/ios-arm64" "${PODS_TARGET_SRCROOT}/ios/openmp.xcframework/ios-arm64"',
|
|
22
|
+
'OTHER_LDFLAGS[sdk=iphoneos*]' => '$(inherited) -framework ncnn -framework openmp -lc++',
|
|
23
|
+
'CLANG_CXX_LANGUAGE_STANDARD' => 'c++17',
|
|
24
|
+
'CLANG_CXX_LIBRARY' => 'libc++'
|
|
18
25
|
}
|
|
19
26
|
|
|
20
27
|
load 'nitrogen/generated/ios/VisionCameraSpoofDetector+autolinking.rb'
|
|
@@ -26,7 +33,10 @@ Pod::Spec.new do |s|
|
|
|
26
33
|
'nitrogen/generated/ios/**/*.{cpp,mm,swift}',
|
|
27
34
|
'nitrogen/generated/ios/VisionCameraSpoofDetector-Swift-Cxx-Bridge.hpp',
|
|
28
35
|
]
|
|
29
|
-
s.public_header_files =
|
|
36
|
+
s.public_header_files = [
|
|
37
|
+
'ios/NcnnLivenessDetector.h',
|
|
38
|
+
'nitrogen/generated/ios/VisionCameraSpoofDetector-Swift-Cxx-Bridge.hpp',
|
|
39
|
+
]
|
|
30
40
|
s.private_header_files = [
|
|
31
41
|
'nitrogen/generated/shared/**/*.hpp',
|
|
32
42
|
'nitrogen/generated/ios/c++/**/*.{h,hpp}',
|
|
@@ -9,8 +9,6 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
9
9
|
private struct DetectedFace {
|
|
10
10
|
let observation: VNFaceObservation
|
|
11
11
|
let face: SpoofDetectedFace
|
|
12
|
-
let leftEye: CGPoint
|
|
13
|
-
let rightEye: CGPoint
|
|
14
12
|
}
|
|
15
13
|
|
|
16
14
|
private let queue = DispatchQueue(label: "com.faceantispoof.output", qos: .userInitiated)
|
|
@@ -18,8 +16,8 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
18
16
|
private let delegate: SpoofDetectorDelegate
|
|
19
17
|
private let faceDetectionRequest = VNDetectFaceLandmarksRequest()
|
|
20
18
|
private let stateLock = NSLock()
|
|
21
|
-
private
|
|
22
|
-
private var
|
|
19
|
+
private lazy var livenessDetector = NcnnLivenessDetector()
|
|
20
|
+
private var ncnnModelsLoaded = false
|
|
23
21
|
private var isBusy = false
|
|
24
22
|
private var isDisposed = false
|
|
25
23
|
private var captured = false
|
|
@@ -32,8 +30,6 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
32
30
|
private var antispoofLevel: Double { options.antispoofLevel ?? 0.5 }
|
|
33
31
|
private var stableFrames: Int { max(1, Int(options.stableFrames ?? 3)) }
|
|
34
32
|
private var requiredLiveFrames: Int { max(1, Int(options.requiredLiveFrames ?? 3)) }
|
|
35
|
-
private let laplacianThreshold = 1100.0
|
|
36
|
-
private let maxNeuralSpoofScore = 0.1
|
|
37
33
|
let output = AVCaptureVideoDataOutput()
|
|
38
34
|
let requiresAudioInput = false
|
|
39
35
|
let requiresDepthFormat = false
|
|
@@ -113,7 +109,7 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
113
109
|
}
|
|
114
110
|
|
|
115
111
|
guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
|
|
116
|
-
options.onError?(RuntimeError.error(withMessage: "Failed to
|
|
112
|
+
options.onError?(RuntimeError.error(withMessage: "Failed to access the camera frame buffer."))
|
|
117
113
|
return
|
|
118
114
|
}
|
|
119
115
|
|
|
@@ -141,25 +137,39 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
141
137
|
invalidateLiveEvidence()
|
|
142
138
|
return
|
|
143
139
|
}
|
|
144
|
-
guard makeRGBInput(from: pixelBuffer, face: faces[0], into: &modelInput) else {
|
|
145
|
-
invalidateLiveEvidence()
|
|
146
|
-
options.onError?(RuntimeError.error(withMessage: "Failed to prepare the face image for inference."))
|
|
147
|
-
return
|
|
148
|
-
}
|
|
149
|
-
|
|
150
140
|
do {
|
|
151
|
-
|
|
152
|
-
|
|
141
|
+
if !ncnnModelsLoaded {
|
|
142
|
+
guard livenessDetector.loadModels(atPath: try findModelDirectory()) else {
|
|
143
|
+
throw RuntimeError.error(withMessage: livenessDetector.lastError)
|
|
144
|
+
}
|
|
145
|
+
ncnnModelsLoaded = true
|
|
153
146
|
}
|
|
154
|
-
guard
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
147
|
+
guard CVPixelBufferGetPixelFormatType(pixelBuffer) == kCVPixelFormatType_32BGRA,
|
|
148
|
+
let baseAddress = CVPixelBufferGetBaseAddress(pixelBuffer) else {
|
|
149
|
+
throw RuntimeError.error(withMessage: "Camera frame is not available in BGRA format.")
|
|
150
|
+
}
|
|
151
|
+
let bounds = faces[0].face.bounds
|
|
152
|
+
let left = max(0, Int(bounds.x))
|
|
153
|
+
let top = max(0, Int(bounds.y))
|
|
154
|
+
let right = min(CVPixelBufferGetWidth(pixelBuffer), Int(bounds.x + bounds.width))
|
|
155
|
+
let bottom = min(CVPixelBufferGetHeight(pixelBuffer), Int(bounds.y + bounds.height))
|
|
156
|
+
let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer)
|
|
157
|
+
let livenessScore = livenessDetector.score(
|
|
158
|
+
forBGRABytes: baseAddress,
|
|
159
|
+
byteCount: bytesPerRow * CVPixelBufferGetHeight(pixelBuffer),
|
|
160
|
+
width: CVPixelBufferGetWidth(pixelBuffer),
|
|
161
|
+
height: CVPixelBufferGetHeight(pixelBuffer),
|
|
162
|
+
bytesPerRow: bytesPerRow,
|
|
163
|
+
left: left,
|
|
164
|
+
top: top,
|
|
165
|
+
right: right,
|
|
166
|
+
bottom: bottom
|
|
162
167
|
)
|
|
168
|
+
guard livenessScore.isFinite, (0...1).contains(livenessScore) else {
|
|
169
|
+
throw RuntimeError.error(withMessage: livenessDetector.lastError)
|
|
170
|
+
}
|
|
171
|
+
guard isActive() else { return }
|
|
172
|
+
processAntiSpoofResult(livenessScore)
|
|
163
173
|
} catch {
|
|
164
174
|
invalidateLiveEvidence()
|
|
165
175
|
options.onError?(error)
|
|
@@ -184,31 +194,15 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
184
194
|
|
|
185
195
|
let width = CVPixelBufferGetWidth(pixelBuffer)
|
|
186
196
|
let height = CVPixelBufferGetHeight(pixelBuffer)
|
|
187
|
-
let isQuarterTurn = outputOrientation == .right || outputOrientation == .left
|
|
188
|
-
let orientedWidth = isQuarterTurn ? height : width
|
|
189
|
-
let orientedHeight = isQuarterTurn ? width : height
|
|
190
197
|
return (faceDetectionRequest.results ?? []).compactMap { observation in
|
|
191
|
-
guard isEligibleFace(observation, width:
|
|
192
|
-
let landmarks = observation.landmarks,
|
|
193
|
-
let leftEye = landmarkCenter(landmarks.leftEye, in: observation.boundingBox),
|
|
194
|
-
let rightEye = landmarkCenter(landmarks.rightEye, in: observation.boundingBox) else {
|
|
198
|
+
guard isEligibleFace(observation, width: width, height: height) else {
|
|
195
199
|
return nil
|
|
196
200
|
}
|
|
197
|
-
let leftEyePoint = CGPoint(
|
|
198
|
-
x: leftEye.x * CGFloat(orientedWidth),
|
|
199
|
-
y: leftEye.y * CGFloat(orientedHeight)
|
|
200
|
-
)
|
|
201
|
-
let rightEyePoint = CGPoint(
|
|
202
|
-
x: rightEye.x * CGFloat(orientedWidth),
|
|
203
|
-
y: rightEye.y * CGFloat(orientedHeight)
|
|
204
|
-
)
|
|
205
201
|
return DetectedFace(
|
|
206
202
|
observation: observation,
|
|
207
203
|
face: SpoofDetectedFace(
|
|
208
204
|
bounds: makeBufferBounds(from: observation.boundingBox, width: width, height: height)
|
|
209
|
-
)
|
|
210
|
-
leftEye: leftEyePoint,
|
|
211
|
-
rightEye: rightEyePoint
|
|
205
|
+
)
|
|
212
206
|
)
|
|
213
207
|
}
|
|
214
208
|
}
|
|
@@ -257,18 +251,19 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
257
251
|
return centerDistance <= max(previousDiagonal, currentDiagonal) * 0.5
|
|
258
252
|
}
|
|
259
253
|
|
|
260
|
-
private func processAntiSpoofResult(_
|
|
254
|
+
private func processAntiSpoofResult(_ livenessScore: Float) {
|
|
255
|
+
let score = Double(livenessScore)
|
|
256
|
+
let result = AntiSpoofingResult(
|
|
257
|
+
neuralNetworkScore: 1 - score,
|
|
258
|
+
laplacianScore: 0,
|
|
259
|
+
combinedScore: score,
|
|
260
|
+
confidence: score
|
|
261
|
+
)
|
|
261
262
|
lastSpoofResult = result
|
|
262
|
-
if
|
|
263
|
-
consecutiveLiveFrames =
|
|
263
|
+
if score >= antispoofLevel {
|
|
264
|
+
consecutiveLiveFrames = min(requiredLiveFrames, consecutiveLiveFrames + 1)
|
|
264
265
|
} else {
|
|
265
|
-
|
|
266
|
-
let meetsLaplacian = result.laplacianScore >= laplacianThreshold
|
|
267
|
-
if meetsConfidence && meetsLaplacian {
|
|
268
|
-
consecutiveLiveFrames = min(requiredLiveFrames, consecutiveLiveFrames + 1)
|
|
269
|
-
} else {
|
|
270
|
-
consecutiveLiveFrames = 0
|
|
271
|
-
}
|
|
266
|
+
consecutiveLiveFrames = 0
|
|
272
267
|
}
|
|
273
268
|
|
|
274
269
|
options.onResult(result)
|
|
@@ -343,105 +338,6 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
343
338
|
return average >= 24 && average <= 220
|
|
344
339
|
}
|
|
345
340
|
|
|
346
|
-
private func makeRGBInput(
|
|
347
|
-
from pixelBuffer: CVPixelBuffer,
|
|
348
|
-
face: DetectedFace,
|
|
349
|
-
into result: inout [Float]
|
|
350
|
-
) -> Bool {
|
|
351
|
-
guard CVPixelBufferGetPixelFormatType(pixelBuffer) == kCVPixelFormatType_32BGRA,
|
|
352
|
-
let baseAddress = CVPixelBufferGetBaseAddress(pixelBuffer),
|
|
353
|
-
result.count == targetSize * targetSize * 3 else { return false }
|
|
354
|
-
|
|
355
|
-
let sourceWidth = CVPixelBufferGetWidth(pixelBuffer)
|
|
356
|
-
let sourceHeight = CVPixelBufferGetHeight(pixelBuffer)
|
|
357
|
-
let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer)
|
|
358
|
-
let isQuarterTurn = outputOrientation == .right || outputOrientation == .left
|
|
359
|
-
let orientedWidth = isQuarterTurn ? sourceHeight : sourceWidth
|
|
360
|
-
let orientedHeight = isQuarterTurn ? sourceWidth : sourceHeight
|
|
361
|
-
let box = face.observation.boundingBox
|
|
362
|
-
let faceWidth = box.width * CGFloat(orientedWidth)
|
|
363
|
-
let faceHeight = box.height * CGFloat(orientedHeight)
|
|
364
|
-
let cropSide = min(
|
|
365
|
-
min(orientedWidth, orientedHeight),
|
|
366
|
-
max(1, Int(max(faceWidth, faceHeight) * 1.4))
|
|
367
|
-
)
|
|
368
|
-
let halfSide = CGFloat(cropSide) / 2
|
|
369
|
-
let centerX = min(max(box.midX * CGFloat(orientedWidth), halfSide), CGFloat(orientedWidth) - halfSide)
|
|
370
|
-
let centerY = min(max((1 - box.midY) * CGFloat(orientedHeight), halfSide), CGFloat(orientedHeight) - halfSide)
|
|
371
|
-
let cropLeft = min(orientedWidth - cropSide, max(0, Int(centerX - halfSide)))
|
|
372
|
-
let cropTop = min(orientedHeight - cropSide, max(0, Int(centerY - halfSide)))
|
|
373
|
-
let cropRight = cropLeft + cropSide - 1
|
|
374
|
-
let cropBottom = cropTop + cropSide - 1
|
|
375
|
-
let cropCenter = CGFloat(cropSide - 1) / 2
|
|
376
|
-
let rawEyeAngle = atan2(face.rightEye.y - face.leftEye.y, face.rightEye.x - face.leftEye.x)
|
|
377
|
-
let eyeAngle = abs(rawEyeAngle * 180 / .pi) >= 0.5 ? rawEyeAngle : 0
|
|
378
|
-
let cosine = Float(cos(eyeAngle))
|
|
379
|
-
let sine = Float(sin(eyeAngle))
|
|
380
|
-
let bytes = baseAddress.assumingMemoryBound(to: UInt8.self)
|
|
381
|
-
let cropCenterFloat = Float(cropCenter)
|
|
382
|
-
let cropLeftFloat = Float(cropLeft)
|
|
383
|
-
let cropTopFloat = Float(cropTop)
|
|
384
|
-
let cropRightFloat = Float(cropRight)
|
|
385
|
-
let cropBottomFloat = Float(cropBottom)
|
|
386
|
-
let sourceMaxX = Float(sourceWidth - 1)
|
|
387
|
-
let sourceMaxY = Float(sourceHeight - 1)
|
|
388
|
-
let cropOriginX = cropLeftFloat + cropCenterFloat
|
|
389
|
-
let cropOriginY = cropTopFloat + cropCenterFloat
|
|
390
|
-
let sampleScale = Float(cropSide) / Float(targetSize)
|
|
391
|
-
|
|
392
|
-
for targetY in 0..<targetSize {
|
|
393
|
-
let relativeY = (Float(targetY) + 0.5) * sampleScale - 0.5 - cropCenterFloat
|
|
394
|
-
let rowBaseX = cropOriginX - sine * relativeY
|
|
395
|
-
let rowBaseY = cropOriginY + cosine * relativeY
|
|
396
|
-
for targetX in 0..<targetSize {
|
|
397
|
-
let relativeX = (Float(targetX) + 0.5) * sampleScale - 0.5 - cropCenterFloat
|
|
398
|
-
let orientedX = min(cropRightFloat, max(cropLeftFloat, rowBaseX + cosine * relativeX))
|
|
399
|
-
let orientedY = min(cropBottomFloat, max(cropTopFloat, rowBaseY + sine * relativeX))
|
|
400
|
-
let sourceX: Float
|
|
401
|
-
let sourceY: Float
|
|
402
|
-
switch outputOrientation {
|
|
403
|
-
case .right:
|
|
404
|
-
sourceX = orientedY
|
|
405
|
-
sourceY = Float(sourceHeight - 1) - orientedX
|
|
406
|
-
case .down:
|
|
407
|
-
sourceX = sourceMaxX - orientedX
|
|
408
|
-
sourceY = sourceMaxY - orientedY
|
|
409
|
-
case .left:
|
|
410
|
-
sourceX = sourceMaxX - orientedY
|
|
411
|
-
sourceY = orientedX
|
|
412
|
-
default:
|
|
413
|
-
sourceX = orientedX
|
|
414
|
-
sourceY = orientedY
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
let destination = (targetY * targetSize + targetX) * 3
|
|
418
|
-
let boundedX = min(sourceMaxX, max(0, sourceX))
|
|
419
|
-
let boundedY = min(sourceMaxY, max(0, sourceY))
|
|
420
|
-
let x0 = Int(boundedX)
|
|
421
|
-
let y0 = Int(boundedY)
|
|
422
|
-
let x1 = min(x0 + 1, sourceWidth - 1)
|
|
423
|
-
let y1 = min(y0 + 1, sourceHeight - 1)
|
|
424
|
-
let xWeight = boundedX - Float(x0)
|
|
425
|
-
let yWeight = boundedY - Float(y0)
|
|
426
|
-
let topLeft = y0 * bytesPerRow + x0 * 4
|
|
427
|
-
let topRight = y0 * bytesPerRow + x1 * 4
|
|
428
|
-
let bottomLeft = y1 * bytesPerRow + x0 * 4
|
|
429
|
-
let bottomRight = y1 * bytesPerRow + x1 * 4
|
|
430
|
-
|
|
431
|
-
let topBlue = Float(bytes[topLeft]) * (1 - xWeight) + Float(bytes[topRight]) * xWeight
|
|
432
|
-
let bottomBlue = Float(bytes[bottomLeft]) * (1 - xWeight) + Float(bytes[bottomRight]) * xWeight
|
|
433
|
-
let topGreen = Float(bytes[topLeft + 1]) * (1 - xWeight) + Float(bytes[topRight + 1]) * xWeight
|
|
434
|
-
let bottomGreen = Float(bytes[bottomLeft + 1]) * (1 - xWeight) + Float(bytes[bottomRight + 1]) * xWeight
|
|
435
|
-
let topRed = Float(bytes[topLeft + 2]) * (1 - xWeight) + Float(bytes[topRight + 2]) * xWeight
|
|
436
|
-
let bottomRed = Float(bytes[bottomLeft + 2]) * (1 - xWeight) + Float(bytes[bottomRight + 2]) * xWeight
|
|
437
|
-
result[destination] = (topRed * (1 - yWeight) + bottomRed * yWeight) / 255
|
|
438
|
-
result[destination + 1] = (topGreen * (1 - yWeight) + bottomGreen * yWeight) / 255
|
|
439
|
-
result[destination + 2] = (topBlue * (1 - yWeight) + bottomBlue * yWeight) / 255
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
return true
|
|
443
|
-
}
|
|
444
|
-
|
|
445
341
|
private func isEligibleFace(_ face: VNFaceObservation, width: Int, height: Int) -> Bool {
|
|
446
342
|
guard width > 0, height > 0 else { return false }
|
|
447
343
|
let box = face.boundingBox
|
|
@@ -460,14 +356,33 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
460
356
|
return true
|
|
461
357
|
}
|
|
462
358
|
|
|
463
|
-
private func
|
|
464
|
-
|
|
465
|
-
let
|
|
466
|
-
|
|
359
|
+
private func findModelDirectory() throws -> String {
|
|
360
|
+
let bundles = [Bundle.main, Bundle(for: NcnnLivenessDetector.self)]
|
|
361
|
+
let subdirectories: [String?] = ["lib/assets/live", "live", nil]
|
|
362
|
+
for bundle in bundles {
|
|
363
|
+
for subdirectory in subdirectories {
|
|
364
|
+
guard let configPath = bundle.path(
|
|
365
|
+
forResource: "config",
|
|
366
|
+
ofType: "json",
|
|
367
|
+
inDirectory: subdirectory
|
|
368
|
+
) else {
|
|
369
|
+
continue
|
|
370
|
+
}
|
|
371
|
+
let directory = (configPath as NSString).deletingLastPathComponent
|
|
372
|
+
let requiredFiles = [
|
|
373
|
+
"model_1.param",
|
|
374
|
+
"model_1.bin",
|
|
375
|
+
"model_2.param",
|
|
376
|
+
"model_2.bin",
|
|
377
|
+
]
|
|
378
|
+
if requiredFiles.allSatisfy({
|
|
379
|
+
FileManager.default.fileExists(atPath: (directory as NSString).appendingPathComponent($0))
|
|
380
|
+
}) {
|
|
381
|
+
return directory
|
|
382
|
+
}
|
|
383
|
+
}
|
|
467
384
|
}
|
|
468
|
-
|
|
469
|
-
let normalizedY = 1 - (box.minY + average.y / CGFloat(points.count) * box.height)
|
|
470
|
-
return CGPoint(x: normalizedX, y: normalizedY)
|
|
385
|
+
throw RuntimeError.error(withMessage: "NCNN model assets were not found in the application or package bundle.")
|
|
471
386
|
}
|
|
472
387
|
|
|
473
388
|
private func invalidateLiveEvidence() {
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
#import <Foundation/Foundation.h>
|
|
2
|
+
|
|
3
|
+
NS_ASSUME_NONNULL_BEGIN
|
|
4
|
+
|
|
5
|
+
@interface NcnnLivenessDetector : NSObject
|
|
6
|
+
|
|
7
|
+
@property(nonatomic, copy, readonly) NSString *lastError;
|
|
8
|
+
|
|
9
|
+
- (BOOL)loadModelsAtPath:(NSString *)modelPath NS_SWIFT_NAME(loadModels(atPath:));
|
|
10
|
+
|
|
11
|
+
- (float)scoreForBGRABytes:(const void *)bytes
|
|
12
|
+
byteCount:(NSUInteger)byteCount
|
|
13
|
+
width:(NSInteger)width
|
|
14
|
+
height:(NSInteger)height
|
|
15
|
+
bytesPerRow:(NSInteger)bytesPerRow
|
|
16
|
+
left:(NSInteger)left
|
|
17
|
+
top:(NSInteger)top
|
|
18
|
+
right:(NSInteger)right
|
|
19
|
+
bottom:(NSInteger)bottom
|
|
20
|
+
NS_SWIFT_NAME(score(forBGRABytes:byteCount:width:height:bytesPerRow:left:top:right:bottom:));
|
|
21
|
+
|
|
22
|
+
@end
|
|
23
|
+
|
|
24
|
+
NS_ASSUME_NONNULL_END
|