react-native-vision-camera-spoof-detector 1.3.8 → 1.3.10
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/android/src/main/java/com/margelo/nitro/camera/spoofdetector/HybridSpoofDetectorOutput.kt +57 -3
- package/ios/HybridSpoofDetectorOutput.swift +35 -8
- package/nitrogen/generated/ios/VisionCameraSpoofDetector-Swift-Cxx-Bridge.hpp +1 -1
- package/nitrogen/generated/ios/VisionCameraSpoofDetector-Swift-Cxx-Umbrella.hpp +1 -1
- package/nitrogen/generated/ios/c++/HybridSpoofDetectorFactorySpecSwift.hpp +1 -1
- package/nitrogen/generated/ios/swift/SpoofDetectorOutputOptions.swift +0 -1
- package/nitrogen/generated/shared/c++/SpoofDetectorSize.hpp +7 -0
- package/package.json +1 -1
package/android/src/main/java/com/margelo/nitro/camera/spoofdetector/HybridSpoofDetectorOutput.kt
CHANGED
|
@@ -2,6 +2,7 @@ package com.margelo.nitro.camera.spoofdetector
|
|
|
2
2
|
|
|
3
3
|
import android.graphics.Matrix
|
|
4
4
|
import android.graphics.PointF
|
|
5
|
+
import android.graphics.Rect
|
|
5
6
|
import android.graphics.RectF
|
|
6
7
|
import androidx.annotation.OptIn
|
|
7
8
|
import androidx.camera.core.ExperimentalGetImage
|
|
@@ -72,6 +73,8 @@ class HybridSpoofDetectorOutput(
|
|
|
72
73
|
private var captured = false
|
|
73
74
|
private var stableCount = 0
|
|
74
75
|
private var lastFaceBounds: SpoofDetectedFaceBounds? = null
|
|
76
|
+
private var lastFaceBoundsForContinuity: RectF? = null
|
|
77
|
+
private var lastFaceTrackingId: Int? = null
|
|
75
78
|
private var consecutiveLiveFrames = 0
|
|
76
79
|
private var lastSpoofResult: AntiSpoofingResult? = null
|
|
77
80
|
private var imageAnalysis: ImageAnalysis? = null
|
|
@@ -80,6 +83,8 @@ class HybridSpoofDetectorOutput(
|
|
|
80
83
|
private const val MIN_FACE_HEIGHT_RATIO = 0.18f
|
|
81
84
|
private const val FACE_FRAME_MARGIN_RATIO = 0.02f
|
|
82
85
|
private const val MAX_FACE_POSE_ANGLE_DEGREES = 25f
|
|
86
|
+
private const val MIN_FACE_BOUNDS_IOU = 0.35f
|
|
87
|
+
private const val MAX_FACE_CENTER_SHIFT_RATIO = 0.5f
|
|
83
88
|
private const val MIN_FRAME_BRIGHTNESS = 24f
|
|
84
89
|
private const val MAX_FRAME_BRIGHTNESS = 220f
|
|
85
90
|
}
|
|
@@ -183,19 +188,39 @@ class HybridSpoofDetectorOutput(
|
|
|
183
188
|
frameWidth: Int,
|
|
184
189
|
frameHeight: Int,
|
|
185
190
|
) {
|
|
186
|
-
val
|
|
187
|
-
|
|
191
|
+
val eligibleFaces = faces.filter { it.isFullFace(frameWidth, frameHeight) }
|
|
192
|
+
val detectedFaces = eligibleFaces
|
|
188
193
|
.map { it.toSpoofDetectedFace(transform) }
|
|
189
194
|
.toTypedArray()
|
|
190
195
|
synchronized(stateLock) {
|
|
191
|
-
if (
|
|
196
|
+
if (eligibleFaces.size == 1) {
|
|
197
|
+
val face = eligibleFaces.single()
|
|
198
|
+
val normalizedBounds = face.boundingBox.toNormalizedFaceBounds(frameWidth, frameHeight)
|
|
199
|
+
val trackingId = face.trackingId
|
|
200
|
+
val sameTrackedFace = lastFaceTrackingId == null || trackingId == null ||
|
|
201
|
+
lastFaceTrackingId == trackingId
|
|
202
|
+
val continuousBounds = lastFaceBoundsForContinuity?.let {
|
|
203
|
+
it.hasContinuousFace(normalizedBounds)
|
|
204
|
+
} ?: true
|
|
205
|
+
|
|
206
|
+
if (!sameTrackedFace || !continuousBounds) {
|
|
207
|
+
captured = false
|
|
208
|
+
stableCount = 0
|
|
209
|
+
consecutiveLiveFrames = 0
|
|
210
|
+
lastSpoofResult = null
|
|
211
|
+
}
|
|
212
|
+
|
|
192
213
|
val bounds = detectedFaces[0].bounds
|
|
193
214
|
stableCount += 1
|
|
194
215
|
lastFaceBounds = bounds
|
|
216
|
+
lastFaceBoundsForContinuity = normalizedBounds
|
|
217
|
+
lastFaceTrackingId = trackingId
|
|
195
218
|
} else {
|
|
196
219
|
captured = false
|
|
197
220
|
stableCount = 0
|
|
198
221
|
lastFaceBounds = null
|
|
222
|
+
lastFaceBoundsForContinuity = null
|
|
223
|
+
lastFaceTrackingId = null
|
|
199
224
|
consecutiveLiveFrames = 0
|
|
200
225
|
lastSpoofResult = null
|
|
201
226
|
}
|
|
@@ -205,6 +230,33 @@ class HybridSpoofDetectorOutput(
|
|
|
205
230
|
options.onFacesDetected?.invoke(detectedFaces)
|
|
206
231
|
}
|
|
207
232
|
|
|
233
|
+
private fun Rect.toNormalizedFaceBounds(frameWidth: Int, frameHeight: Int): RectF =
|
|
234
|
+
RectF(
|
|
235
|
+
left.toFloat() / frameWidth,
|
|
236
|
+
top.toFloat() / frameHeight,
|
|
237
|
+
right.toFloat() / frameWidth,
|
|
238
|
+
bottom.toFloat() / frameHeight,
|
|
239
|
+
)
|
|
240
|
+
|
|
241
|
+
private fun RectF.hasContinuousFace(current: RectF): Boolean {
|
|
242
|
+
val intersectionWidth = (min(right, current.right) - max(left, current.left)).coerceAtLeast(0f)
|
|
243
|
+
val intersectionHeight = (min(bottom, current.bottom) - max(top, current.top)).coerceAtLeast(0f)
|
|
244
|
+
val intersectionArea = intersectionWidth * intersectionHeight
|
|
245
|
+
val unionArea = width() * height() + current.width() * current.height() - intersectionArea
|
|
246
|
+
if (unionArea <= 0f || intersectionArea / unionArea < MIN_FACE_BOUNDS_IOU) return false
|
|
247
|
+
|
|
248
|
+
val centerDistance = kotlin.math.hypot(
|
|
249
|
+
centerX() - current.centerX(),
|
|
250
|
+
centerY() - current.centerY(),
|
|
251
|
+
)
|
|
252
|
+
val maxFaceDiagonal = max(
|
|
253
|
+
kotlin.math.hypot(width(), height()),
|
|
254
|
+
kotlin.math.hypot(current.width(), current.height()),
|
|
255
|
+
)
|
|
256
|
+
return maxFaceDiagonal > 0f &&
|
|
257
|
+
centerDistance <= maxFaceDiagonal * MAX_FACE_CENTER_SHIFT_RATIO
|
|
258
|
+
}
|
|
259
|
+
|
|
208
260
|
private fun processAntiSpoofResult(result: AntiSpoofingResult) {
|
|
209
261
|
var capture: Pair<SpoofDetectedFaceBounds, AntiSpoofingResult>? = null
|
|
210
262
|
|
|
@@ -256,6 +308,8 @@ class HybridSpoofDetectorOutput(
|
|
|
256
308
|
captured = false
|
|
257
309
|
stableCount = 0
|
|
258
310
|
lastFaceBounds = null
|
|
311
|
+
lastFaceBoundsForContinuity = null
|
|
312
|
+
lastFaceTrackingId = null
|
|
259
313
|
consecutiveLiveFrames = 0
|
|
260
314
|
lastSpoofResult = null
|
|
261
315
|
}
|
|
@@ -23,6 +23,7 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
23
23
|
private var captured = false
|
|
24
24
|
private var stableCount = 0
|
|
25
25
|
private var lastFaceBounds: SpoofDetectedFaceBounds?
|
|
26
|
+
private var lastFaceObservationBounds: CGRect?
|
|
26
27
|
private var consecutiveLiveFrames = 0
|
|
27
28
|
private var lastSpoofResult: AntiSpoofingResult?
|
|
28
29
|
|
|
@@ -82,11 +83,9 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
82
83
|
self.captured = false
|
|
83
84
|
self.stableCount = 0
|
|
84
85
|
self.lastFaceBounds = nil
|
|
86
|
+
self.lastFaceObservationBounds = nil
|
|
85
87
|
self.consecutiveLiveFrames = 0
|
|
86
88
|
self.lastSpoofResult = nil
|
|
87
|
-
self.lastFaceCount = -1
|
|
88
|
-
self.lastFacesUpdateAt = 0
|
|
89
|
-
self.lastResultUpdateAt = 0
|
|
90
89
|
self.stateLock.unlock()
|
|
91
90
|
}
|
|
92
91
|
}
|
|
@@ -114,7 +113,7 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
114
113
|
}
|
|
115
114
|
|
|
116
115
|
let faces = detectFaces(pixelBuffer)
|
|
117
|
-
processDetectedFaces(faces
|
|
116
|
+
processDetectedFaces(faces)
|
|
118
117
|
|
|
119
118
|
guard shouldProcessFrame(pixelBuffer), faces.count == 1,
|
|
120
119
|
let input = makeRGBInput(from: pixelBuffer, face: faces[0]) else {
|
|
@@ -200,20 +199,48 @@ final class HybridSpoofDetectorOutput: HybridCameraOutputSpec, NativeCameraOutpu
|
|
|
200
199
|
}
|
|
201
200
|
}
|
|
202
201
|
|
|
203
|
-
private func processDetectedFaces(_ faces: [
|
|
202
|
+
private func processDetectedFaces(_ faces: [DetectedFace]) {
|
|
204
203
|
if faces.count == 1 {
|
|
205
|
-
let
|
|
204
|
+
let detectedFace = faces[0]
|
|
205
|
+
let bounds = detectedFace.observation.boundingBox
|
|
206
|
+
let isContinuous = lastFaceObservationBounds.map {
|
|
207
|
+
hasContinuousFace(from: $0, to: bounds)
|
|
208
|
+
} ?? true
|
|
209
|
+
if !isContinuous {
|
|
210
|
+
captured = false
|
|
211
|
+
stableCount = 0
|
|
212
|
+
consecutiveLiveFrames = 0
|
|
213
|
+
lastSpoofResult = nil
|
|
214
|
+
}
|
|
206
215
|
stableCount += 1
|
|
207
|
-
lastFaceBounds = bounds
|
|
216
|
+
lastFaceBounds = detectedFace.face.bounds
|
|
217
|
+
lastFaceObservationBounds = bounds
|
|
208
218
|
} else {
|
|
209
219
|
captured = false
|
|
210
220
|
stableCount = 0
|
|
211
221
|
lastFaceBounds = nil
|
|
222
|
+
lastFaceObservationBounds = nil
|
|
212
223
|
consecutiveLiveFrames = 0
|
|
213
224
|
lastSpoofResult = nil
|
|
214
225
|
}
|
|
215
226
|
|
|
216
|
-
options.onFacesDetected?(faces)
|
|
227
|
+
options.onFacesDetected?(faces.map(\.face))
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
private func hasContinuousFace(from previous: CGRect, to current: CGRect) -> Bool {
|
|
231
|
+
let intersection = previous.intersection(current)
|
|
232
|
+
guard !intersection.isNull else { return false }
|
|
233
|
+
let intersectionArea = intersection.width * intersection.height
|
|
234
|
+
let unionArea = previous.width * previous.height + current.width * current.height - intersectionArea
|
|
235
|
+
guard unionArea > 0,
|
|
236
|
+
intersectionArea / unionArea >= 0.35 else {
|
|
237
|
+
return false
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
let centerDistance = hypot(previous.midX - current.midX, previous.midY - current.midY)
|
|
241
|
+
let previousDiagonal = hypot(previous.width, previous.height)
|
|
242
|
+
let currentDiagonal = hypot(current.width, current.height)
|
|
243
|
+
return centerDistance <= max(previousDiagonal, currentDiagonal) * 0.5
|
|
217
244
|
}
|
|
218
245
|
|
|
219
246
|
private func processAntiSpoofResult(_ result: AntiSpoofingResult) {
|
|
@@ -30,7 +30,7 @@ namespace VisionCameraSpoofDetector { class HybridSpoofDetectorFactorySpec_cxx;
|
|
|
30
30
|
// Include C++ defined types
|
|
31
31
|
#include "AntiSpoofingResult.hpp"
|
|
32
32
|
#include "HybridSpoofDetectorFactorySpec.hpp"
|
|
33
|
-
#include "
|
|
33
|
+
#include "SpoofDetectorSize.hpp"
|
|
34
34
|
#include "SpoofDetectedFace.hpp"
|
|
35
35
|
#include "SpoofDetectedFaceBounds.hpp"
|
|
36
36
|
#include <NitroModules/Result.hpp>
|
|
@@ -26,7 +26,7 @@ namespace margelo::nitro::camera::spoofdetector { struct SpoofDetectorOutputOpti
|
|
|
26
26
|
// Include C++ defined types
|
|
27
27
|
#include "AntiSpoofingResult.hpp"
|
|
28
28
|
#include "HybridSpoofDetectorFactorySpec.hpp"
|
|
29
|
-
#include "
|
|
29
|
+
#include "SpoofDetectorSize.hpp"
|
|
30
30
|
#include "SpoofDetectedFace.hpp"
|
|
31
31
|
#include "SpoofDetectedFaceBounds.hpp"
|
|
32
32
|
#include "SpoofDetectorOutputOptions.hpp"
|
|
@@ -28,7 +28,7 @@ namespace margelo::nitro::camera::spoofdetector { struct SpoofDetectedFaceBounds
|
|
|
28
28
|
#include <memory>
|
|
29
29
|
#include <VisionCamera/HybridCameraOutputSpec.hpp>
|
|
30
30
|
#include "SpoofDetectorOutputOptions.hpp"
|
|
31
|
-
#include "
|
|
31
|
+
#include "SpoofDetectorSize.hpp"
|
|
32
32
|
#include <optional>
|
|
33
33
|
#include "AntiSpoofingResult.hpp"
|
|
34
34
|
#include <functional>
|
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.10",
|
|
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": {
|