react-native-vision-camera-spoof-detector 1.3.6 → 1.3.8
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
CHANGED
|
@@ -14,7 +14,7 @@ Face anti-spoofing and liveness scoring for React Native Vision Camera 5. A Nitr
|
|
|
14
14
|
- Face size, frame-edge, pose, landmark, brightness, and liveness-quality gates
|
|
15
15
|
- Optional callbacks for scores, detected faces, stable-face capture, and errors
|
|
16
16
|
- Output reset without recreating the camera output
|
|
17
|
-
-
|
|
17
|
+
- Configurable liveness thresholds; uses Vision Camera's selected analysis resolution
|
|
18
18
|
|
|
19
19
|
## Requirements
|
|
20
20
|
|
|
@@ -49,7 +49,9 @@ class FaceAntiSpoofingAdvanced(private val assetManager: AssetManager) {
|
|
|
49
49
|
private var outputIndex0: Int = 0
|
|
50
50
|
private var outputIndex1: Int = 1
|
|
51
51
|
private val modelInputBuffer = Array(INPUT_IMAGE_SIZE) { Array(INPUT_IMAGE_SIZE) { FloatArray(3) } }
|
|
52
|
+
private val modelInput = arrayOf(modelInputBuffer)
|
|
52
53
|
private val grayBuffer = Array(INPUT_IMAGE_SIZE) { IntArray(INPUT_IMAGE_SIZE) }
|
|
54
|
+
private val inputPlanePositions = IntArray(3)
|
|
53
55
|
|
|
54
56
|
init {
|
|
55
57
|
initializeInterpreter()
|
|
@@ -274,6 +276,199 @@ class FaceAntiSpoofingAdvanced(private val assetManager: AssetManager) {
|
|
|
274
276
|
}
|
|
275
277
|
}
|
|
276
278
|
|
|
279
|
+
@Synchronized
|
|
280
|
+
fun advancedAntiSpoofing(
|
|
281
|
+
planes: Array<ByteBuffer>,
|
|
282
|
+
width: Int,
|
|
283
|
+
height: Int,
|
|
284
|
+
rowStrides: IntArray,
|
|
285
|
+
pixelStrides: IntArray,
|
|
286
|
+
rotationDegrees: Int,
|
|
287
|
+
faceBounds: RectF,
|
|
288
|
+
leftEye: PointF,
|
|
289
|
+
rightEye: PointF,
|
|
290
|
+
): AntiSpoofingResult {
|
|
291
|
+
if (!isInitialized) {
|
|
292
|
+
throw IllegalStateException("Classifier not initialized")
|
|
293
|
+
}
|
|
294
|
+
require(planes.size >= 3) { "Expected Y, U and V planes" }
|
|
295
|
+
require(rowStrides.size >= 3 && pixelStrides.size >= 3) {
|
|
296
|
+
"Expected row and pixel strides for Y, U and V planes"
|
|
297
|
+
}
|
|
298
|
+
require(width > 0 && height > 0) { "Frame dimensions must be positive" }
|
|
299
|
+
|
|
300
|
+
val rotation = ((rotationDegrees % 360) + 360) % 360
|
|
301
|
+
require(rotation % 90 == 0) { "Invalid frame rotation: $rotation" }
|
|
302
|
+
|
|
303
|
+
val orientedWidth = if (rotation == 90 || rotation == 270) height else width
|
|
304
|
+
val orientedHeight = if (rotation == 90 || rotation == 270) width else height
|
|
305
|
+
val cropSide = (max(faceBounds.width(), faceBounds.height()) * 1.4f)
|
|
306
|
+
.coerceAtMost(min(orientedWidth, orientedHeight).toFloat())
|
|
307
|
+
.toInt()
|
|
308
|
+
.coerceAtLeast(1)
|
|
309
|
+
val halfSide = cropSide / 2f
|
|
310
|
+
val centerX = faceBounds.centerX().coerceIn(halfSide, orientedWidth - halfSide)
|
|
311
|
+
val centerY = faceBounds.centerY().coerceIn(halfSide, orientedHeight - halfSide)
|
|
312
|
+
val cropLeft = (centerX - halfSide).toInt().coerceIn(0, orientedWidth - cropSide)
|
|
313
|
+
val cropTop = (centerY - halfSide).toInt().coerceIn(0, orientedHeight - cropSide)
|
|
314
|
+
val cropRight = cropLeft + cropSide - 1
|
|
315
|
+
val cropBottom = cropTop + cropSide - 1
|
|
316
|
+
val eyeAngle = atan2(
|
|
317
|
+
(rightEye.y - leftEye.y).toDouble(),
|
|
318
|
+
(rightEye.x - leftEye.x).toDouble(),
|
|
319
|
+
).toFloat().takeIf { abs(Math.toDegrees(it.toDouble())) >= 0.5 } ?: 0f
|
|
320
|
+
val cosine = cos(eyeAngle)
|
|
321
|
+
val sine = sin(eyeAngle)
|
|
322
|
+
val cropCenter = (cropSide - 1) / 2f
|
|
323
|
+
val sampleScale = cropSide.toFloat() / INPUT_IMAGE_SIZE
|
|
324
|
+
for (planeIndex in 0 until 3) {
|
|
325
|
+
inputPlanePositions[planeIndex] = planes[planeIndex].position()
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
for (targetY in 0 until INPUT_IMAGE_SIZE) {
|
|
329
|
+
val cropY = (targetY + 0.5f) * sampleScale - 0.5f
|
|
330
|
+
val relativeY = cropY - cropCenter
|
|
331
|
+
for (targetX in 0 until INPUT_IMAGE_SIZE) {
|
|
332
|
+
val cropX = (targetX + 0.5f) * sampleScale - 0.5f
|
|
333
|
+
val relativeX = cropX - cropCenter
|
|
334
|
+
val orientedX = (cropLeft + cropCenter + cosine * relativeX - sine * relativeY)
|
|
335
|
+
.coerceIn(cropLeft.toFloat(), cropRight.toFloat())
|
|
336
|
+
val orientedY = (cropTop + cropCenter + sine * relativeX + cosine * relativeY)
|
|
337
|
+
.coerceIn(cropTop.toFloat(), cropBottom.toFloat())
|
|
338
|
+
val sourceX: Float
|
|
339
|
+
val sourceY: Float
|
|
340
|
+
when (rotation) {
|
|
341
|
+
90 -> {
|
|
342
|
+
sourceX = orientedY
|
|
343
|
+
sourceY = height - 1f - orientedX
|
|
344
|
+
}
|
|
345
|
+
180 -> {
|
|
346
|
+
sourceX = width - 1f - orientedX
|
|
347
|
+
sourceY = height - 1f - orientedY
|
|
348
|
+
}
|
|
349
|
+
270 -> {
|
|
350
|
+
sourceX = width - 1f - orientedY
|
|
351
|
+
sourceY = orientedX
|
|
352
|
+
}
|
|
353
|
+
else -> {
|
|
354
|
+
sourceX = orientedX
|
|
355
|
+
sourceY = orientedY
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
val rgb = sampleYuvBilinear(
|
|
359
|
+
planes,
|
|
360
|
+
rowStrides,
|
|
361
|
+
pixelStrides,
|
|
362
|
+
width,
|
|
363
|
+
height,
|
|
364
|
+
sourceX,
|
|
365
|
+
sourceY,
|
|
366
|
+
)
|
|
367
|
+
val red = (rgb ushr 16) and 0xFF
|
|
368
|
+
val green = (rgb ushr 8) and 0xFF
|
|
369
|
+
val blue = rgb and 0xFF
|
|
370
|
+
modelInputBuffer[targetY][targetX][0] = red / 255f
|
|
371
|
+
modelInputBuffer[targetY][targetX][1] = green / 255f
|
|
372
|
+
modelInputBuffer[targetY][targetX][2] = blue / 255f
|
|
373
|
+
grayBuffer[targetY][targetX] = (
|
|
374
|
+
0.299f * red + 0.587f * green + 0.114f * blue
|
|
375
|
+
).toInt()
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
val nnResult = neuralNetworkAntiSpoofingSampled()
|
|
380
|
+
val nnScore = nnResult.getOrThrow()
|
|
381
|
+
val laplacianScore = calculateLaplacianFromGray(grayBuffer)
|
|
382
|
+
val combinedScore = calculateCombinedScore(nnScore, laplacianScore)
|
|
383
|
+
return AntiSpoofingResult(
|
|
384
|
+
neuralNetworkScore = nnScore,
|
|
385
|
+
laplacianScore = laplacianScore,
|
|
386
|
+
textureComplexity = 0,
|
|
387
|
+
temporalConsistency = 0f,
|
|
388
|
+
colorSaturation = 0f,
|
|
389
|
+
combinedScore = combinedScore,
|
|
390
|
+
confidence = calculateConfidence(nnScore, laplacianScore),
|
|
391
|
+
)
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
private fun sampleYuvBilinear(
|
|
395
|
+
planes: Array<ByteBuffer>,
|
|
396
|
+
rowStrides: IntArray,
|
|
397
|
+
pixelStrides: IntArray,
|
|
398
|
+
sourceWidth: Int,
|
|
399
|
+
sourceHeight: Int,
|
|
400
|
+
x: Float,
|
|
401
|
+
y: Float,
|
|
402
|
+
): Int {
|
|
403
|
+
val boundedX = x.coerceIn(0f, sourceWidth - 1f)
|
|
404
|
+
val boundedY = y.coerceIn(0f, sourceHeight - 1f)
|
|
405
|
+
val x0 = floor(boundedX).toInt()
|
|
406
|
+
val y0 = floor(boundedY).toInt()
|
|
407
|
+
val x1 = min(x0 + 1, sourceWidth - 1)
|
|
408
|
+
val y1 = min(y0 + 1, sourceHeight - 1)
|
|
409
|
+
val xWeight = boundedX - x0
|
|
410
|
+
val yWeight = boundedY - y0
|
|
411
|
+
val topLeft = readYuvRgb(planes, rowStrides, pixelStrides, x0, y0)
|
|
412
|
+
val topRight = readYuvRgb(planes, rowStrides, pixelStrides, x1, y0)
|
|
413
|
+
val bottomLeft = readYuvRgb(planes, rowStrides, pixelStrides, x0, y1)
|
|
414
|
+
val bottomRight = readYuvRgb(planes, rowStrides, pixelStrides, x1, y1)
|
|
415
|
+
|
|
416
|
+
val red = interpolateChannel(topLeft, topRight, bottomLeft, bottomRight, 16, xWeight, yWeight)
|
|
417
|
+
val green = interpolateChannel(topLeft, topRight, bottomLeft, bottomRight, 8, xWeight, yWeight)
|
|
418
|
+
val blue = interpolateChannel(topLeft, topRight, bottomLeft, bottomRight, 0, xWeight, yWeight)
|
|
419
|
+
return (red shl 16) or (green shl 8) or blue
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
private fun interpolateChannel(
|
|
423
|
+
topLeft: Int,
|
|
424
|
+
topRight: Int,
|
|
425
|
+
bottomLeft: Int,
|
|
426
|
+
bottomRight: Int,
|
|
427
|
+
shift: Int,
|
|
428
|
+
xWeight: Float,
|
|
429
|
+
yWeight: Float,
|
|
430
|
+
): Int {
|
|
431
|
+
val top = ((topLeft ushr shift) and 0xFF) * (1 - xWeight) +
|
|
432
|
+
((topRight ushr shift) and 0xFF) * xWeight
|
|
433
|
+
val bottom = ((bottomLeft ushr shift) and 0xFF) * (1 - xWeight) +
|
|
434
|
+
((bottomRight ushr shift) and 0xFF) * xWeight
|
|
435
|
+
return (top * (1 - yWeight) + bottom * yWeight).roundToInt().coerceIn(0, 255)
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
private fun readYuvRgb(
|
|
439
|
+
planes: Array<ByteBuffer>,
|
|
440
|
+
rowStrides: IntArray,
|
|
441
|
+
pixelStrides: IntArray,
|
|
442
|
+
x: Int,
|
|
443
|
+
y: Int,
|
|
444
|
+
): Int {
|
|
445
|
+
val luma = readPlaneSample(0, planes[0], rowStrides[0], pixelStrides[0], x, y)
|
|
446
|
+
val chromaX = x / 2
|
|
447
|
+
val chromaY = y / 2
|
|
448
|
+
val u = readPlaneSample(1, planes[1], rowStrides[1], pixelStrides[1], chromaX, chromaY) - 128
|
|
449
|
+
val v = readPlaneSample(2, planes[2], rowStrides[2], pixelStrides[2], chromaX, chromaY) - 128
|
|
450
|
+
val red = (luma + 1.402f * v).toInt().coerceIn(0, 255)
|
|
451
|
+
val green = (luma - 0.344f * u - 0.714f * v).toInt().coerceIn(0, 255)
|
|
452
|
+
val blue = (luma + 1.772f * u).toInt().coerceIn(0, 255)
|
|
453
|
+
return (red shl 16) or (green shl 8) or blue
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
private fun readPlaneSample(
|
|
457
|
+
planeIndex: Int,
|
|
458
|
+
buffer: ByteBuffer,
|
|
459
|
+
rowStride: Int,
|
|
460
|
+
pixelStride: Int,
|
|
461
|
+
x: Int,
|
|
462
|
+
y: Int,
|
|
463
|
+
): Int {
|
|
464
|
+
val bufferPosition = inputPlanePositions[planeIndex]
|
|
465
|
+
val index = bufferPosition + y * rowStride + x * pixelStride
|
|
466
|
+
require(index >= bufferPosition && index < buffer.limit()) {
|
|
467
|
+
"Camera plane buffer is too small for the requested sample"
|
|
468
|
+
}
|
|
469
|
+
return buffer.get(index).toInt() and 0xFF
|
|
470
|
+
}
|
|
471
|
+
|
|
277
472
|
private fun cropAndAlignFace(
|
|
278
473
|
source: Bitmap,
|
|
279
474
|
faceBounds: RectF,
|
|
@@ -328,7 +523,7 @@ class FaceAntiSpoofingAdvanced(private val assetManager: AssetManager) {
|
|
|
328
523
|
|
|
329
524
|
return try {
|
|
330
525
|
val sampled = sampleRgbaToModelAndGray(rgba, width, height)
|
|
331
|
-
val nnResult = neuralNetworkAntiSpoofingSampled(
|
|
526
|
+
val nnResult = neuralNetworkAntiSpoofingSampled()
|
|
332
527
|
val nnScore = nnResult.getOrNull() ?: throw IllegalStateException(nnResult.exceptionOrNull()?.message ?: "NN inference failed")
|
|
333
528
|
val laplacianScore = calculateLaplacianFromGray(sampled.grayBuffer)
|
|
334
529
|
val combinedScore = calculateCombinedScore(nnScore, laplacianScore)
|
|
@@ -372,7 +567,7 @@ class FaceAntiSpoofingAdvanced(private val assetManager: AssetManager) {
|
|
|
372
567
|
|
|
373
568
|
return try {
|
|
374
569
|
val sampled = sampleRgbaBufferToModelAndGray(buffer, width, height, rowStride, pixelStride)
|
|
375
|
-
val nnResult = neuralNetworkAntiSpoofingSampled(
|
|
570
|
+
val nnResult = neuralNetworkAntiSpoofingSampled()
|
|
376
571
|
val nnScore = nnResult.getOrNull() ?: throw IllegalStateException(nnResult.exceptionOrNull()?.message ?: "NN inference failed")
|
|
377
572
|
val laplacianScore = calculateLaplacianFromGray(sampled.grayBuffer)
|
|
378
573
|
val combinedScore = calculateCombinedScore(nnScore, laplacianScore)
|
|
@@ -419,9 +614,6 @@ class FaceAntiSpoofingAdvanced(private val assetManager: AssetManager) {
|
|
|
419
614
|
|
|
420
615
|
runInference(input)
|
|
421
616
|
|
|
422
|
-
Log.i(TAG, "clss_pred: [${clssPred[0].joinToString()}]")
|
|
423
|
-
Log.i(TAG, "leaf_node_mask: [${leafNodeMask[0].joinToString()}]")
|
|
424
|
-
|
|
425
617
|
val score = leafScore1(clssPred, leafNodeMask)
|
|
426
618
|
|
|
427
619
|
Result.success(score)
|
|
@@ -433,16 +625,13 @@ class FaceAntiSpoofingAdvanced(private val assetManager: AssetManager) {
|
|
|
433
625
|
}
|
|
434
626
|
}
|
|
435
627
|
|
|
436
|
-
private fun neuralNetworkAntiSpoofingSampled(
|
|
628
|
+
private fun neuralNetworkAntiSpoofingSampled(): Result<Float> {
|
|
437
629
|
return try {
|
|
438
|
-
val input = arrayOf(rgbBuffer)
|
|
439
630
|
clssPred[0].fill(0f)
|
|
440
631
|
leafNodeMask[0].fill(0f)
|
|
441
632
|
outputs[outputIndex0] = clssPred
|
|
442
633
|
outputs[outputIndex1] = leafNodeMask
|
|
443
|
-
runInference(
|
|
444
|
-
Log.i(TAG, "clss_pred: [${clssPred[0].joinToString()}]")
|
|
445
|
-
Log.i(TAG, "leaf_node_mask: [${leafNodeMask[0].joinToString()}]")
|
|
634
|
+
runInference(modelInput)
|
|
446
635
|
Result.success(leafScore1(clssPred, leafNodeMask))
|
|
447
636
|
} catch (e: Exception) {
|
|
448
637
|
Log.e(TAG, "Error in neuralNetworkAntiSpoofingSampled", e)
|
package/android/src/main/java/com/margelo/nitro/camera/spoofdetector/HybridSpoofDetectorOutput.kt
CHANGED
|
@@ -22,6 +22,7 @@ import com.margelo.nitro.camera.extensions.setAllowDroppingLateFrames
|
|
|
22
22
|
import com.margelo.nitro.camera.extensions.surfaceRotation
|
|
23
23
|
import com.margelo.nitro.camera.public.NativeCameraOutput
|
|
24
24
|
import com.faceantispoof.FaceAntiSpoofingAdvanced
|
|
25
|
+
import java.nio.ByteBuffer
|
|
25
26
|
import kotlin.math.abs
|
|
26
27
|
import kotlin.math.max
|
|
27
28
|
import kotlin.math.min
|
|
@@ -46,6 +47,10 @@ class HybridSpoofDetectorOutput(
|
|
|
46
47
|
private val busy = AtomicBoolean(false)
|
|
47
48
|
private val disposed = AtomicBoolean(false)
|
|
48
49
|
private val detectorLock = Any()
|
|
50
|
+
private val emptyPlaneBuffer = ByteBuffer.allocate(0)
|
|
51
|
+
private val planeBuffers = Array(3) { emptyPlaneBuffer }
|
|
52
|
+
private val planeRowStrides = IntArray(3)
|
|
53
|
+
private val planePixelStrides = IntArray(3)
|
|
49
54
|
private val detector = FaceAntiSpoofingAdvanced(
|
|
50
55
|
NitroModules.applicationContext?.assets ?: error("FaceAntiSpoof: no application context")
|
|
51
56
|
)
|
|
@@ -127,20 +132,24 @@ class HybridSpoofDetectorOutput(
|
|
|
127
132
|
?: throw IllegalStateException("Face is missing the left eye landmark")
|
|
128
133
|
val rightEye = face.getLandmark(com.google.mlkit.vision.face.FaceLandmark.RIGHT_EYE)?.position
|
|
129
134
|
?: throw IllegalStateException("Face is missing the right eye landmark")
|
|
130
|
-
val
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
|
|
135
|
+
val imagePlanes = image.planes
|
|
136
|
+
if (imagePlanes.size < planeBuffers.size) {
|
|
137
|
+
throw IllegalStateException("Expected Y, U and V camera planes")
|
|
138
|
+
}
|
|
139
|
+
for (planeIndex in planeBuffers.indices) {
|
|
140
|
+
val plane = imagePlanes[planeIndex]
|
|
141
|
+
planeBuffers[planeIndex] = plane.buffer
|
|
142
|
+
planeRowStrides[planeIndex] = plane.rowStride
|
|
143
|
+
planePixelStrides[planeIndex] = plane.pixelStride
|
|
144
|
+
}
|
|
136
145
|
val result = synchronized(detectorLock) {
|
|
137
146
|
if (disposed.get()) return
|
|
138
147
|
detector.advancedAntiSpoofing(
|
|
139
|
-
|
|
148
|
+
planeBuffers,
|
|
140
149
|
image.width,
|
|
141
150
|
image.height,
|
|
142
|
-
|
|
143
|
-
|
|
151
|
+
planeRowStrides,
|
|
152
|
+
planePixelStrides,
|
|
144
153
|
image.imageInfo.rotationDegrees,
|
|
145
154
|
RectF(face.boundingBox),
|
|
146
155
|
PointF(leftEye.x, leftEye.y),
|
|
@@ -160,6 +169,9 @@ class HybridSpoofDetectorOutput(
|
|
|
160
169
|
options.onError?.invoke(error)
|
|
161
170
|
} finally {
|
|
162
171
|
faceDetectorTask.set(null)
|
|
172
|
+
for (planeIndex in planeBuffers.indices) {
|
|
173
|
+
planeBuffers[planeIndex] = emptyPlaneBuffer
|
|
174
|
+
}
|
|
163
175
|
image.close()
|
|
164
176
|
busy.set(false)
|
|
165
177
|
}
|
package/index.d.ts
CHANGED
|
@@ -1,60 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
* React Native Vision Camera Face Anti-Spoofing Detector
|
|
3
|
-
* High-performance face anti-spoofing and liveness detection module
|
|
4
|
-
* TypeScript type definitions
|
|
5
|
-
*/
|
|
1
|
+
import type { CameraOutput } from 'react-native-vision-camera';
|
|
6
2
|
|
|
7
|
-
import type { CameraFrameOutput, Frame, FrameOutputOptions, FrameProcessor } from 'react-native-vision-camera';
|
|
8
|
-
import type { SharedValue } from 'react-native-reanimated';
|
|
9
|
-
|
|
10
|
-
// ============================================================================
|
|
11
|
-
// Core Detection Results
|
|
12
|
-
// ============================================================================
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Face anti-spoofing detection result
|
|
16
|
-
* Contains comprehensive information about face liveness detection
|
|
17
|
-
*/
|
|
18
3
|
export interface FaceAntiSpoofingResult {
|
|
19
|
-
/**
|
|
20
|
-
* Neural network confidence score
|
|
21
|
-
* Range: 0.0 to 1.0
|
|
22
|
-
* Lower values indicate more likely to be live
|
|
23
|
-
*/
|
|
24
4
|
neuralNetworkScore: number;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Laplacian variance-based image quality score
|
|
28
|
-
* Range: 0.0 to 5000+
|
|
29
|
-
* Higher values = better image quality
|
|
30
|
-
* Used to distinguish blur from spoofing
|
|
31
|
-
*/
|
|
32
5
|
laplacianScore: number;
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Combined weighted score from multiple models
|
|
36
|
-
* Range: 0.0 to 1.0
|
|
37
|
-
* Provides robust detection by ensemble voting
|
|
38
|
-
*/
|
|
39
6
|
combinedScore: number;
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Derived score used with the configured liveness thresholds
|
|
43
|
-
*/
|
|
44
7
|
confidence: number;
|
|
45
8
|
}
|
|
46
9
|
|
|
47
|
-
/**
|
|
48
|
-
* Face rectangle/bounds information
|
|
49
|
-
*/
|
|
50
10
|
export interface FaceRect {
|
|
51
|
-
/** X coordinate in pixels */
|
|
52
11
|
x: number;
|
|
53
|
-
/** Y coordinate in pixels */
|
|
54
12
|
y: number;
|
|
55
|
-
/** Face width in pixels */
|
|
56
13
|
width: number;
|
|
57
|
-
/** Face height in pixels */
|
|
58
14
|
height: number;
|
|
59
15
|
}
|
|
60
16
|
|
|
@@ -62,420 +18,62 @@ export interface SpoofDetectedFace {
|
|
|
62
18
|
bounds: FaceRect;
|
|
63
19
|
}
|
|
64
20
|
|
|
65
|
-
|
|
66
|
-
// Anti-spoofing State & Configuration
|
|
67
|
-
// ============================================================================
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Anti-spoofing detection state
|
|
71
|
-
*/
|
|
72
|
-
export interface AntiSpoofState {
|
|
73
|
-
/** Confidence score (0.0-1.0) */
|
|
74
|
-
confidence: number;
|
|
75
|
-
/** Consecutive frames detected as live */
|
|
76
|
-
consecutiveLiveFrames: number;
|
|
77
|
-
/** Last detection result */
|
|
78
|
-
lastResult: FaceAntiSpoofingResult | null;
|
|
79
|
-
/** Whether face is single and detected */
|
|
80
|
-
hasSingleFace: boolean;
|
|
81
|
-
/** Whether face is centered in frame */
|
|
82
|
-
isFaceCentered: boolean;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Face tracking state
|
|
87
|
-
*/
|
|
88
|
-
export interface FaceTrackingState {
|
|
89
|
-
lastX: number;
|
|
90
|
-
lastY: number;
|
|
91
|
-
lastW: number;
|
|
92
|
-
lastH: number;
|
|
93
|
-
stableCount: number;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Liveness detection state
|
|
98
|
-
*/
|
|
99
|
-
export interface LivenessState {
|
|
100
|
-
level: number;
|
|
101
|
-
step: number;
|
|
102
|
-
blinkCount: number;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Complete shared state for frame processing
|
|
107
|
-
*/
|
|
108
|
-
export interface SharedFrameProcessorState {
|
|
109
|
-
lastProcessedTime: number;
|
|
110
|
-
faceTracking: FaceTrackingState;
|
|
111
|
-
flags: {
|
|
112
|
-
captured: boolean;
|
|
113
|
-
showCodeScanner: boolean;
|
|
114
|
-
isActive: boolean;
|
|
115
|
-
hasSingleFace: boolean;
|
|
116
|
-
isFaceCentered: boolean;
|
|
117
|
-
eyeClosed: boolean;
|
|
118
|
-
};
|
|
119
|
-
liveness: LivenessState;
|
|
120
|
-
antiSpoof: AntiSpoofState;
|
|
121
|
-
centering: {
|
|
122
|
-
centeredFrames: number;
|
|
123
|
-
frameWidth: number;
|
|
124
|
-
frameHeight: number;
|
|
125
|
-
};
|
|
126
|
-
performance: {
|
|
127
|
-
batchCounter: number;
|
|
128
|
-
lastBatchUpdate: number;
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* Face detection update callback data
|
|
134
|
-
*/
|
|
135
|
-
export interface FacesUpdateData {
|
|
136
|
-
count: number;
|
|
137
|
-
progress: number;
|
|
138
|
-
step: number;
|
|
139
|
-
isCentered: boolean;
|
|
140
|
-
antiSpoofState: AntiSpoofState;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Anti-spoof update callback data
|
|
145
|
-
*/
|
|
146
|
-
export interface OnAntiSpoofUpdateData {
|
|
147
|
-
confidence: number;
|
|
148
|
-
rawResult?: FaceAntiSpoofingResult;
|
|
149
|
-
consecutiveLiveFrames: number;
|
|
150
|
-
isFaceCentered: boolean;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// ============================================================================
|
|
154
|
-
// Hook Configuration
|
|
155
|
-
// ============================================================================
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* Configuration options for useFaceDetectionFrameProcessor hook
|
|
159
|
-
*/
|
|
160
|
-
export interface UseFaceDetectionFrameProcessorOptions {
|
|
161
|
-
/**
|
|
162
|
-
* Callback when a stable, live face is detected
|
|
163
|
-
* @param faceRect - Face position and size
|
|
164
|
-
* @param antiSpoofResult - Anti-spoofing detection result
|
|
165
|
-
*/
|
|
166
|
-
onStableFaceDetected?: (
|
|
167
|
-
faceRect: FaceRect,
|
|
168
|
-
antiSpoofResult?: FaceAntiSpoofingResult
|
|
169
|
-
) => void;
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* Callback for face updates during detection
|
|
173
|
-
* Called periodically with detection progress
|
|
174
|
-
*/
|
|
175
|
-
onFacesUpdate?: (data: FacesUpdateData) => void;
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Callback for liveness verification updates
|
|
179
|
-
* @param step - Liveness step (0-2)
|
|
180
|
-
* @param extra - Additional data like blink count
|
|
181
|
-
*/
|
|
182
|
-
onLivenessUpdate?: (step: number, extra?: Record<string, any>) => void;
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* Callback for anti-spoof detection updates
|
|
186
|
-
* Called when anti-spoofing detection result changes
|
|
187
|
-
*/
|
|
188
|
-
onAntiSpoofUpdate?: (result: OnAntiSpoofUpdateData) => void;
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Show code scanner overlay
|
|
192
|
-
* @default false
|
|
193
|
-
*/
|
|
194
|
-
showCodeScanner?: boolean;
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* Loading state - pauses frame processing
|
|
198
|
-
* @default false
|
|
199
|
-
*/
|
|
200
|
-
isLoading?: boolean;
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* Enable/disable frame processing
|
|
204
|
-
* @default true
|
|
205
|
-
*/
|
|
206
|
-
isActive?: boolean;
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* Liveness verification level
|
|
210
|
-
* @values 0 - Anti-spoofing only (fast)
|
|
211
|
-
* @values 1 - Anti-spoofing + blink detection (more thorough)
|
|
212
|
-
* @default 0
|
|
213
|
-
*/
|
|
214
|
-
livenessLevel?: number;
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* Anti-spoofing confidence threshold
|
|
218
|
-
* Range: 0.0 - 1.0
|
|
219
|
-
* Lower values = more lenient, higher values = stricter
|
|
220
|
-
* @default 0.35
|
|
221
|
-
*/
|
|
222
|
-
antispooflevel?: number;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* Return value from useFaceDetectionFrameProcessor hook
|
|
227
|
-
*/
|
|
228
|
-
export interface UseFaceDetectionFrameProcessorReturn {
|
|
229
|
-
/**
|
|
230
|
-
* Frame processor function for Vision Camera
|
|
231
|
-
*/
|
|
232
|
-
frameProcessor: FrameProcessor;
|
|
233
|
-
|
|
234
|
-
/**
|
|
235
|
-
* Reset capture state to initial values
|
|
236
|
-
*/
|
|
237
|
-
resetCaptureState: () => void;
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* Force reset all states including shared values
|
|
241
|
-
*/
|
|
242
|
-
forceResetCaptureState: () => void;
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* Update code scanner visibility
|
|
246
|
-
*/
|
|
247
|
-
updateShowCodeScanner: (value: boolean) => void;
|
|
248
|
-
|
|
249
|
-
/**
|
|
250
|
-
* Update processing active state
|
|
251
|
-
*/
|
|
252
|
-
updateIsActive: (active: boolean) => void;
|
|
253
|
-
|
|
254
|
-
/**
|
|
255
|
-
* Initialize anti-spoofing module
|
|
256
|
-
*/
|
|
257
|
-
initializeAntiSpoof: () => Promise<boolean>;
|
|
258
|
-
|
|
259
|
-
/**
|
|
260
|
-
* Shared value for capture state
|
|
261
|
-
* Use .value to access: capturedSV.value.value
|
|
262
|
-
*/
|
|
263
|
-
capturedSV: SharedValue<boolean>;
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* Current anti-spoofing detection state
|
|
267
|
-
*/
|
|
268
|
-
antiSpoofState: AntiSpoofState;
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
* VisionCamera 5 frame-output options for spoof detection.
|
|
273
|
-
* The callback runs on the React Native runtime after native inference.
|
|
274
|
-
*/
|
|
275
|
-
export interface UseFaceAntiSpoofFrameOutputOptions extends Partial<FrameOutputOptions> {
|
|
21
|
+
export interface UseFaceAntiSpoofFrameOutputOptions {
|
|
276
22
|
antispoofLevel?: number;
|
|
277
23
|
stableFrames?: number;
|
|
278
24
|
requiredLiveFrames?: number;
|
|
279
25
|
laplacianThreshold?: number;
|
|
280
26
|
maxNeuralSpoofScore?: number;
|
|
281
|
-
onResult?: (result: FaceAntiSpoofingResult) => void;
|
|
282
27
|
onFacesDetected?: (faces: SpoofDetectedFace[]) => void;
|
|
283
|
-
|
|
28
|
+
onResult?: (result: FaceAntiSpoofingResult) => void;
|
|
29
|
+
onStableFaceDetected?: (
|
|
30
|
+
face: FaceRect,
|
|
31
|
+
result: FaceAntiSpoofingResult,
|
|
32
|
+
) => void;
|
|
284
33
|
onError?: (error: Error) => void;
|
|
285
34
|
}
|
|
286
35
|
|
|
287
|
-
|
|
288
|
-
export function useFaceAntiSpoofFrameOutput(
|
|
289
|
-
options?: UseFaceAntiSpoofFrameOutputOptions,
|
|
290
|
-
): CameraFrameOutput;
|
|
291
|
-
|
|
292
|
-
export function resetFaceAntiSpoofFrameOutput(output: CameraFrameOutput): void;
|
|
293
|
-
|
|
294
|
-
// ============================================================================
|
|
295
|
-
// Module Functions
|
|
296
|
-
// ============================================================================
|
|
297
|
-
|
|
298
|
-
/**
|
|
299
|
-
* Frame processor function for real-time face anti-spoofing detection
|
|
300
|
-
* Call within a Vision Camera frame processor
|
|
301
|
-
*
|
|
302
|
-
* @example
|
|
303
|
-
* ```typescript
|
|
304
|
-
* const frameProcessor = useFrameProcessor((frame) => {
|
|
305
|
-
* 'worklet';
|
|
306
|
-
* const result = faceAntiSpoofFrameProcessor(frame);
|
|
307
|
-
* if (result && result.confidence > 0.5) {
|
|
308
|
-
* // Evaluate confidence together with neuralNetworkScore and laplacianScore.
|
|
309
|
-
* }
|
|
310
|
-
* }, []);
|
|
311
|
-
* ```
|
|
312
|
-
*
|
|
313
|
-
* @param frame - Vision Camera frame object
|
|
314
|
-
* @returns Detection result or null if no face detected
|
|
315
|
-
*/
|
|
316
|
-
export function faceAntiSpoofFrameProcessor(
|
|
317
|
-
frame: Frame
|
|
318
|
-
): FaceAntiSpoofingResult | null;
|
|
319
|
-
|
|
320
|
-
/**
|
|
321
|
-
* Check if face anti-spoofing module is available on device
|
|
322
|
-
* Call before attempting to use the module
|
|
323
|
-
*
|
|
324
|
-
* @returns true if module is available, false otherwise
|
|
325
|
-
*/
|
|
326
|
-
export function isFaceAntiSpoofAvailable(): boolean;
|
|
327
|
-
|
|
328
|
-
/**
|
|
329
|
-
* Initialize the face anti-spoofing module
|
|
330
|
-
* Must be called before using the frame processor
|
|
331
|
-
*
|
|
332
|
-
* @returns Promise<boolean> - true if initialization successful
|
|
333
|
-
*
|
|
334
|
-
* @example
|
|
335
|
-
* ```typescript
|
|
336
|
-
* useEffect(() => {
|
|
337
|
-
* initializeFaceAntiSpoof()
|
|
338
|
-
* .then(success => {
|
|
339
|
-
* if (success) {
|
|
340
|
-
* console.log('Anti-spoof ready');
|
|
341
|
-
* }
|
|
342
|
-
* });
|
|
343
|
-
* }, []);
|
|
344
|
-
* ```
|
|
345
|
-
*/
|
|
346
|
-
export function initializeFaceAntiSpoof(): Promise<boolean>;
|
|
347
|
-
|
|
348
|
-
/**
|
|
349
|
-
* Custom React Native hook for comprehensive face detection with anti-spoofing
|
|
350
|
-
* Provides advanced features like blink detection and face centering
|
|
351
|
-
*
|
|
352
|
-
* @param options - Configuration options
|
|
353
|
-
* @returns Hook return value with frame processor and callbacks
|
|
354
|
-
*
|
|
355
|
-
* @example
|
|
356
|
-
* ```typescript
|
|
357
|
-
* const { frameProcessor, resetCaptureState, antiSpoofState } =
|
|
358
|
-
* useFaceDetectionFrameProcessor({
|
|
359
|
-
* livenessLevel: 1,
|
|
360
|
-
* antispooflevel: 0.35,
|
|
361
|
-
* onStableFaceDetected: (rect, result) => {
|
|
362
|
-
* console.log('Capture complete!', rect);
|
|
363
|
-
* },
|
|
364
|
-
* });
|
|
365
|
-
* ```
|
|
366
|
-
*/
|
|
367
|
-
export function useFaceDetectionFrameProcessor(
|
|
368
|
-
options: UseFaceDetectionFrameProcessorOptions
|
|
369
|
-
): UseFaceDetectionFrameProcessorReturn;
|
|
370
|
-
|
|
371
|
-
// ============================================================================
|
|
372
|
-
// Accelerator Types
|
|
373
|
-
// ============================================================================
|
|
374
|
-
|
|
375
|
-
/**
|
|
376
|
-
* TensorFlow Lite accelerator type
|
|
377
|
-
* Determines which hardware is used for inference
|
|
378
|
-
*/
|
|
379
|
-
export type AcceleratorType = 'CPU' | 'GPU' | 'NNAPI';
|
|
380
|
-
|
|
381
|
-
/**
|
|
382
|
-
* Module information
|
|
383
|
-
*/
|
|
384
|
-
export interface ModuleInfo {
|
|
385
|
-
/** Module name */
|
|
386
|
-
name: string;
|
|
387
|
-
/** Available API methods */
|
|
388
|
-
methods: string[];
|
|
389
|
-
/** Module version */
|
|
390
|
-
version: string;
|
|
391
|
-
/** Active accelerator (if available) */
|
|
392
|
-
accelerator?: AcceleratorType;
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
/**
|
|
396
|
-
* Model status information
|
|
397
|
-
*/
|
|
398
|
-
export interface ModelStatus {
|
|
399
|
-
/** Whether plugin is available on this device */
|
|
36
|
+
export interface FaceAntiSpoofModelStatus {
|
|
400
37
|
pluginAvailable: boolean;
|
|
401
|
-
/** Whether TensorFlow Lite model is loaded */
|
|
402
38
|
modelLoaded: boolean;
|
|
403
|
-
|
|
404
|
-
accelerator?: AcceleratorType;
|
|
39
|
+
moduleInitialized: boolean;
|
|
405
40
|
}
|
|
406
41
|
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
42
|
+
export interface FaceAntiSpoofModuleInfo {
|
|
43
|
+
name: string;
|
|
44
|
+
methods: string[];
|
|
45
|
+
accelerators?: string[];
|
|
46
|
+
}
|
|
410
47
|
|
|
411
|
-
/**
|
|
412
|
-
* Native module interface for advanced use cases
|
|
413
|
-
* Generally not needed - use the exported functions instead
|
|
414
|
-
*/
|
|
415
48
|
export interface FaceAntiSpoofModule {
|
|
416
|
-
/**
|
|
417
|
-
* Initialize native module
|
|
418
|
-
*/
|
|
419
49
|
initialize(): Promise<boolean>;
|
|
420
|
-
|
|
421
|
-
/**
|
|
422
|
-
* Check model loading status
|
|
423
|
-
*/
|
|
424
|
-
checkModelStatus(): Promise<ModelStatus>;
|
|
425
|
-
|
|
426
|
-
/**
|
|
427
|
-
* Check module availability
|
|
428
|
-
*/
|
|
50
|
+
checkModelStatus(): Promise<FaceAntiSpoofModelStatus>;
|
|
429
51
|
isAvailable(): Promise<boolean>;
|
|
430
|
-
|
|
431
|
-
/**
|
|
432
|
-
* Test connection to native module
|
|
433
|
-
*/
|
|
434
52
|
testMethod(): Promise<string>;
|
|
435
|
-
|
|
436
|
-
/**
|
|
437
|
-
* Get module information
|
|
438
|
-
*/
|
|
439
|
-
getModuleInfo(): Promise<ModuleInfo>;
|
|
440
|
-
|
|
441
|
-
/**
|
|
442
|
-
* Install/setup native dependencies
|
|
443
|
-
*/
|
|
53
|
+
getModuleInfo(): Promise<FaceAntiSpoofModuleInfo>;
|
|
444
54
|
install(): Promise<boolean>;
|
|
55
|
+
processFrame?(
|
|
56
|
+
frameData: number[][],
|
|
57
|
+
width: number,
|
|
58
|
+
height: number,
|
|
59
|
+
rowStrides: number[],
|
|
60
|
+
pixelStrides: number[],
|
|
61
|
+
): Promise<FaceAntiSpoofingResult>;
|
|
62
|
+
cleanup?(): Promise<boolean>;
|
|
63
|
+
getAccelerator?(): Promise<string>;
|
|
445
64
|
}
|
|
446
65
|
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
declare const FaceAntiSpoof: FaceAntiSpoofModule;
|
|
451
|
-
|
|
452
|
-
export default FaceAntiSpoof;
|
|
453
|
-
|
|
454
|
-
// ============================================================================
|
|
455
|
-
// Constants
|
|
456
|
-
// ============================================================================
|
|
457
|
-
|
|
458
|
-
/**
|
|
459
|
-
* Default anti-spoofing confidence threshold
|
|
460
|
-
*/
|
|
461
|
-
export const DEFAULT_ANTI_SPOOF_LEVEL = 0.35;
|
|
66
|
+
export declare function useFaceAntiSpoofFrameOutput(
|
|
67
|
+
options?: UseFaceAntiSpoofFrameOutputOptions,
|
|
68
|
+
): CameraOutput;
|
|
462
69
|
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
export const DEFAULT_FACE_STABILITY_THRESHOLD = 3;
|
|
70
|
+
export declare function resetFaceAntiSpoofFrameOutput(
|
|
71
|
+
output: CameraOutput,
|
|
72
|
+
): void;
|
|
467
73
|
|
|
468
|
-
|
|
469
|
-
* Default face movement threshold (pixels)
|
|
470
|
-
*/
|
|
471
|
-
export const DEFAULT_FACE_MOVEMENT_THRESHOLD = 15;
|
|
74
|
+
export declare function isFaceAntiSpoofAvailable(): boolean;
|
|
472
75
|
|
|
473
|
-
|
|
474
|
-
* Minimum required blinks for liveness verification
|
|
475
|
-
*/
|
|
476
|
-
export const DEFAULT_REQUIRED_BLINKS = 3;
|
|
76
|
+
export declare function initializeFaceAntiSpoof(): Promise<boolean>;
|
|
477
77
|
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
*/
|
|
481
|
-
export const DEFAULT_BLINK_THRESHOLD = 0.3;
|
|
78
|
+
declare const FaceAntiSpoof: FaceAntiSpoofModule | undefined;
|
|
79
|
+
export default FaceAntiSpoof;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-vision-camera-spoof-detector",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.8",
|
|
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": {
|