react-native-vision-camera-spoof-detector 1.4.1 → 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 +42 -46
- package/VisionCameraSpoofDetector.podspec +12 -2
- package/android/build.gradle +0 -13
- package/android/src/main/java/com/faceantispoof/FaceAntiSpoofPackage.kt +1 -3
- package/android/src/main/java/com/faceantispoof/FaceAntiSpoofingAdvanced.kt +78 -925
- package/android/src/main/java/com/lengdev/livenessdetector/LivenessDetector.kt +228 -0
- package/android/src/main/java/com/margelo/nitro/camera/spoofdetector/HybridSpoofDetectorOutput.kt +17 -31
- package/android/src/main/jniLibs/arm64-v8a/libliveness_detector.so +0 -0
- package/android/src/main/jniLibs/armeabi-v7a/libliveness_detector.so +0 -0
- package/index.d.ts +2 -9
- package/index.js +6 -6
- package/ios/FaceAntiSpoofModule.m +3 -2
- package/ios/HybridSpoofDetectorOutput.swift +73 -158
- 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/lib/assets/live/config.json +1 -0
- package/lib/assets/live/model_1.bin +0 -0
- package/lib/assets/live/model_1.param +214 -0
- package/lib/assets/live/model_2.bin +0 -0
- package/lib/assets/live/model_2.param +238 -0
- package/nitrogen/generated/android/c++/JSpoofDetectorOutputOptions.hpp +1 -9
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/camera/spoofdetector/SpoofDetectorOutputOptions.kt +4 -14
- package/nitrogen/generated/ios/swift/SpoofDetectorOutputOptions.swift +1 -37
- package/nitrogen/generated/shared/c++/SpoofDetectorOutputOptions.hpp +1 -9
- package/package.json +1 -2
- package/src/specs/SpoofDetectorFactory.nitro.ts +0 -2
- package/android/fix-prefab.gradle +0 -4
- package/android/src/main/java/com/faceantispoof/AcceleratorConfig.kt +0 -97
- package/android/src/main/java/com/faceantispoof/FaceAntiSpoofModule.kt +0 -242
- package/android/src/main/res/values/strings.xml +0 -3
- package/lib/assets/FaceAntiSpoofing.tflite +0 -0
- package/lib/assets/model_spec.json +0 -16
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
package com.lengdev.livenessdetector
|
|
2
|
+
|
|
3
|
+
import android.content.res.AssetManager
|
|
4
|
+
import android.util.Log
|
|
5
|
+
import androidx.annotation.Keep
|
|
6
|
+
import org.json.JSONArray
|
|
7
|
+
import java.io.BufferedReader
|
|
8
|
+
import java.io.InputStreamReader
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Data class representing a detected face bounding box
|
|
12
|
+
*/
|
|
13
|
+
@Keep
|
|
14
|
+
data class FaceBox(
|
|
15
|
+
val left: Int,
|
|
16
|
+
val top: Int,
|
|
17
|
+
val right: Int,
|
|
18
|
+
val bottom: Int,
|
|
19
|
+
var confidence: Float
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Model configuration matching the C++ ModelConfig structure
|
|
24
|
+
*/
|
|
25
|
+
@Keep
|
|
26
|
+
data class ModelConfig(
|
|
27
|
+
var scale: Float = 0F,
|
|
28
|
+
var shift_x: Float = 0F,
|
|
29
|
+
var shift_y: Float = 0F,
|
|
30
|
+
var height: Int = 0,
|
|
31
|
+
var width: Int = 0,
|
|
32
|
+
var name: String = "",
|
|
33
|
+
var org_resize: Boolean = false
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Kotlin wrapper for the native LivenessDetector
|
|
38
|
+
* Created by Ly Houleng
|
|
39
|
+
*/
|
|
40
|
+
@Keep
|
|
41
|
+
class LivenessDetector {
|
|
42
|
+
|
|
43
|
+
companion object {
|
|
44
|
+
const val TAG = "LivenessDetector"
|
|
45
|
+
|
|
46
|
+
private val nativeLibraryLoaded = try {
|
|
47
|
+
System.loadLibrary("liveness_detector")
|
|
48
|
+
Log.d(TAG, "Native library 'liveness_detector' loaded successfully")
|
|
49
|
+
true
|
|
50
|
+
} catch (error: UnsatisfiedLinkError) {
|
|
51
|
+
Log.e(TAG, "Failed to load native library 'liveness_detector'", error)
|
|
52
|
+
false
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
@Keep
|
|
57
|
+
private var nativeHandler: Long = 0
|
|
58
|
+
private var modelLoaded = false
|
|
59
|
+
|
|
60
|
+
init {
|
|
61
|
+
check(nativeLibraryLoaded) {
|
|
62
|
+
"Failed to load liveness_detector native library for this ABI"
|
|
63
|
+
}
|
|
64
|
+
nativeHandler = allocate()
|
|
65
|
+
if (nativeHandler == 0L) {
|
|
66
|
+
throw RuntimeException("Failed to create native LivenessDetector instance")
|
|
67
|
+
}
|
|
68
|
+
Log.d(TAG, "Native instance created: $nativeHandler")
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Load models from Android assets
|
|
73
|
+
* Reads configuration from assets/live/config.json
|
|
74
|
+
* @param assetManager Android AssetManager
|
|
75
|
+
* @return 0 on success, negative value on error
|
|
76
|
+
*/
|
|
77
|
+
@Synchronized
|
|
78
|
+
fun loadModel(assetManager: AssetManager): Int {
|
|
79
|
+
if (modelLoaded) return 0
|
|
80
|
+
|
|
81
|
+
val configs = parseConfig(assetManager)
|
|
82
|
+
|
|
83
|
+
if (configs.isEmpty()) {
|
|
84
|
+
Log.e(TAG, "Failed to parse model config")
|
|
85
|
+
return -1
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
Log.d(TAG, "Loaded ${configs.size} model configurations")
|
|
89
|
+
val status = nativeLoadModel(assetManager, configs)
|
|
90
|
+
modelLoaded = status == 0
|
|
91
|
+
return status
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Detect liveness from tightly packed NV21 camera data.
|
|
96
|
+
* @param yuv YUV420 image data from camera
|
|
97
|
+
* @param previewWidth Camera preview width
|
|
98
|
+
* @param previewHeight Camera preview height
|
|
99
|
+
* @param orientation Camera orientation (0, 90, 180, 270)
|
|
100
|
+
* @param faceBox Detected face bounding box
|
|
101
|
+
* @return Liveness score (0.0 = fake, 1.0 = real)
|
|
102
|
+
* @throws IllegalArgumentException if YUV data size or face bounds are invalid
|
|
103
|
+
*/
|
|
104
|
+
@Synchronized
|
|
105
|
+
fun detect(
|
|
106
|
+
yuv: ByteArray,
|
|
107
|
+
previewWidth: Int,
|
|
108
|
+
previewHeight: Int,
|
|
109
|
+
orientation: Int,
|
|
110
|
+
faceBox: FaceBox
|
|
111
|
+
): Float {
|
|
112
|
+
check(modelLoaded) { "Liveness detector models are not loaded" }
|
|
113
|
+
require(previewWidth > 0 && previewHeight > 0) { "Frame dimensions must be positive" }
|
|
114
|
+
require(orientation in setOf(0, 90, 180, 270)) { "Invalid frame orientation: $orientation" }
|
|
115
|
+
val orientedWidth = if (orientation == 90 || orientation == 270) previewHeight else previewWidth
|
|
116
|
+
val orientedHeight = if (orientation == 90 || orientation == 270) previewWidth else previewHeight
|
|
117
|
+
require(faceBox.left >= 0 && faceBox.top >= 0 &&
|
|
118
|
+
faceBox.right > faceBox.left && faceBox.bottom > faceBox.top &&
|
|
119
|
+
faceBox.right <= orientedWidth && faceBox.bottom <= orientedHeight
|
|
120
|
+
) { "Face bounds are outside the frame" }
|
|
121
|
+
|
|
122
|
+
val expectedSize = previewWidth.toLong() * previewHeight * 3 / 2
|
|
123
|
+
require(expectedSize <= Int.MAX_VALUE) { "Frame dimensions are too large" }
|
|
124
|
+
require(yuv.size.toLong() == expectedSize) {
|
|
125
|
+
"Invalid YUV data size. Expected: $expectedSize, got: ${yuv.size}"
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return nativeDetectYuv(
|
|
129
|
+
yuv,
|
|
130
|
+
previewWidth,
|
|
131
|
+
previewHeight,
|
|
132
|
+
orientation,
|
|
133
|
+
faceBox.left,
|
|
134
|
+
faceBox.top,
|
|
135
|
+
faceBox.right,
|
|
136
|
+
faceBox.bottom
|
|
137
|
+
)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Release native resources
|
|
143
|
+
* Should be called when the detector is no longer needed
|
|
144
|
+
*/
|
|
145
|
+
@Synchronized
|
|
146
|
+
fun destroy() {
|
|
147
|
+
if (nativeHandler != 0L) {
|
|
148
|
+
deallocate()
|
|
149
|
+
Log.d(TAG, "Native instance destroyed")
|
|
150
|
+
nativeHandler = 0
|
|
151
|
+
modelLoaded = false
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
fun close() = destroy()
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Parse model configuration from assets/live/config.json
|
|
159
|
+
* Expected JSON format:
|
|
160
|
+
* [
|
|
161
|
+
* {
|
|
162
|
+
* "name": "model1",
|
|
163
|
+
* "width": 256,
|
|
164
|
+
* "height": 256,
|
|
165
|
+
* "scale": 1.0,
|
|
166
|
+
* "shift_x": 0.0,
|
|
167
|
+
* "shift_y": 0.0,
|
|
168
|
+
* "org_resize": false
|
|
169
|
+
* }
|
|
170
|
+
* ]
|
|
171
|
+
*/
|
|
172
|
+
private fun parseConfig(assetManager: AssetManager): List<ModelConfig> {
|
|
173
|
+
return try {
|
|
174
|
+
val inputStream = assetManager.open("live/config.json")
|
|
175
|
+
val br = BufferedReader(InputStreamReader(inputStream))
|
|
176
|
+
val jsonContent = br.use { it.readText() }
|
|
177
|
+
|
|
178
|
+
val jsonArray = JSONArray(jsonContent)
|
|
179
|
+
val configList = mutableListOf<ModelConfig>()
|
|
180
|
+
|
|
181
|
+
for (i in 0 until jsonArray.length()) {
|
|
182
|
+
val jsonObject = jsonArray.getJSONObject(i)
|
|
183
|
+
val config = ModelConfig().apply {
|
|
184
|
+
name = jsonObject.optString("name", "")
|
|
185
|
+
width = jsonObject.optInt("width", 256)
|
|
186
|
+
height = jsonObject.optInt("height", 256)
|
|
187
|
+
scale = jsonObject.optDouble("scale", 1.0).toFloat()
|
|
188
|
+
shift_x = jsonObject.optDouble("shift_x", 0.0).toFloat()
|
|
189
|
+
shift_y = jsonObject.optDouble("shift_y", 0.0).toFloat()
|
|
190
|
+
org_resize = jsonObject.optBoolean("org_resize", false)
|
|
191
|
+
}
|
|
192
|
+
configList.add(config)
|
|
193
|
+
Log.d(TAG, "Loaded config: ${config.name} (${config.width}x${config.height})")
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
configList
|
|
197
|
+
} catch (e: Exception) {
|
|
198
|
+
Log.e(TAG, "Error parsing config.json", e)
|
|
199
|
+
emptyList()
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
///////////////////////////////////// Native Methods ////////////////////////////////////
|
|
204
|
+
|
|
205
|
+
@Keep
|
|
206
|
+
private external fun allocate(): Long
|
|
207
|
+
|
|
208
|
+
@Keep
|
|
209
|
+
private external fun deallocate()
|
|
210
|
+
|
|
211
|
+
@Keep
|
|
212
|
+
private external fun nativeLoadModel(
|
|
213
|
+
assetManager: AssetManager,
|
|
214
|
+
configs: List<ModelConfig>
|
|
215
|
+
): Int
|
|
216
|
+
|
|
217
|
+
@Keep
|
|
218
|
+
private external fun nativeDetectYuv(
|
|
219
|
+
yuv: ByteArray,
|
|
220
|
+
previewWidth: Int,
|
|
221
|
+
previewHeight: Int,
|
|
222
|
+
orientation: Int,
|
|
223
|
+
left: Int,
|
|
224
|
+
top: Int,
|
|
225
|
+
right: Int,
|
|
226
|
+
bottom: Int
|
|
227
|
+
): Float
|
|
228
|
+
}
|
package/android/src/main/java/com/margelo/nitro/camera/spoofdetector/HybridSpoofDetectorOutput.kt
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
package com.margelo.nitro.camera.spoofdetector
|
|
2
2
|
|
|
3
3
|
import android.graphics.Matrix
|
|
4
|
-
import android.graphics.PointF
|
|
5
4
|
import android.graphics.Rect
|
|
6
5
|
import android.graphics.RectF
|
|
7
6
|
import androidx.annotation.OptIn
|
|
@@ -65,11 +64,9 @@ class HybridSpoofDetectorOutput(
|
|
|
65
64
|
)
|
|
66
65
|
private val faceDetectorTask = java.util.concurrent.atomic.AtomicReference<Task<List<Face>>?>()
|
|
67
66
|
private val stateLock = Any()
|
|
68
|
-
private val antispoofLevel = options.antispoofLevel ?: 0.
|
|
67
|
+
private val antispoofLevel = options.antispoofLevel ?: 0.5
|
|
69
68
|
private val stableFrames = (options.stableFrames ?: 3.0).toInt().coerceAtLeast(1)
|
|
70
69
|
private val requiredLiveFrames = (options.requiredLiveFrames ?: 3.0).toInt().coerceAtLeast(1)
|
|
71
|
-
private val laplacianThreshold = options.laplacianThreshold ?: 1100.0
|
|
72
|
-
private val maxNeuralSpoofScore = options.maxNeuralSpoofScore ?: 0.1
|
|
73
70
|
private var captured = false
|
|
74
71
|
private var stableCount = 0
|
|
75
72
|
private var lastFaceBounds: SpoofDetectedFaceBounds? = null
|
|
@@ -79,6 +76,7 @@ class HybridSpoofDetectorOutput(
|
|
|
79
76
|
private var lastSpoofResult: AntiSpoofingResult? = null
|
|
80
77
|
private var imageAnalysis: ImageAnalysis? = null
|
|
81
78
|
private companion object {
|
|
79
|
+
private const val TAG = "SpoofDetectorOutput"
|
|
82
80
|
private const val MIN_FACE_WIDTH_RATIO = 0.18f
|
|
83
81
|
private const val MIN_FACE_HEIGHT_RATIO = 0.18f
|
|
84
82
|
private const val FACE_FRAME_MARGIN_RATIO = 0.02f
|
|
@@ -133,10 +131,6 @@ class HybridSpoofDetectorOutput(
|
|
|
133
131
|
}
|
|
134
132
|
|
|
135
133
|
val face = eligibleFaces.single()
|
|
136
|
-
val leftEye = face.getLandmark(com.google.mlkit.vision.face.FaceLandmark.LEFT_EYE)?.position
|
|
137
|
-
?: throw IllegalStateException("Face is missing the left eye landmark")
|
|
138
|
-
val rightEye = face.getLandmark(com.google.mlkit.vision.face.FaceLandmark.RIGHT_EYE)?.position
|
|
139
|
-
?: throw IllegalStateException("Face is missing the right eye landmark")
|
|
140
134
|
val imagePlanes = image.planes
|
|
141
135
|
if (imagePlanes.size < planeBuffers.size) {
|
|
142
136
|
throw IllegalStateException("Expected Y, U and V camera planes")
|
|
@@ -147,7 +141,7 @@ class HybridSpoofDetectorOutput(
|
|
|
147
141
|
planeRowStrides[planeIndex] = plane.rowStride
|
|
148
142
|
planePixelStrides[planeIndex] = plane.pixelStride
|
|
149
143
|
}
|
|
150
|
-
val
|
|
144
|
+
val livenessScore = synchronized(detectorLock) {
|
|
151
145
|
if (disposed.get()) return
|
|
152
146
|
detector.advancedAntiSpoofing(
|
|
153
147
|
planeBuffers,
|
|
@@ -157,18 +151,9 @@ class HybridSpoofDetectorOutput(
|
|
|
157
151
|
planePixelStrides,
|
|
158
152
|
image.imageInfo.rotationDegrees,
|
|
159
153
|
RectF(face.boundingBox),
|
|
160
|
-
PointF(leftEye.x, leftEye.y),
|
|
161
|
-
PointF(rightEye.x, rightEye.y),
|
|
162
154
|
)
|
|
163
155
|
}
|
|
164
|
-
processAntiSpoofResult(
|
|
165
|
-
AntiSpoofingResult(
|
|
166
|
-
result.neuralNetworkScore.toDouble(),
|
|
167
|
-
result.laplacianScore.toDouble(),
|
|
168
|
-
result.combinedScore.toDouble(),
|
|
169
|
-
result.confidence.toDouble(),
|
|
170
|
-
)
|
|
171
|
-
)
|
|
156
|
+
processAntiSpoofResult(livenessScore)
|
|
172
157
|
} catch (error: Throwable) {
|
|
173
158
|
invalidateLiveEvidence()
|
|
174
159
|
options.onError?.invoke(error)
|
|
@@ -257,23 +242,24 @@ class HybridSpoofDetectorOutput(
|
|
|
257
242
|
centerDistance <= maxFaceDiagonal * MAX_FACE_CENTER_SHIFT_RATIO
|
|
258
243
|
}
|
|
259
244
|
|
|
260
|
-
private fun processAntiSpoofResult(
|
|
245
|
+
private fun processAntiSpoofResult(livenessScore: Float) {
|
|
246
|
+
require(livenessScore.isFinite() && livenessScore in 0f..1f) {
|
|
247
|
+
"NCNN returned an invalid liveness score: $livenessScore"
|
|
248
|
+
}
|
|
249
|
+
val result = AntiSpoofingResult(
|
|
250
|
+
1.0 - livenessScore,
|
|
251
|
+
0.0,
|
|
252
|
+
livenessScore.toDouble(),
|
|
253
|
+
livenessScore.toDouble(),
|
|
254
|
+
)
|
|
261
255
|
var capture: Pair<SpoofDetectedFaceBounds, AntiSpoofingResult>? = null
|
|
262
256
|
|
|
263
257
|
synchronized(stateLock) {
|
|
264
258
|
lastSpoofResult = result
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
consecutiveLiveFrames = 0
|
|
259
|
+
consecutiveLiveFrames = if (livenessScore >= antispoofLevel) {
|
|
260
|
+
min(requiredLiveFrames, consecutiveLiveFrames + 1)
|
|
268
261
|
} else {
|
|
269
|
-
|
|
270
|
-
val meetsLaplacian = result.laplacianScore >= laplacianThreshold
|
|
271
|
-
val framePassesLiveness = meetsConfidence && meetsLaplacian
|
|
272
|
-
consecutiveLiveFrames = if (framePassesLiveness) {
|
|
273
|
-
min(requiredLiveFrames, consecutiveLiveFrames + 1)
|
|
274
|
-
} else {
|
|
275
|
-
0
|
|
276
|
-
}
|
|
262
|
+
0
|
|
277
263
|
}
|
|
278
264
|
|
|
279
265
|
capture = readyToCaptureLocked()
|
|
Binary file
|
|
Binary file
|
package/index.d.ts
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
import type { CameraOutput } from 'react-native-vision-camera';
|
|
2
2
|
|
|
3
|
-
export
|
|
4
|
-
neuralNetworkScore: number;
|
|
5
|
-
laplacianScore: number;
|
|
6
|
-
combinedScore: number;
|
|
7
|
-
confidence: number;
|
|
8
|
-
}
|
|
3
|
+
export type FaceAntiSpoofingResult = number;
|
|
9
4
|
|
|
10
5
|
export interface FaceRect {
|
|
11
6
|
x: number;
|
|
@@ -22,8 +17,6 @@ export interface UseFaceAntiSpoofFrameOutputOptions {
|
|
|
22
17
|
antispoofLevel?: number;
|
|
23
18
|
stableFrames?: number;
|
|
24
19
|
requiredLiveFrames?: number;
|
|
25
|
-
laplacianThreshold?: number;
|
|
26
|
-
maxNeuralSpoofScore?: number;
|
|
27
20
|
onFacesDetected?: (faces: SpoofDetectedFace[]) => void;
|
|
28
21
|
onResult?: (result: FaceAntiSpoofingResult) => void;
|
|
29
22
|
onStableFaceDetected?: (
|
|
@@ -58,7 +51,7 @@ export interface FaceAntiSpoofModule {
|
|
|
58
51
|
height: number,
|
|
59
52
|
rowStrides: number[],
|
|
60
53
|
pixelStrides: number[],
|
|
61
|
-
): Promise<
|
|
54
|
+
): Promise<number>;
|
|
62
55
|
cleanup?(): Promise<boolean>;
|
|
63
56
|
getAccelerator?(): Promise<string>;
|
|
64
57
|
}
|
package/index.js
CHANGED
|
@@ -37,16 +37,14 @@ export const useFaceAntiSpoofFrameOutput = ({
|
|
|
37
37
|
antispoofLevel: options.antispoofLevel,
|
|
38
38
|
stableFrames: options.stableFrames,
|
|
39
39
|
requiredLiveFrames: options.requiredLiveFrames,
|
|
40
|
-
laplacianThreshold: options.laplacianThreshold,
|
|
41
|
-
maxNeuralSpoofScore: options.maxNeuralSpoofScore,
|
|
42
40
|
onResult(result) {
|
|
43
|
-
resultRef.current?.(result);
|
|
41
|
+
resultRef.current?.(getLivenessScore(result));
|
|
44
42
|
},
|
|
45
43
|
onFacesDetected: hasFaceCallback
|
|
46
44
|
? (faces) => facesRef.current?.(faces)
|
|
47
45
|
: undefined,
|
|
48
46
|
onStableFaceDetected: hasStableFaceCallback
|
|
49
|
-
? (face, result) => stableFaceRef.current?.(face, result)
|
|
47
|
+
? (face, result) => stableFaceRef.current?.(face, getLivenessScore(result))
|
|
50
48
|
: undefined,
|
|
51
49
|
onError(error) {
|
|
52
50
|
errorRef.current?.(error);
|
|
@@ -57,14 +55,16 @@ export const useFaceAntiSpoofFrameOutput = ({
|
|
|
57
55
|
options.antispoofLevel,
|
|
58
56
|
options.stableFrames,
|
|
59
57
|
options.requiredLiveFrames,
|
|
60
|
-
options.laplacianThreshold,
|
|
61
|
-
options.maxNeuralSpoofScore,
|
|
62
58
|
hasFaceCallback,
|
|
63
59
|
hasStableFaceCallback,
|
|
64
60
|
],
|
|
65
61
|
);
|
|
66
62
|
};
|
|
67
63
|
|
|
64
|
+
const getLivenessScore = (result) => (
|
|
65
|
+
typeof result === 'number' ? result : result.confidence
|
|
66
|
+
);
|
|
67
|
+
|
|
68
68
|
export const resetFaceAntiSpoofFrameOutput = (output) => {
|
|
69
69
|
getSpoofDetectorFactory().resetSpoofDetectorOutput(output);
|
|
70
70
|
};
|
|
@@ -111,11 +111,12 @@ RCT_REMAP_METHOD(processFrame,
|
|
|
111
111
|
NSDictionary *(*processFrame)(id, SEL, NSArray *, NSNumber *, NSNumber *, NSArray *, NSArray *, NSError **) =
|
|
112
112
|
(NSDictionary *(*)(id, SEL, NSArray *, NSNumber *, NSNumber *, NSArray *, NSArray *, NSError **))objc_msgSend;
|
|
113
113
|
NSDictionary *result = processFrame(managerClass, selector, frameData, width, height, rowStrides, pixelStrides, &error);
|
|
114
|
-
|
|
114
|
+
NSNumber *livenessScore = [result[@"confidence"] isKindOfClass:[NSNumber class]] ? result[@"confidence"] : nil;
|
|
115
|
+
if (error != nil || livenessScore == nil) {
|
|
115
116
|
reject(@"PROCESS_ERROR", @"Failed to process camera frame", error);
|
|
116
117
|
return;
|
|
117
118
|
}
|
|
118
|
-
resolve(
|
|
119
|
+
resolve(livenessScore);
|
|
119
120
|
}
|
|
120
121
|
|
|
121
122
|
RCT_REMAP_METHOD(isAvailable,
|