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

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.
@@ -13,6 +13,9 @@ import com.margelo.nitro.camera.extensions.setAllowDroppingLateFrames
13
13
  import com.margelo.nitro.camera.extensions.surfaceRotation
14
14
  import com.margelo.nitro.camera.public.NativeCameraOutput
15
15
  import com.faceantispoof.FaceAntiSpoofingAdvanced
16
+ import kotlin.math.abs
17
+ import kotlin.math.max
18
+ import kotlin.math.min
16
19
  import java.util.concurrent.Executors
17
20
  import java.util.concurrent.atomic.AtomicBoolean
18
21
 
@@ -32,10 +35,19 @@ class HybridSpoofDetectorOutput(
32
35
  private val executor = Executors.newSingleThreadExecutor()
33
36
  private val busy = AtomicBoolean(false)
34
37
  private val disposed = AtomicBoolean(false)
38
+ private val frameCounter = java.util.concurrent.atomic.AtomicInteger(0)
39
+ private var lastFrameBrightness = 0f
35
40
  private val detector = FaceAntiSpoofingAdvanced(
36
41
  NitroModules.applicationContext?.assets ?: error("FaceAntiSpoof: no application context")
37
42
  )
38
43
  private var imageAnalysis: ImageAnalysis? = null
44
+ private companion object {
45
+ private const val FRAME_EVALUATION_INTERVAL = 3
46
+ private const val MIN_FRAME_BRIGHTNESS = 24f
47
+ private const val MAX_FRAME_BRIGHTNESS = 220f
48
+ private const val MIN_VARIANCE_FOR_ANALYSIS = 1500f
49
+ private const val MIN_BRIGHTNESS_DELTA = 8f
50
+ }
39
51
 
40
52
  override fun createUseCase(
41
53
  mirrorMode: MirrorMode,
@@ -72,6 +84,10 @@ class HybridSpoofDetectorOutput(
72
84
  }
73
85
  try {
74
86
  val rgba = extractRgbaBytes(image)
87
+ if (!shouldProcessFrame(rgba, image.width, image.height)) {
88
+ return
89
+ }
90
+
75
91
  val result = detector.advancedAntiSpoofingRGBA(rgba, image.width, image.height)
76
92
  val isLive = result.neuralNetworkScore < FaceAntiSpoofingAdvanced.BASE_THRESHOLD &&
77
93
  result.laplacianScore > FaceAntiSpoofingAdvanced.LAPLACIAN_THRESHOLD
@@ -93,6 +109,50 @@ class HybridSpoofDetectorOutput(
93
109
  }
94
110
  }
95
111
 
112
+ private fun shouldProcessFrame(rgba: ByteArray, width: Int, height: Int): Boolean {
113
+ val frameIndex = frameCounter.incrementAndGet()
114
+ if (frameIndex % FRAME_EVALUATION_INTERVAL != 0) {
115
+ return false
116
+ }
117
+
118
+ val sampleStep = max(1, min(width, height) / 32)
119
+ var brightnessSum = 0L
120
+ var brightnessSquaredSum = 0L
121
+ var sampleCount = 0
122
+
123
+ for (y in 0 until height step sampleStep) {
124
+ val rowOffset = y * width * 4
125
+ for (x in 0 until width step sampleStep) {
126
+ val index = rowOffset + x * 4
127
+ if (index + 2 >= rgba.size) continue
128
+
129
+ val r = rgba[index].toInt() and 0xFF
130
+ val g = rgba[index + 1].toInt() and 0xFF
131
+ val b = rgba[index + 2].toInt() and 0xFF
132
+ val luma = (0.299 * r + 0.587 * g + 0.114 * b).toInt()
133
+
134
+ brightnessSum += luma.toLong()
135
+ brightnessSquaredSum += (luma.toLong() * luma.toLong())
136
+ sampleCount += 1
137
+ }
138
+ }
139
+
140
+ if (sampleCount == 0) {
141
+ return false
142
+ }
143
+
144
+ val avgBrightness = brightnessSum.toFloat() / sampleCount
145
+ val variance = (brightnessSquaredSum.toFloat() / sampleCount) - (avgBrightness * avgBrightness)
146
+ val brightnessDelta = kotlin.math.abs(avgBrightness - lastFrameBrightness)
147
+ lastFrameBrightness = avgBrightness
148
+
149
+ val brightnessOk = avgBrightness in MIN_FRAME_BRIGHTNESS..MAX_FRAME_BRIGHTNESS
150
+ val textureOk = variance >= MIN_VARIANCE_FOR_ANALYSIS
151
+ val motionLikely = brightnessDelta >= MIN_BRIGHTNESS_DELTA || lastFrameBrightness == 0f
152
+
153
+ return brightnessOk && textureOk && motionLikely
154
+ }
155
+
96
156
  private fun extractRgbaBytes(image: ImageProxy): ByteArray {
97
157
  val plane = image.planes.firstOrNull() ?: throw IllegalStateException("RGBA image has no planes")
98
158
  val rowStride = plane.rowStride
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-vision-camera-spoof-detector",
3
- "version": "1.1.20",
3
+ "version": "1.1.21",
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": {