react-native-vision-camera-spoof-detector 1.4.0 → 1.5.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.
Files changed (29) hide show
  1. package/README.md +16 -29
  2. package/android/build.gradle +0 -13
  3. package/android/src/main/java/com/faceantispoof/FaceAntiSpoofPackage.kt +1 -3
  4. package/android/src/main/java/com/faceantispoof/FaceAntiSpoofingAdvanced.kt +78 -961
  5. package/android/src/main/java/com/lengdev/livenessdetector/LivenessDetector.kt +228 -0
  6. package/android/src/main/java/com/margelo/nitro/camera/spoofdetector/HybridSpoofDetectorOutput.kt +17 -31
  7. package/android/src/main/jniLibs/arm64-v8a/libliveness_detector.so +0 -0
  8. package/android/src/main/jniLibs/armeabi-v7a/libliveness_detector.so +0 -0
  9. package/index.d.ts +2 -9
  10. package/index.js +6 -6
  11. package/ios/FaceAntiSpoofModule.m +3 -2
  12. package/ios/HybridSpoofDetectorOutput.swift +3 -3
  13. package/lib/assets/live/config.json +1 -0
  14. package/lib/assets/live/model_1.bin +0 -0
  15. package/lib/assets/live/model_1.param +214 -0
  16. package/lib/assets/live/model_2.bin +0 -0
  17. package/lib/assets/live/model_2.param +238 -0
  18. package/nitrogen/generated/android/c++/JSpoofDetectorOutputOptions.hpp +1 -9
  19. package/nitrogen/generated/android/kotlin/com/margelo/nitro/camera/spoofdetector/SpoofDetectorOutputOptions.kt +4 -14
  20. package/nitrogen/generated/ios/swift/SpoofDetectorOutputOptions.swift +1 -37
  21. package/nitrogen/generated/shared/c++/SpoofDetectorOutputOptions.hpp +1 -9
  22. package/package.json +1 -2
  23. package/src/specs/SpoofDetectorFactory.nitro.ts +0 -2
  24. package/android/fix-prefab.gradle +0 -4
  25. package/android/src/main/java/com/faceantispoof/AcceleratorConfig.kt +0 -97
  26. package/android/src/main/java/com/faceantispoof/FaceAntiSpoofModule.kt +0 -242
  27. package/android/src/main/res/values/strings.xml +0 -3
  28. package/lib/assets/FaceAntiSpoofing.tflite +0 -0
  29. package/lib/assets/model_spec.json +0 -16
@@ -1,242 +0,0 @@
1
- package com.faceantispoof
2
-
3
- import com.facebook.react.bridge.ReactApplicationContext
4
- import com.facebook.react.bridge.ReactContextBaseJavaModule
5
- import com.facebook.react.bridge.ReactMethod
6
- import com.facebook.react.bridge.Promise
7
- import com.facebook.react.bridge.Arguments
8
- import com.facebook.react.bridge.ReadableArray
9
- import com.facebook.react.module.annotations.ReactModule
10
-
11
- @ReactModule(name = FaceAntiSpoofModule.NAME)
12
- class FaceAntiSpoofModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
13
-
14
- companion object {
15
- const val NAME = "FaceAntiSpoof"
16
- private var activeModule: FaceAntiSpoofModule? = null
17
-
18
- @JvmStatic
19
- fun processFrameFromJSI(
20
- planes: Array<ByteArray>,
21
- width: Int,
22
- height: Int,
23
- rowStrides: IntArray,
24
- pixelStrides: IntArray
25
- ): DoubleArray {
26
- val module = activeModule
27
- ?: return doubleArrayOf(0.0, 0.0, 0.0, 0.0)
28
- return module.processNativeFrame(planes, width, height, rowStrides, pixelStrides)
29
- }
30
- }
31
-
32
- private var faceAntiSpoofing: FaceAntiSpoofingAdvanced? = null
33
- private var isModuleInitialized = false
34
-
35
- init {
36
- activeModule = this
37
-
38
- try {
39
- // Initialize face anti-spoofing with app assets
40
- faceAntiSpoofing = FaceAntiSpoofingAdvanced(reactContext.assets)
41
- isModuleInitialized = true
42
- android.util.Log.i("FaceAntiSpoof", "Face anti-spoofing initialized successfully")
43
- } catch (e: Exception) {
44
- android.util.Log.e("FaceAntiSpoof", "Failed to initialize face anti-spoofing: ${e.message}", e)
45
- isModuleInitialized = false
46
- }
47
-
48
- }
49
-
50
- override fun getName(): String = NAME
51
-
52
- @ReactMethod
53
- fun initialize(promise: Promise) {
54
- try {
55
- if (!isModuleInitialized) {
56
- // Try to initialize now if not done in init()
57
- try {
58
- faceAntiSpoofing = FaceAntiSpoofingAdvanced(reactApplicationContext.assets)
59
- isModuleInitialized = true
60
- android.util.Log.i("FaceAntiSpoof", "Face anti-spoofing initialized in initialize()")
61
- } catch (e: Exception) {
62
- android.util.Log.e("FaceAntiSpoof", "Failed to initialize face anti-spoofing: ${e.message}", e)
63
- promise.reject("INIT_ERROR", "Failed to initialize face anti-spoof: ${e.message}", e)
64
- return
65
- }
66
- }
67
- promise.resolve(isModuleInitialized)
68
- } catch (e: Exception) {
69
- android.util.Log.e("FaceAntiSpoof", "initialize() error", e)
70
- promise.reject("INIT_ERROR", "Failed to initialize face anti-spoof", e)
71
- }
72
- }
73
-
74
- @ReactMethod
75
- fun checkModelStatus(promise: Promise) {
76
- try {
77
- val isInitialized = isModuleInitialized && faceAntiSpoofing?.isInitialized() == true
78
- val result = mapOf(
79
- "pluginAvailable" to isInitialized,
80
- "modelLoaded" to isInitialized,
81
- "moduleInitialized" to isModuleInitialized
82
- )
83
- promise.resolve(com.facebook.react.bridge.Arguments.makeNativeMap(result))
84
- } catch (e: Exception) {
85
- android.util.Log.e("FaceAntiSpoof", "checkModelStatus() error", e)
86
- promise.reject("STATUS_ERROR", "Failed to check model status", e)
87
- }
88
- }
89
-
90
- @ReactMethod
91
- fun processFrame(
92
- frameData: ReadableArray,
93
- width: Int,
94
- height: Int,
95
- rowStrides: ReadableArray,
96
- pixelStrides: ReadableArray,
97
- promise: Promise
98
- ) {
99
- try {
100
- if (frameData.size() < 3 || rowStrides.size() < 3 || pixelStrides.size() < 3) {
101
- promise.reject("INVALID_FRAME", "Expected Y, U and V camera planes")
102
- return
103
- }
104
-
105
- if (!isModuleInitialized || faceAntiSpoofing == null) {
106
- promise.reject("NOT_INITIALIZED", "Face anti-spoofing is not initialized")
107
- return
108
- }
109
-
110
- val planes = Array(3) { planeIndex ->
111
- val plane = frameData.getArray(planeIndex)
112
- ?: throw IllegalArgumentException("Camera plane $planeIndex is null")
113
- ByteArray(plane.size()) { index -> plane.getInt(index).toByte() }
114
- }
115
- val yuvRowStrides = IntArray(3) { rowStrides.getDouble(it).toInt() }
116
- val yuvPixelStrides = IntArray(3) { pixelStrides.getDouble(it).toInt().coerceAtLeast(1) }
117
- val result = faceAntiSpoofing!!.advancedAntiSpoofing(
118
- planes,
119
- width,
120
- height,
121
- yuvRowStrides,
122
- yuvPixelStrides
123
- )
124
- promise.resolve(
125
- Arguments.makeNativeMap(
126
- mapOf(
127
- "neuralNetworkScore" to result.neuralNetworkScore.toDouble(),
128
- "laplacianScore" to result.laplacianScore,
129
- "combinedScore" to result.combinedScore.toDouble(),
130
- "confidence" to result.confidence.toDouble()
131
- )
132
- )
133
- )
134
- } catch (e: Exception) {
135
- promise.reject("PROCESS_ERROR", "Failed to process camera frame", e)
136
- }
137
- }
138
-
139
- @Synchronized
140
- private fun processNativeFrame(
141
- planes: Array<ByteArray>,
142
- width: Int,
143
- height: Int,
144
- rowStrides: IntArray,
145
- pixelStrides: IntArray
146
- ): DoubleArray {
147
- if (!isModuleInitialized || faceAntiSpoofing == null || planes.size < 3) {
148
- return doubleArrayOf(0.0, 0.0, 0.0, 0.0)
149
- }
150
-
151
- return try {
152
- val result = faceAntiSpoofing!!.advancedAntiSpoofing(
153
- planes,
154
- width,
155
- height,
156
- rowStrides,
157
- pixelStrides
158
- )
159
- doubleArrayOf(
160
- result.neuralNetworkScore.toDouble(),
161
- result.laplacianScore.toDouble(),
162
- result.combinedScore.toDouble(),
163
- result.confidence.toDouble()
164
- )
165
- } catch (error: Exception) {
166
- android.util.Log.e("FaceAntiSpoof", "Native frame processing failed", error)
167
- doubleArrayOf(0.0, 0.0, 0.0, 0.0)
168
- }
169
- }
170
-
171
- @ReactMethod
172
- fun isAvailable(promise: Promise) {
173
- try {
174
- val available = isModuleInitialized && faceAntiSpoofing?.isInitialized() == true
175
- promise.resolve(available)
176
- } catch (e: Exception) {
177
- android.util.Log.e("FaceAntiSpoof", "isAvailable() error", e)
178
- promise.reject("AVAIL_ERROR", "Failed to check availability", e)
179
- }
180
- }
181
-
182
- @ReactMethod
183
- fun testMethod(promise: Promise) {
184
- try {
185
- promise.resolve("Native module is working!")
186
- } catch (e: Exception) {
187
- android.util.Log.e("FaceAntiSpoof", "testMethod() error", e)
188
- promise.reject("TEST_ERROR", "Failed executing testMethod", e)
189
- }
190
- }
191
-
192
- @ReactMethod
193
- fun getModuleInfo(promise: Promise) {
194
- try {
195
- val info = mapOf(
196
- "name" to NAME,
197
- "methods" to listOf("initialize", "checkModelStatus", "isAvailable", "testMethod", "getModuleInfo")
198
- )
199
- promise.resolve(com.facebook.react.bridge.Arguments.makeNativeMap(info))
200
- } catch (e: Exception) {
201
- android.util.Log.e("FaceAntiSpoof", "getModuleInfo() error", e)
202
- promise.reject("INFO_ERROR", "Failed to get module info", e)
203
- }
204
- }
205
-
206
- @ReactMethod
207
- fun install(promise: Promise) {
208
- // VisionCamera 5 uses the Nitro CameraOutput. The old reflective JSI
209
- // installer targets a native library that is no longer part of this module.
210
- promise.resolve(false)
211
- }
212
-
213
- @ReactMethod
214
- fun cleanup(promise: Promise) {
215
- try {
216
- faceAntiSpoofing?.close()
217
- faceAntiSpoofing = null
218
- isModuleInitialized = false
219
- android.util.Log.i("FaceAntiSpoof", "Cleanup completed")
220
- promise.resolve(true)
221
- } catch (e: Exception) {
222
- android.util.Log.e("FaceAntiSpoof", "cleanup() error", e)
223
- promise.reject("CLEANUP_ERROR", "Failed to cleanup face anti-spoof", e)
224
- }
225
- }
226
-
227
- @Suppress("DEPRECATION")
228
- @Deprecated("React Native deprecated this lifecycle callback; retained for compatibility.")
229
- override fun onCatalystInstanceDestroy() {
230
- // Cleanup when React Native is shutting down
231
- try {
232
- faceAntiSpoofing?.close()
233
- faceAntiSpoofing = null
234
- isModuleInitialized = false
235
- android.util.Log.i("FaceAntiSpoof", "onCatalystInstanceDestroy cleanup completed")
236
- } catch (e: Exception) {
237
- android.util.Log.e("FaceAntiSpoof", "Error in onCatalystInstanceDestroy", e)
238
- }
239
- super.onCatalystInstanceDestroy()
240
- }
241
-
242
- }
@@ -1,3 +0,0 @@
1
- <resources>
2
- <string name="app_name">Vision Camera Face Anti-Spoof</string>
3
- </resources>
Binary file
@@ -1,16 +0,0 @@
1
- {
2
- "model_file": "FaceAntiSpoofing.tflite",
3
- "input": {
4
- "shape": [1, 256, 256, 3],
5
- "dtype": "float32",
6
- "normalization": {"scale": 255.0}
7
- },
8
- "outputs": {
9
- "Identity": {"shape": [1,8], "dtype": "float32", "description": "class predictions (clss_pred)"},
10
- "Identity_1": {"shape": [1,8], "dtype": "float32", "description": "leaf node mask (leaf_node_mask)"}
11
- },
12
- "post_processing": {
13
- "leafScore": "sum(abs(clss_pred[i]) * leaf_node_mask[i]) for i in 0..7",
14
- "combinedScore": "calculated from neural and laplacian scores as in Android implementation"
15
- }
16
- }