react-native-vision-camera-spoof-detector 1.1.18 → 1.1.20

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.
@@ -45,6 +45,8 @@ class FaceAntiSpoofingAdvanced(private val assetManager: AssetManager) {
45
45
  private val outputs = mutableMapOf<Int, Any>()
46
46
  private var outputIndex0: Int = 0
47
47
  private var outputIndex1: Int = 1
48
+ private val modelInputBuffer = Array(INPUT_IMAGE_SIZE) { Array(INPUT_IMAGE_SIZE) { FloatArray(3) } }
49
+ private val grayBuffer = Array(INPUT_IMAGE_SIZE) { IntArray(INPUT_IMAGE_SIZE) }
48
50
 
49
51
  init {
50
52
  initializeInterpreter()
@@ -53,31 +55,60 @@ class FaceAntiSpoofingAdvanced(private val assetManager: AssetManager) {
53
55
  private fun initializeInterpreter() {
54
56
  try {
55
57
  val modelBuffer = loadModelFile(assetManager, MODEL_FILE)
56
- val options = Interpreter.Options()
57
-
58
- // Use optimal settings
59
- val optimalThreads = 4 // Fixed as in working version
60
- options.setNumThreads(optimalThreads)
61
-
62
- currentAccelerator = ACCELERATOR_CPU
63
- Log.i(TAG, "CPU-ONLY MODE - Using $optimalThreads threads")
64
-
65
- interpreter = Interpreter(modelBuffer, options)
66
-
58
+ val optimalThreads = 4
59
+ val compatibilityList = CompatibilityList()
60
+ var selectedAccelerator = ACCELERATOR_CPU
61
+
62
+ try {
63
+ if (compatibilityList.isDelegateSupportedOnThisDevice) {
64
+ val delegate = GpuDelegate(compatibilityList.bestOptionsForThisDevice)
65
+ gpuDelegate = delegate
66
+ val options = Interpreter.Options().apply {
67
+ setNumThreads(optimalThreads)
68
+ addDelegate(delegate)
69
+ }
70
+ interpreter = Interpreter(modelBuffer, options)
71
+ selectedAccelerator = ACCELERATOR_GPU
72
+ Log.i(TAG, "Face Anti-Spoof initialized | GPU delegate enabled | Threads: $optimalThreads")
73
+ } else {
74
+ throw IllegalStateException("GPU delegate not supported")
75
+ }
76
+ } catch (e: Exception) {
77
+ Log.w(TAG, "GPU delegate unavailable or failed: ${e.message}", e)
78
+ try {
79
+ val nnapiOptions = org.tensorflow.lite.nnapi.NnApiDelegate()
80
+ val options = Interpreter.Options().apply {
81
+ setNumThreads(optimalThreads)
82
+ addDelegate(nnapiOptions)
83
+ }
84
+ interpreter = Interpreter(modelBuffer, options)
85
+ selectedAccelerator = ACCELERATOR_NNAPI
86
+ Log.i(TAG, "Face Anti-Spoof initialized | NNAPI delegate enabled | Threads: $optimalThreads")
87
+ } catch (nnapiError: Exception) {
88
+ Log.w(TAG, "NNAPI delegate unavailable or failed: ${nnapiError.message}", nnapiError)
89
+ val options = Interpreter.Options().apply {
90
+ setNumThreads(optimalThreads)
91
+ }
92
+ interpreter = Interpreter(modelBuffer, options)
93
+ selectedAccelerator = ACCELERATOR_CPU
94
+ Log.i(TAG, "Face Anti-Spoof initialized | CPU fallback enabled | Threads: $optimalThreads")
95
+ }
96
+ }
97
+
98
+ currentAccelerator = selectedAccelerator
99
+
67
100
  try {
68
101
  outputIndex0 = interpreter!!.getOutputIndex("Identity")
69
102
  outputIndex1 = interpreter!!.getOutputIndex("Identity_1")
70
103
  } catch (e: Exception) {
71
104
  Log.w(TAG, "Could not resolve named output indices, using defaults: ${e.message}")
72
- // Fallback to default indices
73
105
  outputIndex0 = 0
74
106
  outputIndex1 = 1
75
107
  }
76
-
108
+
77
109
  outputs[outputIndex0] = clssPred
78
110
  outputs[outputIndex1] = leafNodeMask
79
111
  isInitialized = true
80
- Log.i(TAG, "Face Anti-Spoof initialized | CPU-ONLY MODE | Threads: $optimalThreads")
81
112
  } catch (e: Exception) {
82
113
  Log.e(TAG, "FAILED to load TFLite model: ${e.message}", e)
83
114
  isInitialized = false
@@ -371,8 +402,6 @@ class FaceAntiSpoofingAdvanced(private val assetManager: AssetManager) {
371
402
  ): SampledFrame {
372
403
  val targetWidth = INPUT_IMAGE_SIZE
373
404
  val targetHeight = INPUT_IMAGE_SIZE
374
- val rgbBuffer = Array(targetHeight) { Array(targetWidth) { FloatArray(3) } }
375
- val grayBuffer = Array(targetHeight) { IntArray(targetWidth) }
376
405
  val scaleX = srcWidth.toFloat() / targetWidth
377
406
  val scaleY = srcHeight.toFloat() / targetHeight
378
407
 
@@ -385,12 +414,19 @@ class FaceAntiSpoofingAdvanced(private val assetManager: AssetManager) {
385
414
  val r = (rgba[srcIndex].toInt() and 0xFF) / 255f
386
415
  val g = (rgba[srcIndex + 1].toInt() and 0xFF) / 255f
387
416
  val b = (rgba[srcIndex + 2].toInt() and 0xFF) / 255f
388
- rgbBuffer[targetY][targetX] = floatArrayOf(r, g, b)
389
- grayBuffer[targetY][targetX] = (0.299f * rgba[srcIndex].toInt() + 0.587f * rgba[srcIndex + 1].toInt() + 0.114f * rgba[srcIndex + 2].toInt()).toInt()
417
+
418
+ modelInputBuffer[targetY][targetX][0] = r
419
+ modelInputBuffer[targetY][targetX][1] = g
420
+ modelInputBuffer[targetY][targetX][2] = b
421
+ grayBuffer[targetY][targetX] = (
422
+ 0.299f * rgba[srcIndex].toInt() +
423
+ 0.587f * rgba[srcIndex + 1].toInt() +
424
+ 0.114f * rgba[srcIndex + 2].toInt()
425
+ ).toInt()
390
426
  }
391
427
  }
392
428
 
393
- return SampledFrame(rgbBuffer, grayBuffer)
429
+ return SampledFrame(modelInputBuffer, grayBuffer)
394
430
  }
395
431
 
396
432
  // FROM WORKING VERSION: laplacian with Bitmap
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-vision-camera-spoof-detector",
3
- "version": "1.1.18",
3
+ "version": "1.1.20",
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": {