react-native-face-match-sdk 0.0.0-stage → 1.0.1
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/FaceMatch.podspec +55 -0
- package/README.md +174 -2
- package/android/build.gradle +59 -0
- package/android/consumer-rules.pro +5 -0
- package/android/src/main/AndroidManifest.xml +11 -0
- package/android/src/main/java/com/facematch/sdk/AntiSpoofHelper.kt +216 -0
- package/android/src/main/java/com/facematch/sdk/BitmapUtils.kt +138 -0
- package/android/src/main/java/com/facematch/sdk/CameraFrameView.kt +75 -0
- package/android/src/main/java/com/facematch/sdk/FaceAlignmentHelper.kt +156 -0
- package/android/src/main/java/com/facematch/sdk/FaceCameraView.kt +628 -0
- package/android/src/main/java/com/facematch/sdk/FaceCameraViewManager.kt +79 -0
- package/android/src/main/java/com/facematch/sdk/FaceCandidateFilter.kt +65 -0
- package/android/src/main/java/com/facematch/sdk/FaceDetectionHelper.kt +225 -0
- package/android/src/main/java/com/facematch/sdk/FaceEmbeddingHelper.kt +242 -0
- package/android/src/main/java/com/facematch/sdk/FaceFrameAnalyzer.kt +228 -0
- package/android/src/main/java/com/facematch/sdk/FaceMatchModule.kt +249 -0
- package/android/src/main/java/com/facematch/sdk/FaceMatchPackage.kt +14 -0
- package/android/src/main/java/com/facematch/sdk/FaceMatchRuntime.kt +138 -0
- package/android/src/main/java/com/facematch/sdk/FaceMatcherHelper.kt +92 -0
- package/android/src/main/java/com/facematch/sdk/FaceOverlayView.kt +236 -0
- package/android/src/main/java/com/facematch/sdk/LivenessDetector.kt +483 -0
- package/android/src/main/java/com/facematch/sdk/LivenessPrompts.kt +137 -0
- package/android/src/main/java/com/facematch/sdk/LivenessVoice.kt +270 -0
- package/android/src/main/java/com/facematch/sdk/PrivateCaptureStore.kt +50 -0
- package/ios/Sources/AntiSpoofHelper.swift +239 -0
- package/ios/Sources/FaceAlignmentHelper.swift +169 -0
- package/ios/Sources/FaceCameraView.swift +1161 -0
- package/ios/Sources/FaceCameraViewManager.m +28 -0
- package/ios/Sources/FaceCameraViewManager.swift +57 -0
- package/ios/Sources/FaceCandidateFilter.swift +62 -0
- package/ios/Sources/FaceDetectionHelper.swift +283 -0
- package/ios/Sources/FaceEmbeddingHelper.swift +188 -0
- package/ios/Sources/FaceFrameAnalyzer.swift +179 -0
- package/ios/Sources/FaceFrameResult.swift +96 -0
- package/ios/Sources/FaceMatchBundle.swift +26 -0
- package/ios/Sources/FaceMatchEngine.swift +136 -0
- package/ios/Sources/FaceMatchError.swift +40 -0
- package/ios/Sources/FaceMatchModule.m +55 -0
- package/ios/Sources/FaceMatchModule.swift +288 -0
- package/ios/Sources/FaceMatcherHelper.swift +107 -0
- package/ios/Sources/FaceOverlayView.swift +285 -0
- package/ios/Sources/ImageUtils.swift +322 -0
- package/ios/Sources/LivenessDetector.swift +505 -0
- package/ios/Sources/LivenessPrompts.swift +121 -0
- package/ios/Sources/LivenessVoice.swift +229 -0
- package/ios/Sources/OnnxRuntime/ORTShim.h +91 -0
- package/ios/Sources/OnnxRuntime/ORTShim.mm +260 -0
- package/ios/Sources/PrivateCaptureStore.swift +71 -0
- package/ios/Sources/VisionFaceFallback.swift +95 -0
- package/ios/onnxruntime-c.podspec +47 -0
- package/models/anti_spoof_minifasnet_v2.onnx +0 -0
- package/models/w600k_mbf.onnx +0 -0
- package/package.json +38 -4
- package/react-native.config.js +16 -0
- package/src/FaceCameraView.tsx +75 -0
- package/src/FaceMatch.ts +66 -0
- package/src/index.ts +4 -0
- package/src/types.ts +87 -0
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
package com.facematch.sdk
|
|
2
|
+
|
|
3
|
+
import kotlin.math.abs
|
|
4
|
+
|
|
5
|
+
enum class LivenessAction(val wireName: String, val prompt: LivenessPrompt) {
|
|
6
|
+
BLINK("blink", LivenessPrompt.BLINK),
|
|
7
|
+
SMILE("smile", LivenessPrompt.SMILE),
|
|
8
|
+
LEFT("left", LivenessPrompt.TURN_LEFT),
|
|
9
|
+
RIGHT("right", LivenessPrompt.TURN_RIGHT),
|
|
10
|
+
UP("up", LivenessPrompt.LOOK_UP),
|
|
11
|
+
NEAR("near", LivenessPrompt.COME_CLOSER),
|
|
12
|
+
FAR("far", LivenessPrompt.GO_BACK),
|
|
13
|
+
HOLD("hold", LivenessPrompt.HOLD_STILL)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
data class LivenessProgress(
|
|
17
|
+
val currentAction: LivenessAction?,
|
|
18
|
+
val completedActions: List<LivenessAction>,
|
|
19
|
+
val isComplete: Boolean,
|
|
20
|
+
val promptKey: LivenessPrompt,
|
|
21
|
+
val cueAction: LivenessAction? = null,
|
|
22
|
+
/** Confidence toward completing the current action, 0..1 (overlay meter). */
|
|
23
|
+
val actionProgress: Float = 0f,
|
|
24
|
+
val stepIndex: Int = 0,
|
|
25
|
+
val stepCount: Int = 0
|
|
26
|
+
) {
|
|
27
|
+
val instruction: String get() = promptKey.en
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Deterministic active-liveness state machine.
|
|
32
|
+
*
|
|
33
|
+
* Every session runs exactly three steps: a blink (which a photo cannot do) and
|
|
34
|
+
* one movement challenge in a randomised order, then a short neutral hold. Only
|
|
35
|
+
* ONE movement is ever drawn, so a session never asks the user to turn left and
|
|
36
|
+
* then right — the QA complaint this shape was built to fix.
|
|
37
|
+
*
|
|
38
|
+
* Because the challenge set is short, the anti-photo guarantee rests on the
|
|
39
|
+
* blink's open -> closed -> open transition rather than on step count, and the
|
|
40
|
+
* anti-replay guarantee rests on the passive MiniFASNetV2 model: it runs on
|
|
41
|
+
* every analysed frame, [passiveConfidenceOk] additionally requires most of the
|
|
42
|
+
* recent window to have scored REAL before liveness may complete, and
|
|
43
|
+
* `extractLive()` re-checks the captured still fail-closed. The user must
|
|
44
|
+
* return to a neutral face between steps, and per-step and total timeouts reset
|
|
45
|
+
* the flow.
|
|
46
|
+
*/
|
|
47
|
+
class LivenessDetector {
|
|
48
|
+
private var actions = randomChallenge()
|
|
49
|
+
private var currentIndex = 0
|
|
50
|
+
private var needsNeutral = true
|
|
51
|
+
private var stableFrames = 0
|
|
52
|
+
private var neutralFrames = 0
|
|
53
|
+
private var blinkClosedSeen = false
|
|
54
|
+
private var baselineFill = 0f
|
|
55
|
+
private var actionProgressValue = 0f
|
|
56
|
+
private var stepDeadlineMs = 0L
|
|
57
|
+
private var challengeStartedMs = 0L
|
|
58
|
+
private var outOfFrameFrames = 0
|
|
59
|
+
private var spoofFrames = 0
|
|
60
|
+
/** Rolling window of recent passive anti-spoof verdicts, newest last. */
|
|
61
|
+
private val passiveWindow = ArrayDeque<Boolean>()
|
|
62
|
+
|
|
63
|
+
fun reset(requested: List<String>? = null): LivenessProgress {
|
|
64
|
+
actions = requested
|
|
65
|
+
?.mapNotNull { name -> LivenessAction.entries.firstOrNull { it.wireName == name.lowercase() } }
|
|
66
|
+
?.distinct()
|
|
67
|
+
?.takeIf { it.isNotEmpty() }
|
|
68
|
+
?.let(::withTerminalHold)
|
|
69
|
+
?: randomChallenge()
|
|
70
|
+
currentIndex = 0
|
|
71
|
+
needsNeutral = true
|
|
72
|
+
stableFrames = 0
|
|
73
|
+
neutralFrames = 0
|
|
74
|
+
blinkClosedSeen = false
|
|
75
|
+
baselineFill = 0f
|
|
76
|
+
actionProgressValue = 0f
|
|
77
|
+
stepDeadlineMs = 0L
|
|
78
|
+
outOfFrameFrames = 0
|
|
79
|
+
spoofFrames = 0
|
|
80
|
+
passiveWindow.clear()
|
|
81
|
+
challengeStartedMs = nowMs()
|
|
82
|
+
return progress(LivenessPrompt.GET_READY)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
fun update(frame: FaceFrameResult): LivenessProgress {
|
|
86
|
+
if (!frame.hasFace) {
|
|
87
|
+
pauseCurrentSignal()
|
|
88
|
+
return progress(LivenessPrompt.POSITION_FACE)
|
|
89
|
+
}
|
|
90
|
+
if (frame.hasMultipleFaces) {
|
|
91
|
+
resetAllProgress()
|
|
92
|
+
return progress(LivenessPrompt.MULTIPLE_FACES)
|
|
93
|
+
}
|
|
94
|
+
when (frame.antiSpoof?.status) {
|
|
95
|
+
AntiSpoofStatus.REAL -> {
|
|
96
|
+
spoofFrames = 0
|
|
97
|
+
recordPassive(true)
|
|
98
|
+
}
|
|
99
|
+
// Motion and lighting make individual frame scores fluctuate, and a
|
|
100
|
+
// SPOOF frame never advances a step either way. Only a sustained run
|
|
101
|
+
// of them discards the progress already earned, so one noisy score no
|
|
102
|
+
// longer costs the user the whole step.
|
|
103
|
+
AntiSpoofStatus.SPOOF -> {
|
|
104
|
+
recordPassive(false)
|
|
105
|
+
spoofFrames++
|
|
106
|
+
if (spoofFrames >= SPOOF_GRACE_FRAMES) pauseCurrentSignal()
|
|
107
|
+
return progress(LivenessPrompt.SPOOF_BLOCKED)
|
|
108
|
+
}
|
|
109
|
+
AntiSpoofStatus.ERROR -> {
|
|
110
|
+
recordPassive(false)
|
|
111
|
+
resetAllProgress()
|
|
112
|
+
return progress(LivenessPrompt.SPOOF_BLOCKED)
|
|
113
|
+
}
|
|
114
|
+
null -> {
|
|
115
|
+
pauseCurrentSignal()
|
|
116
|
+
return progress(LivenessPrompt.VERIFY_REAL)
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (currentIndex >= actions.size) {
|
|
120
|
+
return progress(LivenessPrompt.VERIFYING)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
val action = actions[currentIndex]
|
|
124
|
+
val performing = !needsNeutral
|
|
125
|
+
|
|
126
|
+
// Strict framing gate before each step; a looser one while a move is in
|
|
127
|
+
// progress so a deliberate head turn / lean is not rejected mid-action.
|
|
128
|
+
if (!performing && frame.luminance < MIN_LUMINANCE) {
|
|
129
|
+
pauseCurrentSignal()
|
|
130
|
+
return progress(LivenessPrompt.LOW_LIGHT)
|
|
131
|
+
}
|
|
132
|
+
if (!performing && !frame.isPositioned) {
|
|
133
|
+
pauseCurrentSignal()
|
|
134
|
+
return progress(positionPrompt(frame))
|
|
135
|
+
}
|
|
136
|
+
// A deliberate head turn briefly pushes the face box past the loose
|
|
137
|
+
// bounds. Abandoning the step on the first such frame made the prompt
|
|
138
|
+
// oscillate TURN_LEFT -> KEEP_IN_FRAME -> LOOK_STRAIGHT several times a
|
|
139
|
+
// second, and every cycle re-imposed the neutral gate on top of the turn
|
|
140
|
+
// itself. A short grace window keeps the step alive through the turn.
|
|
141
|
+
if (performing && !frame.looselyInFrame) {
|
|
142
|
+
outOfFrameFrames++
|
|
143
|
+
if (outOfFrameFrames >= OUT_OF_FRAME_GRACE_FRAMES) {
|
|
144
|
+
pauseCurrentSignal()
|
|
145
|
+
needsNeutral = true
|
|
146
|
+
outOfFrameFrames = 0
|
|
147
|
+
return progress(LivenessPrompt.KEEP_IN_FRAME)
|
|
148
|
+
}
|
|
149
|
+
} else {
|
|
150
|
+
outOfFrameFrames = 0
|
|
151
|
+
}
|
|
152
|
+
if (abs(frame.roll) > 26f) {
|
|
153
|
+
pauseCurrentSignal()
|
|
154
|
+
return progress(LivenessPrompt.HOLD_UPRIGHT)
|
|
155
|
+
}
|
|
156
|
+
if (nowMs() - challengeStartedMs > CHALLENGE_TIMEOUT_MS) {
|
|
157
|
+
resetAllProgress()
|
|
158
|
+
return progress(LivenessPrompt.TOO_SLOW)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
val leftEye = frame.leftEyeOpenProbability
|
|
162
|
+
val rightEye = frame.rightEyeOpenProbability
|
|
163
|
+
val smile = frame.smilingProbability ?: 0f
|
|
164
|
+
val eyesOpen = (leftEye ?: 1f) >= EYE_OPEN_THRESHOLD &&
|
|
165
|
+
(rightEye ?: 1f) >= EYE_OPEN_THRESHOLD
|
|
166
|
+
val eyesClosed = leftEye != null && rightEye != null &&
|
|
167
|
+
leftEye <= EYE_CLOSED_MAXIMUM && rightEye <= EYE_CLOSED_MAXIMUM &&
|
|
168
|
+
(leftEye + rightEye) / 2f <= EYE_CLOSED_AVERAGE
|
|
169
|
+
val poseNeutral = abs(frame.yaw) < NEUTRAL_DEGREES &&
|
|
170
|
+
abs(frame.pitch) < NEUTRAL_DEGREES
|
|
171
|
+
val neutral = poseNeutral && eyesOpen && smile < SMILE_NEUTRAL_MAX
|
|
172
|
+
// Only the terminal hold — the frame auto-capture actually photographs —
|
|
173
|
+
// demands a non-smiling face. A relaxed wide mouth must not be able to
|
|
174
|
+
// stall every inter-step gate, which is what the old single 0.35 bound did.
|
|
175
|
+
val captureNeutral = neutral && smile < SMILE_CAPTURE_MAX
|
|
176
|
+
|
|
177
|
+
if (needsNeutral) {
|
|
178
|
+
actionProgressValue = 0f
|
|
179
|
+
neutralFrames = if (neutral) neutralFrames + 1 else 0
|
|
180
|
+
if (neutralFrames >= REQUIRED_NEUTRAL_FRAMES) {
|
|
181
|
+
needsNeutral = false
|
|
182
|
+
neutralFrames = 0
|
|
183
|
+
blinkClosedSeen = false
|
|
184
|
+
stableFrames = 0
|
|
185
|
+
baselineFill = frame.faceFill
|
|
186
|
+
stepDeadlineMs = nowMs() + STEP_TIMEOUT_MS
|
|
187
|
+
return progress(action.prompt, action, 0f)
|
|
188
|
+
}
|
|
189
|
+
return progress(LivenessPrompt.LOOK_STRAIGHT)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (nowMs() > stepDeadlineMs) {
|
|
193
|
+
pauseCurrentSignal()
|
|
194
|
+
needsNeutral = true
|
|
195
|
+
return progress(LivenessPrompt.RETRY_STEP)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return when (action) {
|
|
199
|
+
LivenessAction.BLINK -> updateBlink(eyesOpen, eyesClosed, poseNeutral)
|
|
200
|
+
LivenessAction.SMILE -> updateSmile(smile)
|
|
201
|
+
LivenessAction.HOLD -> updateHold(captureNeutral)
|
|
202
|
+
LivenessAction.NEAR -> updateDistance(frame, closer = true)
|
|
203
|
+
LivenessAction.FAR -> updateDistance(frame, closer = false)
|
|
204
|
+
else -> updateHeadPose(frame, action)
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
fun isComplete(): Boolean = currentIndex >= actions.size
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* [poseNeutral] deliberately excludes the smile term: the blink is finished
|
|
212
|
+
* the moment the eyes reopen on a straight head, and a lingering grin from a
|
|
213
|
+
* previous step must not hold a completed blink open.
|
|
214
|
+
*/
|
|
215
|
+
private fun updateBlink(
|
|
216
|
+
eyesOpen: Boolean,
|
|
217
|
+
eyesClosed: Boolean,
|
|
218
|
+
poseNeutral: Boolean
|
|
219
|
+
): LivenessProgress {
|
|
220
|
+
if (!blinkClosedSeen) {
|
|
221
|
+
actionProgressValue = 0.12f
|
|
222
|
+
if (eyesClosed) {
|
|
223
|
+
blinkClosedSeen = true
|
|
224
|
+
stableFrames = 0
|
|
225
|
+
actionProgressValue = 0.6f
|
|
226
|
+
return progress(LivenessPrompt.OPEN_EYES, LivenessAction.BLINK, actionProgressValue)
|
|
227
|
+
}
|
|
228
|
+
return progress(LivenessPrompt.BLINK, LivenessAction.BLINK, actionProgressValue)
|
|
229
|
+
}
|
|
230
|
+
stableFrames = if (eyesOpen && poseNeutral) stableFrames + 1 else 0
|
|
231
|
+
actionProgressValue =
|
|
232
|
+
(0.6f + 0.4f * stableFrames / FINAL_STABLE_FRAMES).coerceIn(0f, 1f)
|
|
233
|
+
if (stableFrames >= FINAL_STABLE_FRAMES) return completeCurrentAction()
|
|
234
|
+
return progress(
|
|
235
|
+
if (eyesOpen) LivenessPrompt.HOLD_STILL else LivenessPrompt.OPEN_EYES,
|
|
236
|
+
LivenessAction.BLINK,
|
|
237
|
+
actionProgressValue
|
|
238
|
+
)
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
private fun updateSmile(smile: Float): LivenessProgress {
|
|
242
|
+
actionProgressValue = (smile / SMILE_THRESHOLD).coerceIn(0f, 1f)
|
|
243
|
+
stableFrames = if (smile >= SMILE_THRESHOLD) stableFrames + 1 else 0
|
|
244
|
+
if (stableFrames >= REQUIRED_STABLE_FRAMES) return completeCurrentAction()
|
|
245
|
+
return progress(
|
|
246
|
+
if (smile >= SMILE_THRESHOLD * 0.5f) LivenessPrompt.HOLD_SMILE else LivenessPrompt.SMILE,
|
|
247
|
+
LivenessAction.SMILE,
|
|
248
|
+
actionProgressValue
|
|
249
|
+
)
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* The terminal settle step. Auto-capture fires the instant liveness
|
|
254
|
+
* completes, so this is the only step whose predicate describes the frame the
|
|
255
|
+
* shutter is about to photograph: straight-on, eyes open, not mid-grin.
|
|
256
|
+
*
|
|
257
|
+
* The counter decays instead of resetting, because at [HOLD_FRAMES] > 2 a
|
|
258
|
+
* hard reset meant one involuntary blink threw away the whole window and
|
|
259
|
+
* charged the user for it twice.
|
|
260
|
+
*/
|
|
261
|
+
private fun updateHold(captureNeutral: Boolean): LivenessProgress {
|
|
262
|
+
stableFrames = if (captureNeutral) stableFrames + 1 else maxOf(0, stableFrames - 1)
|
|
263
|
+
actionProgressValue = (stableFrames.toFloat() / HOLD_FRAMES).coerceIn(0f, 1f)
|
|
264
|
+
if (stableFrames >= HOLD_FRAMES) {
|
|
265
|
+
// The challenge set is short by design, so the passive model has to
|
|
266
|
+
// have agreed across the recent window — not merely on this frame —
|
|
267
|
+
// before the session is allowed to finish.
|
|
268
|
+
if (!passiveConfidenceOk()) {
|
|
269
|
+
return progress(LivenessPrompt.VERIFY_REAL, LivenessAction.HOLD, actionProgressValue)
|
|
270
|
+
}
|
|
271
|
+
return completeCurrentAction()
|
|
272
|
+
}
|
|
273
|
+
return progress(LivenessPrompt.HOLD_STILL, LivenessAction.HOLD, actionProgressValue)
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
private fun updateDistance(frame: FaceFrameResult, closer: Boolean): LivenessProgress {
|
|
277
|
+
val base = if (baselineFill <= 0f) frame.faceFill.also { baselineFill = it } else baselineFill
|
|
278
|
+
val ratio = if (base > 0f) frame.faceFill / base else 1f
|
|
279
|
+
val reached = if (closer) {
|
|
280
|
+
ratio >= DISTANCE_NEAR_RATIO || frame.faceFill >= 0.80f
|
|
281
|
+
} else {
|
|
282
|
+
ratio <= DISTANCE_FAR_RATIO || frame.faceFill <= 0.20f
|
|
283
|
+
}
|
|
284
|
+
actionProgressValue = if (closer) {
|
|
285
|
+
((ratio - 1f) / (DISTANCE_NEAR_RATIO - 1f)).coerceIn(0f, 1f)
|
|
286
|
+
} else {
|
|
287
|
+
((1f - ratio) / (1f - DISTANCE_FAR_RATIO)).coerceIn(0f, 1f)
|
|
288
|
+
}
|
|
289
|
+
stableFrames = if (reached) stableFrames + 1 else 0
|
|
290
|
+
if (stableFrames >= REQUIRED_STABLE_FRAMES) return completeCurrentAction()
|
|
291
|
+
val action = if (closer) LivenessAction.NEAR else LivenessAction.FAR
|
|
292
|
+
return progress(action.prompt, action, actionProgressValue)
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
private fun updateHeadPose(
|
|
296
|
+
frame: FaceFrameResult,
|
|
297
|
+
action: LivenessAction
|
|
298
|
+
): LivenessProgress {
|
|
299
|
+
// The preview is mirrored, so the signs follow the user's screen direction.
|
|
300
|
+
val value: Float
|
|
301
|
+
val target: Float
|
|
302
|
+
when (action) {
|
|
303
|
+
LivenessAction.LEFT -> { value = frame.yaw; target = HEAD_YAW_TARGET }
|
|
304
|
+
LivenessAction.RIGHT -> { value = -frame.yaw; target = HEAD_YAW_TARGET }
|
|
305
|
+
LivenessAction.UP -> { value = frame.pitch; target = HEAD_PITCH_TARGET }
|
|
306
|
+
else -> { value = 0f; target = 1f }
|
|
307
|
+
}
|
|
308
|
+
actionProgressValue = (value / target).coerceIn(0f, 1f)
|
|
309
|
+
stableFrames = if (value >= target) stableFrames + 1 else 0
|
|
310
|
+
if (stableFrames >= REQUIRED_STABLE_FRAMES) return completeCurrentAction()
|
|
311
|
+
return progress(action.prompt, action, actionProgressValue)
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
private fun completeCurrentAction(): LivenessProgress {
|
|
315
|
+
currentIndex++
|
|
316
|
+
stableFrames = 0
|
|
317
|
+
neutralFrames = 0
|
|
318
|
+
blinkClosedSeen = false
|
|
319
|
+
baselineFill = 0f
|
|
320
|
+
actionProgressValue = 1f
|
|
321
|
+
needsNeutral = currentIndex < actions.size
|
|
322
|
+
return if (currentIndex >= actions.size) {
|
|
323
|
+
progress(LivenessPrompt.LIVENESS_OK)
|
|
324
|
+
} else {
|
|
325
|
+
progress(LivenessPrompt.STEP_DONE)
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
private fun positionPrompt(frame: FaceFrameResult): LivenessPrompt {
|
|
330
|
+
val fill = frame.faceFill
|
|
331
|
+
return when {
|
|
332
|
+
fill in 0.01f..0.20f -> LivenessPrompt.MOVE_CLOSER
|
|
333
|
+
fill > 0.74f -> LivenessPrompt.MOVE_AWAY
|
|
334
|
+
else -> LivenessPrompt.CENTER_FACE
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
private fun pauseCurrentSignal() {
|
|
339
|
+
stableFrames = 0
|
|
340
|
+
neutralFrames = 0
|
|
341
|
+
blinkClosedSeen = false
|
|
342
|
+
actionProgressValue = 0f
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
private fun resetCurrentSignal() {
|
|
346
|
+
pauseCurrentSignal()
|
|
347
|
+
needsNeutral = true
|
|
348
|
+
baselineFill = 0f
|
|
349
|
+
stepDeadlineMs = 0L
|
|
350
|
+
outOfFrameFrames = 0
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
private fun resetAllProgress() {
|
|
354
|
+
currentIndex = 0
|
|
355
|
+
resetCurrentSignal()
|
|
356
|
+
challengeStartedMs = nowMs()
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
private fun recordPassive(real: Boolean) {
|
|
360
|
+
passiveWindow.addLast(real)
|
|
361
|
+
while (passiveWindow.size > PASSIVE_WINDOW_FRAMES) passiveWindow.removeFirst()
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* True once the passive model has scored REAL on most of the recent window.
|
|
366
|
+
* A full window is required, so a session cannot complete on a handful of
|
|
367
|
+
* frames that happened to pass.
|
|
368
|
+
*/
|
|
369
|
+
private fun passiveConfidenceOk(): Boolean =
|
|
370
|
+
passiveWindow.size >= PASSIVE_WINDOW_FRAMES &&
|
|
371
|
+
passiveWindow.count { it } >= PASSIVE_MIN_REAL_FRAMES
|
|
372
|
+
|
|
373
|
+
private fun progress(
|
|
374
|
+
promptKey: LivenessPrompt,
|
|
375
|
+
cueAction: LivenessAction? = null,
|
|
376
|
+
actionProgress: Float = actionProgressValue
|
|
377
|
+
) = LivenessProgress(
|
|
378
|
+
currentAction = actions.getOrNull(currentIndex),
|
|
379
|
+
completedActions = actions.take(currentIndex),
|
|
380
|
+
isComplete = currentIndex >= actions.size,
|
|
381
|
+
promptKey = promptKey,
|
|
382
|
+
cueAction = cueAction,
|
|
383
|
+
actionProgress = actionProgress.coerceIn(0f, 1f),
|
|
384
|
+
stepIndex = currentIndex.coerceIn(0, actions.size),
|
|
385
|
+
stepCount = actions.size
|
|
386
|
+
)
|
|
387
|
+
|
|
388
|
+
private fun nowMs(): Long = System.nanoTime() / 1_000_000L
|
|
389
|
+
|
|
390
|
+
/** `internal` purely so the unit test can assert the challenge composition. */
|
|
391
|
+
internal companion object {
|
|
392
|
+
/**
|
|
393
|
+
* A blink plus exactly ONE movement, in a random order, then a neutral
|
|
394
|
+
* hold — always three steps.
|
|
395
|
+
*
|
|
396
|
+
* Exactly one movement is drawn, so LEFT and RIGHT can never both be asked
|
|
397
|
+
* in the same session: the old code drew one *or two* from a pool holding
|
|
398
|
+
* both, which asked ~8% of users to turn left and then right for no added
|
|
399
|
+
* security. SMILE is gone from the set entirely — ML Kit's classifier
|
|
400
|
+
* rarely crossed the 0.62 bar for a closed-mouth smile, making it the
|
|
401
|
+
* single biggest source of step timeouts, and the blink already covers
|
|
402
|
+
* everything a printed photo cannot do.
|
|
403
|
+
*
|
|
404
|
+
* HOLD is appended rather than shuffled in. Auto-capture fires the instant
|
|
405
|
+
* liveness completes, so the last step has to leave the user straight-on
|
|
406
|
+
* with eyes open; when a turn or a blink could land last, the still caught
|
|
407
|
+
* them mid-action and `extractLive()` failed the whole session.
|
|
408
|
+
*
|
|
409
|
+
* UP stays out of the pool: it has never been reachable from a random
|
|
410
|
+
* session, so HEAD_PITCH_TARGET is unvalidated on real devices. It remains
|
|
411
|
+
* available through an explicit `startLiveness(['up'])`.
|
|
412
|
+
*
|
|
413
|
+
* This leaves 6 ordered sequences (3 movements x 2 orders) where the old
|
|
414
|
+
* set had 168. That is a deliberate trade: replay resistance rests on the
|
|
415
|
+
* passive MiniFASNetV2 check — per frame, across the window in
|
|
416
|
+
* `passiveConfidenceOk`, and fail-closed again on the captured still — not
|
|
417
|
+
* on challenge entropy. Do not lower
|
|
418
|
+
* [AntiSpoofHelper.DEFAULT_THRESHOLD] to compensate for anything here.
|
|
419
|
+
*/
|
|
420
|
+
internal fun randomChallenge(): List<LivenessAction> {
|
|
421
|
+
val movement = listOf(
|
|
422
|
+
LivenessAction.LEFT,
|
|
423
|
+
LivenessAction.RIGHT,
|
|
424
|
+
LivenessAction.NEAR
|
|
425
|
+
).random()
|
|
426
|
+
return listOf(LivenessAction.BLINK, movement).shuffled() + LivenessAction.HOLD
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Guarantees the terminal neutral settle for an explicitly requested action
|
|
431
|
+
* list too. Without this, `startLiveness(['blink','left'])` ended on the
|
|
432
|
+
* turn and auto-capture photographed the user mid-turn — the exact failure
|
|
433
|
+
* the terminal hold exists to prevent.
|
|
434
|
+
*/
|
|
435
|
+
internal fun withTerminalHold(
|
|
436
|
+
requested: List<LivenessAction>
|
|
437
|
+
): List<LivenessAction> =
|
|
438
|
+
if (requested.lastOrNull() == LivenessAction.HOLD) {
|
|
439
|
+
requested
|
|
440
|
+
} else {
|
|
441
|
+
requested.filterNot { it == LivenessAction.HOLD } + LivenessAction.HOLD
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
// Every *_FRAMES constant is denominated in analysis frames, which run at
|
|
445
|
+
// roughly 12 fps (~83 ms) on a mid-range handset — so one frame trimmed
|
|
446
|
+
// here is ~83 ms off every session that passes through the gate.
|
|
447
|
+
//
|
|
448
|
+
// REQUIRED_STABLE_FRAMES stays at 2: at ~167 ms it is already negligible,
|
|
449
|
+
// and a single frame would let one noisy yaw estimate satisfy the only
|
|
450
|
+
// movement challenge in the session.
|
|
451
|
+
const val REQUIRED_STABLE_FRAMES = 2
|
|
452
|
+
const val REQUIRED_NEUTRAL_FRAMES = 2
|
|
453
|
+
const val FINAL_STABLE_FRAMES = 2
|
|
454
|
+
// Long enough to cover the shutter latency that follows it, no longer.
|
|
455
|
+
const val HOLD_FRAMES = 5
|
|
456
|
+
/** Consecutive out-of-frame frames tolerated while a move is in progress. */
|
|
457
|
+
const val OUT_OF_FRAME_GRACE_FRAMES = 3
|
|
458
|
+
/** Consecutive SPOOF frames tolerated before earned progress is dropped. */
|
|
459
|
+
const val SPOOF_GRACE_FRAMES = 4
|
|
460
|
+
const val PASSIVE_WINDOW_FRAMES = 10
|
|
461
|
+
const val PASSIVE_MIN_REAL_FRAMES = 6
|
|
462
|
+
const val NEUTRAL_DEGREES = 10f
|
|
463
|
+
const val HEAD_YAW_TARGET = 18f
|
|
464
|
+
const val HEAD_PITCH_TARGET = 14f
|
|
465
|
+
const val MIN_LUMINANCE = 42f
|
|
466
|
+
const val EYE_OPEN_THRESHOLD = 0.55f
|
|
467
|
+
const val EYE_CLOSED_MAXIMUM = 0.55f
|
|
468
|
+
const val EYE_CLOSED_AVERAGE = 0.42f
|
|
469
|
+
const val SMILE_THRESHOLD = 0.62f
|
|
470
|
+
/** Inter-step gates: loose, so a naturally wide mouth cannot stall them. */
|
|
471
|
+
const val SMILE_NEUTRAL_MAX = 0.70f
|
|
472
|
+
/** The terminal hold only: the frame auto-capture photographs. */
|
|
473
|
+
const val SMILE_CAPTURE_MAX = 0.45f
|
|
474
|
+
const val DISTANCE_NEAR_RATIO = 1.20f
|
|
475
|
+
const val DISTANCE_FAR_RATIO = 0.82f
|
|
476
|
+
// 13 s was sized for the SMILE step that no longer exists. 9 s is still
|
|
477
|
+
// ~3x the p95 of the two remaining steps with room for one missed blink on
|
|
478
|
+
// a slow device. The 45 s session budget is unchanged, so a hesitant user
|
|
479
|
+
// gets another RETRY_STEP rather than losing the session outright.
|
|
480
|
+
const val STEP_TIMEOUT_MS = 9_000L
|
|
481
|
+
const val CHALLENGE_TIMEOUT_MS = 45_000L
|
|
482
|
+
}
|
|
483
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
package com.facematch.sdk
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Every user-facing liveness / positioning prompt as a stable key with an
|
|
5
|
+
* English and a Hindi string. The detector only ever emits a key; the on-screen
|
|
6
|
+
* strip and the spoken voice both resolve it to the language the caller chose,
|
|
7
|
+
* so the two never drift apart.
|
|
8
|
+
*
|
|
9
|
+
* [spoken] marks the prompts that voice guidance reads aloud. Fast intermediate
|
|
10
|
+
* states (mid-blink, "look straight" between every step, silent confirmations)
|
|
11
|
+
* are shown on screen but never spoken, so the voice stays calm.
|
|
12
|
+
*/
|
|
13
|
+
enum class LivenessPrompt(
|
|
14
|
+
val en: String,
|
|
15
|
+
val hi: String,
|
|
16
|
+
val spoken: Boolean = true
|
|
17
|
+
) {
|
|
18
|
+
GET_READY(
|
|
19
|
+
"Get ready",
|
|
20
|
+
"तैयार हो जाइए"
|
|
21
|
+
),
|
|
22
|
+
POSITION_FACE(
|
|
23
|
+
"Position your face in the frame",
|
|
24
|
+
"अपना चेहरा फ्रेम के अंदर रखें"
|
|
25
|
+
),
|
|
26
|
+
KEEP_IN_FRAME(
|
|
27
|
+
"Keep your face inside the frame",
|
|
28
|
+
"चेहरे को फ्रेम के अंदर रखें"
|
|
29
|
+
),
|
|
30
|
+
MOVE_CLOSER(
|
|
31
|
+
"Move a little closer",
|
|
32
|
+
"थोड़ा पास आएँ"
|
|
33
|
+
),
|
|
34
|
+
MOVE_AWAY(
|
|
35
|
+
"Move a little away",
|
|
36
|
+
"थोड़ा पीछे जाएँ"
|
|
37
|
+
),
|
|
38
|
+
CENTER_FACE(
|
|
39
|
+
"Center your face",
|
|
40
|
+
"चेहरे को बीच में रखें"
|
|
41
|
+
),
|
|
42
|
+
HOLD_UPRIGHT(
|
|
43
|
+
"Keep your head upright",
|
|
44
|
+
"अपना सिर सीधा रखें"
|
|
45
|
+
),
|
|
46
|
+
MULTIPLE_FACES(
|
|
47
|
+
"Only one person allowed",
|
|
48
|
+
"केवल एक व्यक्ति की अनुमति है"
|
|
49
|
+
),
|
|
50
|
+
LOW_LIGHT(
|
|
51
|
+
"Move to better lighting",
|
|
52
|
+
"बेहतर रोशनी में आएँ"
|
|
53
|
+
),
|
|
54
|
+
VERIFY_REAL(
|
|
55
|
+
"Checking it's really you",
|
|
56
|
+
"जाँच हो रही है कि यह वाकई आप हैं",
|
|
57
|
+
spoken = false
|
|
58
|
+
),
|
|
59
|
+
SPOOF_BLOCKED(
|
|
60
|
+
"Use your real face, not a photo or screen",
|
|
61
|
+
"असली चेहरा दिखाएँ, फ़ोटो या स्क्रीन नहीं"
|
|
62
|
+
),
|
|
63
|
+
LOOK_STRAIGHT(
|
|
64
|
+
"Look straight at the camera",
|
|
65
|
+
"कैमरे की ओर सीधे देखें",
|
|
66
|
+
spoken = false
|
|
67
|
+
),
|
|
68
|
+
BLINK(
|
|
69
|
+
"Please blink your eyes",
|
|
70
|
+
"कृपया अपनी आँखें झपकाएँ"
|
|
71
|
+
),
|
|
72
|
+
OPEN_EYES(
|
|
73
|
+
"Now open your eyes",
|
|
74
|
+
"अब अपनी आँखें खोलें",
|
|
75
|
+
spoken = false
|
|
76
|
+
),
|
|
77
|
+
SMILE(
|
|
78
|
+
"Please smile",
|
|
79
|
+
"कृपया मुस्कुराएँ"
|
|
80
|
+
),
|
|
81
|
+
HOLD_SMILE(
|
|
82
|
+
"Hold that smile",
|
|
83
|
+
"मुस्कान बनाए रखें",
|
|
84
|
+
spoken = false
|
|
85
|
+
),
|
|
86
|
+
TURN_LEFT(
|
|
87
|
+
"Please turn your head to the left",
|
|
88
|
+
"कृपया अपना सिर बाईं ओर घुमाएँ"
|
|
89
|
+
),
|
|
90
|
+
TURN_RIGHT(
|
|
91
|
+
"Please turn your head to the right",
|
|
92
|
+
"कृपया अपना सिर दाईं ओर घुमाएँ"
|
|
93
|
+
),
|
|
94
|
+
LOOK_UP(
|
|
95
|
+
"Please tilt your head up",
|
|
96
|
+
"कृपया अपना सिर ऊपर करें"
|
|
97
|
+
),
|
|
98
|
+
COME_CLOSER(
|
|
99
|
+
"Please move slightly closer",
|
|
100
|
+
"कृपया थोड़ा पास आएँ"
|
|
101
|
+
),
|
|
102
|
+
GO_BACK(
|
|
103
|
+
"Please move slightly away",
|
|
104
|
+
"कृपया थोड़ा पीछे जाएँ"
|
|
105
|
+
),
|
|
106
|
+
HOLD_STILL(
|
|
107
|
+
"Hold still",
|
|
108
|
+
"स्थिर रहें"
|
|
109
|
+
),
|
|
110
|
+
STEP_DONE(
|
|
111
|
+
"Good, next step",
|
|
112
|
+
"बढ़िया, अगला चरण",
|
|
113
|
+
spoken = false
|
|
114
|
+
),
|
|
115
|
+
RETRY_STEP(
|
|
116
|
+
"Let's try that step again",
|
|
117
|
+
"यह चरण फिर से करें"
|
|
118
|
+
),
|
|
119
|
+
TOO_SLOW(
|
|
120
|
+
"That took too long, starting again",
|
|
121
|
+
"बहुत समय लग गया, फिर से शुरू"
|
|
122
|
+
),
|
|
123
|
+
VERIFYING(
|
|
124
|
+
"Verifying",
|
|
125
|
+
"सत्यापन हो रहा है",
|
|
126
|
+
spoken = false
|
|
127
|
+
),
|
|
128
|
+
LIVENESS_OK(
|
|
129
|
+
"Liveness verified",
|
|
130
|
+
"जीवंतता सत्यापित"
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
fun localized(language: String): String = when (language.lowercase()) {
|
|
134
|
+
"hi" -> hi
|
|
135
|
+
else -> en
|
|
136
|
+
}
|
|
137
|
+
}
|