react-native-vision-camera-spoof-detector 1.2.5 → 1.2.7
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/faceantispoof/FaceAntiSpoofModule.kt +3 -10
- package/android/src/main/java/com/faceantispoof/FaceAntiSpoofingAdvanced.kt +0 -2
- package/android/src/main/java/com/margelo/nitro/camera/spoofdetector/HybridSpoofDetectorOutput.kt +54 -16
- package/index.js +50 -50
- package/nitrogen/generated/android/c++/JAntiSpoofingResult.hpp +7 -17
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/camera/spoofdetector/AntiSpoofingResult.kt +3 -13
- package/nitrogen/generated/shared/c++/AntiSpoofingResult.hpp +2 -0
- package/package.json +1 -1
|
@@ -24,7 +24,7 @@ class FaceAntiSpoofModule(reactContext: ReactApplicationContext) : ReactContextB
|
|
|
24
24
|
pixelStrides: IntArray
|
|
25
25
|
): DoubleArray {
|
|
26
26
|
val module = activeModule
|
|
27
|
-
?: return doubleArrayOf(0.0, 0.0, 0.0, 0.0
|
|
27
|
+
?: return doubleArrayOf(0.0, 0.0, 0.0, 0.0)
|
|
28
28
|
return module.processNativeFrame(planes, width, height, rowStrides, pixelStrides)
|
|
29
29
|
}
|
|
30
30
|
}
|
|
@@ -121,13 +121,9 @@ class FaceAntiSpoofModule(reactContext: ReactApplicationContext) : ReactContextB
|
|
|
121
121
|
yuvRowStrides,
|
|
122
122
|
yuvPixelStrides
|
|
123
123
|
)
|
|
124
|
-
val isLive = result.neuralNetworkScore < FaceAntiSpoofingAdvanced.BASE_THRESHOLD &&
|
|
125
|
-
result.laplacianScore > FaceAntiSpoofingAdvanced.LAPLACIAN_THRESHOLD
|
|
126
124
|
promise.resolve(
|
|
127
125
|
Arguments.makeNativeMap(
|
|
128
126
|
mapOf(
|
|
129
|
-
"isLive" to isLive,
|
|
130
|
-
"label" to if (isLive) "Live Face" else "Spoof Face",
|
|
131
127
|
"neuralNetworkScore" to result.neuralNetworkScore.toDouble(),
|
|
132
128
|
"laplacianScore" to result.laplacianScore,
|
|
133
129
|
"combinedScore" to result.combinedScore.toDouble(),
|
|
@@ -149,7 +145,7 @@ class FaceAntiSpoofModule(reactContext: ReactApplicationContext) : ReactContextB
|
|
|
149
145
|
pixelStrides: IntArray
|
|
150
146
|
): DoubleArray {
|
|
151
147
|
if (!isModuleInitialized || faceAntiSpoofing == null || planes.size < 3) {
|
|
152
|
-
return doubleArrayOf(0.0, 0.0, 0.0, 0.0
|
|
148
|
+
return doubleArrayOf(0.0, 0.0, 0.0, 0.0)
|
|
153
149
|
}
|
|
154
150
|
|
|
155
151
|
return try {
|
|
@@ -160,10 +156,7 @@ class FaceAntiSpoofModule(reactContext: ReactApplicationContext) : ReactContextB
|
|
|
160
156
|
rowStrides,
|
|
161
157
|
pixelStrides
|
|
162
158
|
)
|
|
163
|
-
val isLive = result.neuralNetworkScore < FaceAntiSpoofingAdvanced.BASE_THRESHOLD &&
|
|
164
|
-
result.laplacianScore > FaceAntiSpoofingAdvanced.LAPLACIAN_THRESHOLD
|
|
165
159
|
doubleArrayOf(
|
|
166
|
-
if (isLive) 1.0 else 0.0,
|
|
167
160
|
result.neuralNetworkScore.toDouble(),
|
|
168
161
|
result.laplacianScore.toDouble(),
|
|
169
162
|
result.combinedScore.toDouble(),
|
|
@@ -171,7 +164,7 @@ class FaceAntiSpoofModule(reactContext: ReactApplicationContext) : ReactContextB
|
|
|
171
164
|
)
|
|
172
165
|
} catch (error: Exception) {
|
|
173
166
|
android.util.Log.e("FaceAntiSpoof", "Native frame processing failed", error)
|
|
174
|
-
doubleArrayOf(0.0, 0.0, 0.0, 0.0
|
|
167
|
+
doubleArrayOf(0.0, 0.0, 0.0, 0.0)
|
|
175
168
|
}
|
|
176
169
|
}
|
|
177
170
|
|
|
@@ -127,7 +127,6 @@ class FaceAntiSpoofingAdvanced(private val assetManager: AssetManager) {
|
|
|
127
127
|
} else {
|
|
128
128
|
val nnScore = neuralNetworkAntiSpoofing(bitmap).getOrThrow()
|
|
129
129
|
val laplacianScore = laplacian(bitmap).getOrThrow()
|
|
130
|
-
val isLive = nnScore < BASE_THRESHOLD && laplacianScore > LAPLACIAN_THRESHOLD
|
|
131
130
|
val combinedScore = calculateCombinedScore(nnScore, laplacianScore)
|
|
132
131
|
val confidence = calculateConfidence(nnScore, laplacianScore)
|
|
133
132
|
|
|
@@ -249,7 +248,6 @@ class FaceAntiSpoofingAdvanced(private val assetManager: AssetManager) {
|
|
|
249
248
|
val nnResult = neuralNetworkAntiSpoofingSampled(sampled.rgbBuffer)
|
|
250
249
|
val nnScore = nnResult.getOrNull() ?: throw IllegalStateException(nnResult.exceptionOrNull()?.message ?: "NN inference failed")
|
|
251
250
|
val laplacianScore = calculateLaplacianFromGray(sampled.grayBuffer)
|
|
252
|
-
val isLive = nnScore < BASE_THRESHOLD && laplacianScore > LAPLACIAN_THRESHOLD
|
|
253
251
|
val combinedScore = calculateCombinedScore(nnScore, laplacianScore)
|
|
254
252
|
val confidence = calculateConfidence(nnScore, laplacianScore)
|
|
255
253
|
|
package/android/src/main/java/com/margelo/nitro/camera/spoofdetector/HybridSpoofDetectorOutput.kt
CHANGED
|
@@ -56,7 +56,9 @@ class HybridSpoofDetectorOutput(
|
|
|
56
56
|
if (!enabled) return@let null
|
|
57
57
|
FaceDetection.getClient(
|
|
58
58
|
FaceDetectorOptions.Builder()
|
|
59
|
-
.setPerformanceMode(FaceDetectorOptions.
|
|
59
|
+
.setPerformanceMode(FaceDetectorOptions.PERFORMANCE_MODE_ACCURATE)
|
|
60
|
+
.setLandmarkMode(FaceDetectorOptions.LANDMARK_MODE_ALL)
|
|
61
|
+
.setMinFaceSize(MIN_FACE_WIDTH_RATIO)
|
|
60
62
|
.enableTracking()
|
|
61
63
|
.build()
|
|
62
64
|
)
|
|
@@ -78,13 +80,16 @@ class HybridSpoofDetectorOutput(
|
|
|
78
80
|
private var stableCount = 0
|
|
79
81
|
private var lastFaceBounds: SpoofDetectedFaceBounds? = null
|
|
80
82
|
private var consecutiveLiveFrames = 0
|
|
81
|
-
private var isLive = false
|
|
82
83
|
private var lastSpoofResult: AntiSpoofingResult? = null
|
|
83
84
|
private var lastFaceCount = -1
|
|
84
85
|
private var lastFacesUpdateAtMs = 0L
|
|
85
86
|
private var lastResultUpdateAtMs = 0L
|
|
86
87
|
private var imageAnalysis: ImageAnalysis? = null
|
|
87
88
|
private companion object {
|
|
89
|
+
private const val MIN_FACE_WIDTH_RATIO = 0.18f
|
|
90
|
+
private const val MIN_FACE_HEIGHT_RATIO = 0.18f
|
|
91
|
+
private const val FACE_FRAME_MARGIN_RATIO = 0.02f
|
|
92
|
+
private const val MAX_FACE_POSE_ANGLE_DEGREES = 25f
|
|
88
93
|
private const val MIN_FRAME_BRIGHTNESS = 24f
|
|
89
94
|
private const val MAX_FRAME_BRIGHTNESS = 220f
|
|
90
95
|
private const val MIN_VARIANCE_FOR_ANALYSIS = 1500f
|
|
@@ -150,11 +155,14 @@ class HybridSpoofDetectorOutput(
|
|
|
150
155
|
val mediaImage = image.image ?: throw IllegalStateException("ImageProxy has no media image")
|
|
151
156
|
val inputImage = InputImage.fromMediaImage(mediaImage, image.imageInfo.rotationDegrees)
|
|
152
157
|
val transform = createMLKitToCameraTransform(image)
|
|
158
|
+
val rotation = ((image.imageInfo.rotationDegrees % 360) + 360) % 360
|
|
159
|
+
val detectorFrameWidth = if (rotation == 90 || rotation == 270) image.height else image.width
|
|
160
|
+
val detectorFrameHeight = if (rotation == 90 || rotation == 270) image.width else image.height
|
|
153
161
|
val task = faceDetector.process(inputImage)
|
|
154
162
|
faceDetectorTask.set(task)
|
|
155
163
|
task.addOnSuccessListener { faces ->
|
|
156
164
|
try {
|
|
157
|
-
processDetectedFaces(faces, transform)
|
|
165
|
+
processDetectedFaces(faces, transform, detectorFrameWidth, detectorFrameHeight)
|
|
158
166
|
} catch (error: Throwable) {
|
|
159
167
|
options.onError?.invoke(error)
|
|
160
168
|
}
|
|
@@ -186,12 +194,8 @@ class HybridSpoofDetectorOutput(
|
|
|
186
194
|
pixelStrides,
|
|
187
195
|
image.imageInfo.rotationDegrees,
|
|
188
196
|
)
|
|
189
|
-
val isLive = result.neuralNetworkScore < FaceAntiSpoofingAdvanced.BASE_THRESHOLD &&
|
|
190
|
-
result.laplacianScore > FaceAntiSpoofingAdvanced.LAPLACIAN_THRESHOLD
|
|
191
197
|
processAntiSpoofResult(
|
|
192
198
|
AntiSpoofingResult(
|
|
193
|
-
isLive,
|
|
194
|
-
if (isLive) "Live Face" else "Spoof Face",
|
|
195
199
|
result.neuralNetworkScore.toDouble(),
|
|
196
200
|
result.laplacianScore.toDouble(),
|
|
197
201
|
result.combinedScore.toDouble(),
|
|
@@ -208,8 +212,16 @@ class HybridSpoofDetectorOutput(
|
|
|
208
212
|
}
|
|
209
213
|
}
|
|
210
214
|
|
|
211
|
-
private fun processDetectedFaces(
|
|
212
|
-
|
|
215
|
+
private fun processDetectedFaces(
|
|
216
|
+
faces: List<Face>,
|
|
217
|
+
transform: Matrix,
|
|
218
|
+
frameWidth: Int,
|
|
219
|
+
frameHeight: Int,
|
|
220
|
+
) {
|
|
221
|
+
val detectedFaces = faces
|
|
222
|
+
.filter { it.isFullFace(frameWidth, frameHeight) }
|
|
223
|
+
.map { it.toSpoofDetectedFace(transform) }
|
|
224
|
+
.toTypedArray()
|
|
213
225
|
val now = SystemClock.elapsedRealtime()
|
|
214
226
|
var shouldNotify = false
|
|
215
227
|
var capture: Pair<SpoofDetectedFaceBounds, AntiSpoofingResult>? = null
|
|
@@ -224,7 +236,6 @@ class HybridSpoofDetectorOutput(
|
|
|
224
236
|
stableCount = 0
|
|
225
237
|
lastFaceBounds = null
|
|
226
238
|
consecutiveLiveFrames = 0
|
|
227
|
-
isLive = false
|
|
228
239
|
lastSpoofResult = null
|
|
229
240
|
}
|
|
230
241
|
|
|
@@ -251,7 +262,6 @@ class HybridSpoofDetectorOutput(
|
|
|
251
262
|
val isNeuralSpoof = result.neuralNetworkScore >= maxNeuralSpoofScore
|
|
252
263
|
if (isNeuralSpoof) {
|
|
253
264
|
consecutiveLiveFrames = 0
|
|
254
|
-
isLive = false
|
|
255
265
|
} else {
|
|
256
266
|
val targetPercent = (antispoofLevel * 100).roundToInt()
|
|
257
267
|
val currentPercent = (result.confidence * 100).roundToInt()
|
|
@@ -262,13 +272,12 @@ class HybridSpoofDetectorOutput(
|
|
|
262
272
|
result.combinedScore >= antispoofLevel
|
|
263
273
|
val meetsLaplacian = result.laplacianScore == 0.0 ||
|
|
264
274
|
result.laplacianScore >= laplacianThreshold
|
|
265
|
-
val
|
|
266
|
-
consecutiveLiveFrames = if (
|
|
275
|
+
val framePassesLiveness = meetsConfidence && meetsLaplacian
|
|
276
|
+
consecutiveLiveFrames = if (framePassesLiveness) {
|
|
267
277
|
min(requiredLiveFrames, consecutiveLiveFrames + 1)
|
|
268
278
|
} else {
|
|
269
279
|
max(0, consecutiveLiveFrames - 1)
|
|
270
280
|
}
|
|
271
|
-
isLive = consecutiveLiveFrames >= requiredLiveFrames
|
|
272
281
|
}
|
|
273
282
|
|
|
274
283
|
shouldNotify = now - lastResultUpdateAtMs >= uiUpdateIntervalMs
|
|
@@ -283,7 +292,7 @@ class HybridSpoofDetectorOutput(
|
|
|
283
292
|
}
|
|
284
293
|
|
|
285
294
|
private fun readyToCaptureLocked(): Pair<SpoofDetectedFaceBounds, AntiSpoofingResult>? {
|
|
286
|
-
if (captured || stableCount < stableFrames ||
|
|
295
|
+
if (captured || stableCount < stableFrames ||
|
|
287
296
|
consecutiveLiveFrames < requiredLiveFrames
|
|
288
297
|
) return null
|
|
289
298
|
val bounds = lastFaceBounds ?: return null
|
|
@@ -299,7 +308,6 @@ class HybridSpoofDetectorOutput(
|
|
|
299
308
|
stableCount = 0
|
|
300
309
|
lastFaceBounds = null
|
|
301
310
|
consecutiveLiveFrames = 0
|
|
302
|
-
isLive = false
|
|
303
311
|
lastSpoofResult = null
|
|
304
312
|
lastFaceCount = -1
|
|
305
313
|
lastFacesUpdateAtMs = 0L
|
|
@@ -388,6 +396,36 @@ class HybridSpoofDetectorOutput(
|
|
|
388
396
|
)
|
|
389
397
|
}
|
|
390
398
|
|
|
399
|
+
private fun Face.isFullFace(frameWidth: Int, frameHeight: Int): Boolean {
|
|
400
|
+
if (frameWidth <= 0 || frameHeight <= 0) return false
|
|
401
|
+
|
|
402
|
+
val bounds = boundingBox
|
|
403
|
+
val marginX = frameWidth * FACE_FRAME_MARGIN_RATIO
|
|
404
|
+
val marginY = frameHeight * FACE_FRAME_MARGIN_RATIO
|
|
405
|
+
if (bounds.left < marginX || bounds.top < marginY ||
|
|
406
|
+
bounds.right > frameWidth - marginX || bounds.bottom > frameHeight - marginY
|
|
407
|
+
) return false
|
|
408
|
+
if (bounds.width() < frameWidth * MIN_FACE_WIDTH_RATIO ||
|
|
409
|
+
bounds.height() < frameHeight * MIN_FACE_HEIGHT_RATIO
|
|
410
|
+
) return false
|
|
411
|
+
if (abs(headEulerAngleY) > MAX_FACE_POSE_ANGLE_DEGREES ||
|
|
412
|
+
abs(headEulerAngleX) > MAX_FACE_POSE_ANGLE_DEGREES
|
|
413
|
+
) return false
|
|
414
|
+
|
|
415
|
+
val requiredLandmarks = intArrayOf(
|
|
416
|
+
com.google.mlkit.vision.face.FaceLandmark.LEFT_EYE,
|
|
417
|
+
com.google.mlkit.vision.face.FaceLandmark.RIGHT_EYE,
|
|
418
|
+
com.google.mlkit.vision.face.FaceLandmark.NOSE_BASE,
|
|
419
|
+
com.google.mlkit.vision.face.FaceLandmark.MOUTH_LEFT,
|
|
420
|
+
com.google.mlkit.vision.face.FaceLandmark.MOUTH_RIGHT,
|
|
421
|
+
)
|
|
422
|
+
return requiredLandmarks.all { landmarkType ->
|
|
423
|
+
val point = getLandmark(landmarkType)?.position ?: return@all false
|
|
424
|
+
point.x >= bounds.left && point.x <= bounds.right &&
|
|
425
|
+
point.y >= bounds.top && point.y <= bounds.bottom
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
391
429
|
override fun dispose() {
|
|
392
430
|
disposed.set(true)
|
|
393
431
|
imageAnalysis?.clearAnalyzer()
|
package/index.js
CHANGED
|
@@ -3,76 +3,76 @@ import { NativeModules } from 'react-native';
|
|
|
3
3
|
import { NitroModules } from 'react-native-nitro-modules';
|
|
4
4
|
|
|
5
5
|
const { FaceAntiSpoof } = NativeModules;
|
|
6
|
-
let sharedSpoofDetectorFactory;
|
|
6
|
+
let sharedSpoofDetectorFactory;
|
|
7
7
|
|
|
8
|
-
const getSpoofDetectorFactory = () => {
|
|
9
|
-
if (!sharedSpoofDetectorFactory) {
|
|
10
|
-
sharedSpoofDetectorFactory = NitroModules.createHybridObject('SpoofDetectorFactory');
|
|
11
|
-
}
|
|
12
|
-
return sharedSpoofDetectorFactory;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export const useFaceAntiSpoofFrameOutput = ({
|
|
16
|
-
onResult,
|
|
17
|
-
onFacesDetected,
|
|
18
|
-
onStableFaceDetected,
|
|
19
|
-
...options
|
|
20
|
-
} = {}) => {
|
|
8
|
+
const getSpoofDetectorFactory = () => {
|
|
9
|
+
if (!sharedSpoofDetectorFactory) {
|
|
10
|
+
sharedSpoofDetectorFactory = NitroModules.createHybridObject('SpoofDetectorFactory');
|
|
11
|
+
}
|
|
12
|
+
return sharedSpoofDetectorFactory;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const useFaceAntiSpoofFrameOutput = ({
|
|
16
|
+
onResult,
|
|
17
|
+
onFacesDetected,
|
|
18
|
+
onStableFaceDetected,
|
|
19
|
+
...options
|
|
20
|
+
} = {}) => {
|
|
21
21
|
const resultRef = useRef(onResult);
|
|
22
22
|
resultRef.current = onResult;
|
|
23
|
-
const facesRef = useRef(onFacesDetected);
|
|
24
|
-
facesRef.current = onFacesDetected;
|
|
25
|
-
const stableFaceRef = useRef(onStableFaceDetected);
|
|
26
|
-
stableFaceRef.current = onStableFaceDetected;
|
|
23
|
+
const facesRef = useRef(onFacesDetected);
|
|
24
|
+
facesRef.current = onFacesDetected;
|
|
25
|
+
const stableFaceRef = useRef(onStableFaceDetected);
|
|
26
|
+
stableFaceRef.current = onStableFaceDetected;
|
|
27
27
|
const errorRef = useRef(options.onError);
|
|
28
28
|
errorRef.current = options.onError;
|
|
29
|
-
const hasFaceCallback = typeof onFacesDetected === 'function';
|
|
30
|
-
const hasStableFaceCallback = typeof onStableFaceDetected === 'function';
|
|
29
|
+
const hasFaceCallback = typeof onFacesDetected === 'function';
|
|
30
|
+
const hasStableFaceCallback = typeof onStableFaceDetected === 'function';
|
|
31
31
|
const factory = useMemo(
|
|
32
|
-
getSpoofDetectorFactory,
|
|
32
|
+
getSpoofDetectorFactory,
|
|
33
33
|
[],
|
|
34
34
|
);
|
|
35
35
|
return useMemo(
|
|
36
36
|
() => factory.createSpoofDetectorOutput({
|
|
37
|
-
targetResolution: options.targetResolution,
|
|
38
|
-
antispoofLevel: options.antispoofLevel,
|
|
39
|
-
stableFrames: options.stableFrames,
|
|
40
|
-
requiredLiveFrames: options.requiredLiveFrames,
|
|
41
|
-
laplacianThreshold: options.laplacianThreshold,
|
|
42
|
-
maxNeuralSpoofScore: options.maxNeuralSpoofScore,
|
|
43
|
-
uiUpdateIntervalMs: options.uiUpdateIntervalMs,
|
|
37
|
+
targetResolution: options.targetResolution,
|
|
38
|
+
antispoofLevel: options.antispoofLevel,
|
|
39
|
+
stableFrames: options.stableFrames,
|
|
40
|
+
requiredLiveFrames: options.requiredLiveFrames,
|
|
41
|
+
laplacianThreshold: options.laplacianThreshold,
|
|
42
|
+
maxNeuralSpoofScore: options.maxNeuralSpoofScore,
|
|
43
|
+
uiUpdateIntervalMs: options.uiUpdateIntervalMs,
|
|
44
44
|
onResult(result) {
|
|
45
45
|
resultRef.current?.(result);
|
|
46
46
|
},
|
|
47
|
-
onFacesDetected: hasFaceCallback
|
|
48
|
-
? (faces) => facesRef.current?.(faces)
|
|
49
|
-
: undefined,
|
|
50
|
-
onStableFaceDetected: hasStableFaceCallback
|
|
51
|
-
? (face, result) => stableFaceRef.current?.(face, result)
|
|
52
|
-
: undefined,
|
|
47
|
+
onFacesDetected: hasFaceCallback
|
|
48
|
+
? (faces) => facesRef.current?.(faces)
|
|
49
|
+
: undefined,
|
|
50
|
+
onStableFaceDetected: hasStableFaceCallback
|
|
51
|
+
? (face, result) => stableFaceRef.current?.(face, result)
|
|
52
|
+
: undefined,
|
|
53
53
|
onError(error) {
|
|
54
54
|
errorRef.current?.(error);
|
|
55
55
|
},
|
|
56
56
|
}),
|
|
57
|
-
[
|
|
58
|
-
factory,
|
|
59
|
-
options.targetResolution,
|
|
60
|
-
options.antispoofLevel,
|
|
61
|
-
options.stableFrames,
|
|
62
|
-
options.requiredLiveFrames,
|
|
63
|
-
options.laplacianThreshold,
|
|
64
|
-
options.maxNeuralSpoofScore,
|
|
65
|
-
options.uiUpdateIntervalMs,
|
|
66
|
-
hasFaceCallback,
|
|
67
|
-
hasStableFaceCallback,
|
|
68
|
-
],
|
|
57
|
+
[
|
|
58
|
+
factory,
|
|
59
|
+
options.targetResolution,
|
|
60
|
+
options.antispoofLevel,
|
|
61
|
+
options.stableFrames,
|
|
62
|
+
options.requiredLiveFrames,
|
|
63
|
+
options.laplacianThreshold,
|
|
64
|
+
options.maxNeuralSpoofScore,
|
|
65
|
+
options.uiUpdateIntervalMs,
|
|
66
|
+
hasFaceCallback,
|
|
67
|
+
hasStableFaceCallback,
|
|
68
|
+
],
|
|
69
69
|
);
|
|
70
70
|
};
|
|
71
71
|
|
|
72
|
-
export const resetFaceAntiSpoofFrameOutput = (output) => {
|
|
73
|
-
getSpoofDetectorFactory().resetSpoofDetectorOutput(output);
|
|
74
|
-
};
|
|
75
|
-
|
|
72
|
+
export const resetFaceAntiSpoofFrameOutput = (output) => {
|
|
73
|
+
getSpoofDetectorFactory().resetSpoofDetectorOutput(output);
|
|
74
|
+
};
|
|
75
|
+
|
|
76
76
|
export default FaceAntiSpoof;
|
|
77
77
|
|
|
78
78
|
// Utility functions
|
|
@@ -10,8 +10,6 @@
|
|
|
10
10
|
#include <fbjni/fbjni.h>
|
|
11
11
|
#include "AntiSpoofingResult.hpp"
|
|
12
12
|
|
|
13
|
-
#include <string>
|
|
14
|
-
|
|
15
13
|
namespace margelo::nitro::camera::spoofdetector {
|
|
16
14
|
|
|
17
15
|
using namespace facebook;
|
|
@@ -31,10 +29,6 @@ namespace margelo::nitro::camera::spoofdetector {
|
|
|
31
29
|
[[nodiscard]]
|
|
32
30
|
AntiSpoofingResult toCpp() const {
|
|
33
31
|
static const auto clazz = javaClassStatic();
|
|
34
|
-
static const auto fieldIsLive = clazz->getField<jboolean>("isLive");
|
|
35
|
-
jboolean isLive = this->getFieldValue(fieldIsLive);
|
|
36
|
-
static const auto fieldLabel = clazz->getField<jni::JString>("label");
|
|
37
|
-
jni::local_ref<jni::JString> label = this->getFieldValue(fieldLabel);
|
|
38
32
|
static const auto fieldNeuralNetworkScore = clazz->getField<double>("neuralNetworkScore");
|
|
39
33
|
double neuralNetworkScore = this->getFieldValue(fieldNeuralNetworkScore);
|
|
40
34
|
static const auto fieldLaplacianScore = clazz->getField<double>("laplacianScore");
|
|
@@ -43,14 +37,12 @@ namespace margelo::nitro::camera::spoofdetector {
|
|
|
43
37
|
double combinedScore = this->getFieldValue(fieldCombinedScore);
|
|
44
38
|
static const auto fieldConfidence = clazz->getField<double>("confidence");
|
|
45
39
|
double confidence = this->getFieldValue(fieldConfidence);
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
confidence
|
|
53
|
-
);
|
|
40
|
+
AntiSpoofingResult result{};
|
|
41
|
+
result.neuralNetworkScore = neuralNetworkScore;
|
|
42
|
+
result.laplacianScore = laplacianScore;
|
|
43
|
+
result.combinedScore = combinedScore;
|
|
44
|
+
result.confidence = confidence;
|
|
45
|
+
return result;
|
|
54
46
|
}
|
|
55
47
|
|
|
56
48
|
public:
|
|
@@ -59,13 +51,11 @@ namespace margelo::nitro::camera::spoofdetector {
|
|
|
59
51
|
*/
|
|
60
52
|
[[maybe_unused]]
|
|
61
53
|
static jni::local_ref<JAntiSpoofingResult::javaobject> fromCpp(const AntiSpoofingResult& value) {
|
|
62
|
-
using JSignature = JAntiSpoofingResult(
|
|
54
|
+
using JSignature = JAntiSpoofingResult(double, double, double, double);
|
|
63
55
|
static const auto clazz = javaClassStatic();
|
|
64
56
|
static const auto create = clazz->getStaticMethod<JSignature>("fromCpp");
|
|
65
57
|
return create(
|
|
66
58
|
clazz,
|
|
67
|
-
value.isLive,
|
|
68
|
-
jni::make_jstring(value.label),
|
|
69
59
|
value.neuralNetworkScore,
|
|
70
60
|
value.laplacianScore,
|
|
71
61
|
value.combinedScore,
|
|
@@ -18,12 +18,6 @@ import java.util.Objects
|
|
|
18
18
|
@DoNotStrip
|
|
19
19
|
@Keep
|
|
20
20
|
data class AntiSpoofingResult(
|
|
21
|
-
@DoNotStrip
|
|
22
|
-
@Keep
|
|
23
|
-
val isLive: Boolean,
|
|
24
|
-
@DoNotStrip
|
|
25
|
-
@Keep
|
|
26
|
-
val label: String,
|
|
27
21
|
@DoNotStrip
|
|
28
22
|
@Keep
|
|
29
23
|
val neuralNetworkScore: Double,
|
|
@@ -42,9 +36,7 @@ data class AntiSpoofingResult(
|
|
|
42
36
|
override fun equals(other: Any?): Boolean {
|
|
43
37
|
if (this === other) return true
|
|
44
38
|
if (other !is AntiSpoofingResult) return false
|
|
45
|
-
return Objects.deepEquals(this.
|
|
46
|
-
&& Objects.deepEquals(this.label, other.label)
|
|
47
|
-
&& Objects.deepEquals(this.neuralNetworkScore, other.neuralNetworkScore)
|
|
39
|
+
return Objects.deepEquals(this.neuralNetworkScore, other.neuralNetworkScore)
|
|
48
40
|
&& Objects.deepEquals(this.laplacianScore, other.laplacianScore)
|
|
49
41
|
&& Objects.deepEquals(this.combinedScore, other.combinedScore)
|
|
50
42
|
&& Objects.deepEquals(this.confidence, other.confidence)
|
|
@@ -52,8 +44,6 @@ data class AntiSpoofingResult(
|
|
|
52
44
|
|
|
53
45
|
override fun hashCode(): Int {
|
|
54
46
|
return arrayOf<Any?>(
|
|
55
|
-
isLive,
|
|
56
|
-
label,
|
|
57
47
|
neuralNetworkScore,
|
|
58
48
|
laplacianScore,
|
|
59
49
|
combinedScore,
|
|
@@ -69,8 +59,8 @@ data class AntiSpoofingResult(
|
|
|
69
59
|
@Keep
|
|
70
60
|
@Suppress("unused")
|
|
71
61
|
@JvmStatic
|
|
72
|
-
private fun fromCpp(
|
|
73
|
-
return AntiSpoofingResult(
|
|
62
|
+
private fun fromCpp(neuralNetworkScore: Double, laplacianScore: Double, combinedScore: Double, confidence: Double): AntiSpoofingResult {
|
|
63
|
+
return AntiSpoofingResult(neuralNetworkScore, laplacianScore, combinedScore, confidence)
|
|
74
64
|
}
|
|
75
65
|
}
|
|
76
66
|
}
|
|
@@ -74,8 +74,10 @@ namespace margelo::nitro {
|
|
|
74
74
|
}
|
|
75
75
|
static inline jsi::Value toJSI(jsi::Runtime& runtime, const margelo::nitro::camera::spoofdetector::AntiSpoofingResult& arg) {
|
|
76
76
|
jsi::Object obj(runtime);
|
|
77
|
+
#if !defined(__ANDROID__)
|
|
77
78
|
obj.setProperty(runtime, PropNameIDCache::get(runtime, "isLive"), JSIConverter<bool>::toJSI(runtime, arg.isLive));
|
|
78
79
|
obj.setProperty(runtime, PropNameIDCache::get(runtime, "label"), JSIConverter<std::string>::toJSI(runtime, arg.label));
|
|
80
|
+
#endif
|
|
79
81
|
obj.setProperty(runtime, PropNameIDCache::get(runtime, "neuralNetworkScore"), JSIConverter<double>::toJSI(runtime, arg.neuralNetworkScore));
|
|
80
82
|
obj.setProperty(runtime, PropNameIDCache::get(runtime, "laplacianScore"), JSIConverter<double>::toJSI(runtime, arg.laplacianScore));
|
|
81
83
|
obj.setProperty(runtime, PropNameIDCache::get(runtime, "combinedScore"), JSIConverter<double>::toJSI(runtime, arg.combinedScore));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-vision-camera-spoof-detector",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.7",
|
|
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": {
|