react-native-vision-camera-spoof-detector 1.3.7 → 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.
|
@@ -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/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": {
|