react-native-vision-camera-spoof-detector 1.3.1 → 1.3.2

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.
@@ -60,57 +60,36 @@ class FaceAntiSpoofingAdvanced(private val assetManager: AssetManager) {
60
60
  val modelBuffer = loadModelFile(assetManager, MODEL_FILE)
61
61
  val optimalThreads = 4
62
62
  val compatibilityList = CompatibilityList()
63
- var selectedAccelerator = ACCELERATOR_CPU
64
63
 
65
- try {
66
- if (compatibilityList.isDelegateSupportedOnThisDevice) {
64
+ if (compatibilityList.isDelegateSupportedOnThisDevice) {
65
+ try {
67
66
  val delegate = GpuDelegate(compatibilityList.bestOptionsForThisDevice)
68
- gpuDelegate = delegate
69
67
  val options = Interpreter.Options().apply {
70
68
  setNumThreads(optimalThreads)
71
69
  addDelegate(delegate)
72
70
  }
73
71
  interpreter = Interpreter(modelBuffer, options)
74
- selectedAccelerator = ACCELERATOR_GPU
72
+ gpuDelegate = delegate
73
+ currentAccelerator = ACCELERATOR_GPU
75
74
  Log.i(TAG, "Face Anti-Spoof initialized | GPU delegate enabled | Threads: $optimalThreads")
76
- } else {
77
- throw IllegalStateException("GPU delegate not supported")
78
- }
79
- } catch (e: Exception) {
80
- Log.w(TAG, "GPU delegate unavailable or failed: ${e.message}", e)
81
- try {
82
- val nnapiOptions = org.tensorflow.lite.nnapi.NnApiDelegate()
83
- val options = Interpreter.Options().apply {
84
- setNumThreads(optimalThreads)
85
- addDelegate(nnapiOptions)
86
- }
87
- interpreter = Interpreter(modelBuffer, options)
88
- selectedAccelerator = ACCELERATOR_NNAPI
89
- Log.i(TAG, "Face Anti-Spoof initialized | NNAPI delegate enabled | Threads: $optimalThreads")
90
- } catch (nnapiError: Exception) {
91
- Log.w(TAG, "NNAPI delegate unavailable or failed: ${nnapiError.message}", nnapiError)
92
- val options = Interpreter.Options().apply {
93
- setNumThreads(optimalThreads)
94
- }
95
- interpreter = Interpreter(modelBuffer, options)
96
- selectedAccelerator = ACCELERATOR_CPU
97
- Log.i(TAG, "Face Anti-Spoof initialized | CPU fallback enabled | Threads: $optimalThreads")
75
+ } catch (gpuError: Exception) {
76
+ Log.w(TAG, "GPU delegate initialization failed; using CPU: ${gpuError.message}", gpuError)
77
+ interpreter = null
78
+ gpuDelegate?.close()
79
+ gpuDelegate = null
98
80
  }
99
81
  }
100
82
 
101
- currentAccelerator = selectedAccelerator
102
-
103
- try {
104
- outputIndex0 = interpreter!!.getOutputIndex("Identity")
105
- outputIndex1 = interpreter!!.getOutputIndex("Identity_1")
106
- } catch (e: Exception) {
107
- Log.w(TAG, "Could not resolve named output indices, using defaults: ${e.message}")
108
- outputIndex0 = 0
109
- outputIndex1 = 1
83
+ if (interpreter == null) {
84
+ val options = Interpreter.Options().apply {
85
+ setNumThreads(optimalThreads)
86
+ }
87
+ interpreter = Interpreter(modelBuffer, options)
88
+ currentAccelerator = ACCELERATOR_CPU
89
+ Log.i(TAG, "Face Anti-Spoof initialized | CPU inference enabled | Threads: $optimalThreads")
110
90
  }
111
91
 
112
- outputs[outputIndex0] = clssPred
113
- outputs[outputIndex1] = leafNodeMask
92
+ resolveOutputIndices()
114
93
  isInitialized = true
115
94
  } catch (e: Exception) {
116
95
  Log.e(TAG, "FAILED to load TFLite model: ${e.message}", e)
@@ -119,6 +98,57 @@ class FaceAntiSpoofingAdvanced(private val assetManager: AssetManager) {
119
98
  }
120
99
  }
121
100
 
101
+ private fun resolveOutputIndices() {
102
+ try {
103
+ outputIndex0 = interpreter!!.getOutputIndex("Identity")
104
+ outputIndex1 = interpreter!!.getOutputIndex("Identity_1")
105
+ } catch (e: Exception) {
106
+ Log.w(TAG, "Could not resolve named output indices, using defaults: ${e.message}")
107
+ outputIndex0 = 0
108
+ outputIndex1 = 1
109
+ }
110
+ outputs[outputIndex0] = clssPred
111
+ outputs[outputIndex1] = leafNodeMask
112
+ }
113
+
114
+ @Synchronized
115
+ private fun runInference(input: Any) {
116
+ try {
117
+ interpreter!!.runForMultipleInputsOutputs(arrayOf(input), outputs)
118
+ } catch (gpuError: Exception) {
119
+ if (currentAccelerator != ACCELERATOR_GPU) throw gpuError
120
+
121
+ Log.w(TAG, "GPU inference failed; retrying this frame on CPU: ${gpuError.message}", gpuError)
122
+ switchToCpu()
123
+ interpreter!!.runForMultipleInputsOutputs(arrayOf(input), outputs)
124
+ }
125
+ }
126
+
127
+ private fun switchToCpu() {
128
+ try {
129
+ interpreter?.close()
130
+ } catch (closeError: Exception) {
131
+ Log.w(TAG, "Error closing GPU interpreter before CPU fallback: ${closeError.message}", closeError)
132
+ } finally {
133
+ interpreter = null
134
+ }
135
+ try {
136
+ gpuDelegate?.close()
137
+ } catch (closeError: Exception) {
138
+ Log.w(TAG, "Error closing GPU delegate before CPU fallback: ${closeError.message}", closeError)
139
+ } finally {
140
+ gpuDelegate = null
141
+ }
142
+
143
+ val options = Interpreter.Options().apply {
144
+ setNumThreads(4)
145
+ }
146
+ interpreter = Interpreter(loadModelFile(assetManager, MODEL_FILE), options)
147
+ currentAccelerator = ACCELERATOR_CPU
148
+ resolveOutputIndices()
149
+ Log.i(TAG, "Face Anti-Spoof switched to CPU inference")
150
+ }
151
+
122
152
  fun getAccelerator(): String = currentAccelerator
123
153
 
124
154
  // NEW: Add this method to accept Bitmap as input (matching working version)
@@ -385,21 +415,21 @@ class FaceAntiSpoofingAdvanced(private val assetManager: AssetManager) {
385
415
  outputs[outputIndex0] = clssPred
386
416
  outputs[outputIndex1] = leafNodeMask
387
417
 
388
- interpreter!!.runForMultipleInputsOutputs(arrayOf(input), outputs)
418
+ runInference(input)
389
419
 
390
420
  Log.i(TAG, "clss_pred: [${clssPred[0].joinToString()}]")
391
421
  Log.i(TAG, "leaf_node_mask: [${leafNodeMask[0].joinToString()}]")
392
422
 
393
423
  val score = leafScore1(clssPred, leafNodeMask)
394
424
 
395
- if (bitmapScale != bitmap) {
396
- bitmapScale.recycle()
397
- }
398
-
399
425
  Result.success(score)
400
426
  } catch (e: Exception) {
401
427
  Log.e(TAG, "Error in neuralNetworkAntiSpoofing", e)
402
428
  Result.failure(e)
429
+ } finally {
430
+ if (bitmapScale != bitmap) {
431
+ bitmapScale.recycle()
432
+ }
403
433
  }
404
434
  }
405
435
 
@@ -410,7 +440,7 @@ class FaceAntiSpoofingAdvanced(private val assetManager: AssetManager) {
410
440
  leafNodeMask[0].fill(0f)
411
441
  outputs[outputIndex0] = clssPred
412
442
  outputs[outputIndex1] = leafNodeMask
413
- interpreter!!.runForMultipleInputsOutputs(arrayOf(input), outputs)
443
+ runInference(input)
414
444
  Log.i(TAG, "clss_pred: [${clssPred[0].joinToString()}]")
415
445
  Log.i(TAG, "leaf_node_mask: [${leafNodeMask[0].joinToString()}]")
416
446
  Result.success(leafScore1(clssPred, leafNodeMask))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-vision-camera-spoof-detector",
3
- "version": "1.3.1",
3
+ "version": "1.3.2",
4
4
  "description": "High-performance face anti-spoofing and liveness detection module for React Native Vision Camera. Uses TensorFlow Lite with GPU acceleration and optimized YUV processing.",
5
5
  "homepage": "https://github.com/dpraful/react-native-vision-camera-spoof-detector",
6
6
  "repository": {