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,92 @@
|
|
|
1
|
+
package com.facematch.sdk
|
|
2
|
+
|
|
3
|
+
import kotlin.math.exp
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Stateless face comparison.
|
|
7
|
+
*
|
|
8
|
+
* faceSimilarityScore — sigmoid-calibrated cosine similarity [0, 100].
|
|
9
|
+
*
|
|
10
|
+
* Raw cosine from buffalo_sc (MobileFaceNet / ArcFace) embeddings:
|
|
11
|
+
* Genuine (same-person) pairs : ~0.38 – 0.65
|
|
12
|
+
* Impostor (different person) : ~0.05 – 0.32
|
|
13
|
+
*
|
|
14
|
+
* A sigmoid centred in the measured separation band is applied:
|
|
15
|
+
* score = sigmoid(K * (rawCosine − MID)) * 100
|
|
16
|
+
* K = 47, MID = 0.338
|
|
17
|
+
*
|
|
18
|
+
* Key calibration points:
|
|
19
|
+
* raw 0.375 -> ~85 % (low genuine / long age gap)
|
|
20
|
+
* raw 0.351 -> ~65 % (Possible Match boundary)
|
|
21
|
+
* raw 0.320 -> ~30 % (upper documented impostor range)
|
|
22
|
+
* raw 0.200 -> <1 % (clear impostor)
|
|
23
|
+
*
|
|
24
|
+
* Match tiers:
|
|
25
|
+
* >= 80 % -> Match
|
|
26
|
+
* 65-79 % -> Possible Match
|
|
27
|
+
* < 65 % -> No Match
|
|
28
|
+
*/
|
|
29
|
+
object FaceMatcherHelper {
|
|
30
|
+
|
|
31
|
+
// Sigmoid calibration parameters — must stay in sync with Python matcher.py
|
|
32
|
+
private const val SIG_MID = 0.338
|
|
33
|
+
private const val SIG_K = 47.0
|
|
34
|
+
|
|
35
|
+
// Score-space thresholds (applied after sigmoid mapping)
|
|
36
|
+
private const val THRESHOLD_MATCH = 80.0f
|
|
37
|
+
private const val THRESHOLD_POSSIBLE = 65.0f
|
|
38
|
+
|
|
39
|
+
enum class MatchStatus(val label: String, val emoji: String) {
|
|
40
|
+
MATCH("Match", "✅"),
|
|
41
|
+
POSSIBLE_MATCH("Possible Match", "⚠️"),
|
|
42
|
+
NO_MATCH("No Match", "❌")
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
data class MatchResult(
|
|
46
|
+
/** Sigmoid-calibrated similarity for this pair — 0.0 to 100.0. */
|
|
47
|
+
val faceSimilarityScore: Float,
|
|
48
|
+
val matchStatus: MatchStatus,
|
|
49
|
+
val isMatch: Boolean
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
/** Map raw cosine in [0, 1] to a calibrated score in [0, 100]. */
|
|
53
|
+
private fun sigmoidScore(rawCosine: Double): Float {
|
|
54
|
+
val score = 1.0 / (1.0 + exp(-SIG_K * (rawCosine - SIG_MID))) * 100.0
|
|
55
|
+
return score.toFloat()
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
fun compare(emb1: FloatArray, emb2: FloatArray): MatchResult {
|
|
59
|
+
require(emb1.size == emb2.size) {
|
|
60
|
+
"Embedding size mismatch: ${emb1.size} vs ${emb2.size}"
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
var dot = 0.0
|
|
64
|
+
var norm1 = 0.0
|
|
65
|
+
var norm2 = 0.0
|
|
66
|
+
for (i in emb1.indices) {
|
|
67
|
+
val first = emb1[i].toDouble()
|
|
68
|
+
val second = emb2[i].toDouble()
|
|
69
|
+
require(first.isFinite() && second.isFinite()) { "Embedding values must be finite" }
|
|
70
|
+
dot += first * second
|
|
71
|
+
norm1 += first * first
|
|
72
|
+
norm2 += second * second
|
|
73
|
+
}
|
|
74
|
+
require(norm1 > 1e-12 && norm2 > 1e-12) { "Embeddings must have non-zero norm" }
|
|
75
|
+
val rawCosine = (dot / kotlin.math.sqrt(norm1 * norm2)).coerceIn(0.0, 1.0)
|
|
76
|
+
|
|
77
|
+
val faceSimilarityScore = Math.round(sigmoidScore(rawCosine) * 100f) / 100f
|
|
78
|
+
|
|
79
|
+
val matchStatus = when {
|
|
80
|
+
faceSimilarityScore >= THRESHOLD_MATCH -> MatchStatus.MATCH
|
|
81
|
+
faceSimilarityScore >= THRESHOLD_POSSIBLE -> MatchStatus.POSSIBLE_MATCH
|
|
82
|
+
else -> MatchStatus.NO_MATCH
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return MatchResult(
|
|
86
|
+
faceSimilarityScore = faceSimilarityScore,
|
|
87
|
+
matchStatus = matchStatus,
|
|
88
|
+
isMatch = matchStatus == MatchStatus.MATCH
|
|
89
|
+
)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
package com.facematch.sdk
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.graphics.Canvas
|
|
5
|
+
import android.graphics.Color
|
|
6
|
+
import android.graphics.DashPathEffect
|
|
7
|
+
import android.graphics.Paint
|
|
8
|
+
import android.graphics.RectF
|
|
9
|
+
import android.view.Choreographer
|
|
10
|
+
import android.view.View
|
|
11
|
+
import kotlin.math.min
|
|
12
|
+
import kotlin.math.sin
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Minimal face-positioning frame drawn over the camera preview.
|
|
16
|
+
*
|
|
17
|
+
* By design this view draws only the oval frame, its corner brackets and a
|
|
18
|
+
* thin edge progress arc — never text, cards or cues that sit over the face.
|
|
19
|
+
* All instructions live in the React Native strip outside the preview so the
|
|
20
|
+
* camera feed stays completely visible.
|
|
21
|
+
*/
|
|
22
|
+
class FaceOverlayView(context: Context) : View(context) {
|
|
23
|
+
|
|
24
|
+
private enum class Guide { SEARCHING, WARN, POSITIONING, ACTIVE, COMPLETE }
|
|
25
|
+
|
|
26
|
+
@Volatile private var frame = FaceFrameResult(null, null, 1, 1)
|
|
27
|
+
@Volatile private var progress: LivenessProgress = LivenessProgress(
|
|
28
|
+
currentAction = null,
|
|
29
|
+
completedActions = emptyList(),
|
|
30
|
+
isComplete = false,
|
|
31
|
+
promptKey = LivenessPrompt.POSITION_FACE
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
private val density = resources.displayMetrics.density
|
|
35
|
+
private fun dp(value: Float) = value * density
|
|
36
|
+
|
|
37
|
+
private val cSearch = Color.parseColor("#94A3B8")
|
|
38
|
+
private val cWarn = Color.parseColor("#F87171")
|
|
39
|
+
private val cAmber = Color.parseColor("#FBBF24")
|
|
40
|
+
private val cGreen = Color.parseColor("#34D399")
|
|
41
|
+
private val cInk = Color.parseColor("#F4F6FB")
|
|
42
|
+
|
|
43
|
+
private val ringPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
|
44
|
+
style = Paint.Style.STROKE
|
|
45
|
+
strokeCap = Paint.Cap.ROUND
|
|
46
|
+
}
|
|
47
|
+
private val glowPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
|
48
|
+
style = Paint.Style.STROKE
|
|
49
|
+
strokeCap = Paint.Cap.ROUND
|
|
50
|
+
}
|
|
51
|
+
private val strokePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
|
52
|
+
style = Paint.Style.STROKE
|
|
53
|
+
strokeCap = Paint.Cap.ROUND
|
|
54
|
+
strokeJoin = Paint.Join.ROUND
|
|
55
|
+
}
|
|
56
|
+
private val dashEffect = DashPathEffect(floatArrayOf(dp(3f), dp(9f)), 0f)
|
|
57
|
+
|
|
58
|
+
private var running = false
|
|
59
|
+
private var startNanos = 0L
|
|
60
|
+
private var tMs = 0L
|
|
61
|
+
private val frameCallback = object : Choreographer.FrameCallback {
|
|
62
|
+
override fun doFrame(frameTimeNanos: Long) {
|
|
63
|
+
if (startNanos == 0L) startNanos = frameTimeNanos
|
|
64
|
+
tMs = (frameTimeNanos - startNanos) / 1_000_000L
|
|
65
|
+
invalidate()
|
|
66
|
+
if (running) Choreographer.getInstance().postFrameCallback(this)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
override fun onAttachedToWindow() {
|
|
71
|
+
super.onAttachedToWindow()
|
|
72
|
+
running = true
|
|
73
|
+
startNanos = 0L
|
|
74
|
+
Choreographer.getInstance().postFrameCallback(frameCallback)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
override fun onDetachedFromWindow() {
|
|
78
|
+
super.onDetachedFromWindow()
|
|
79
|
+
running = false
|
|
80
|
+
Choreographer.getInstance().removeFrameCallback(frameCallback)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
fun update(result: FaceFrameResult, prog: LivenessProgress) {
|
|
84
|
+
frame = result
|
|
85
|
+
progress = prog
|
|
86
|
+
if (!running) postInvalidate()
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
fun reset() {
|
|
90
|
+
frame = FaceFrameResult(null, null, 1, 1)
|
|
91
|
+
progress = LivenessProgress(
|
|
92
|
+
currentAction = null,
|
|
93
|
+
completedActions = emptyList(),
|
|
94
|
+
isComplete = false,
|
|
95
|
+
promptKey = LivenessPrompt.POSITION_FACE
|
|
96
|
+
)
|
|
97
|
+
postInvalidate()
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
override fun onDraw(canvas: Canvas) {
|
|
101
|
+
super.onDraw(canvas)
|
|
102
|
+
val w = width.toFloat()
|
|
103
|
+
val h = height.toFloat()
|
|
104
|
+
if (w <= 0f || h <= 0f) return
|
|
105
|
+
|
|
106
|
+
val f = frame
|
|
107
|
+
val p = progress
|
|
108
|
+
val guide = resolveGuide(f, p)
|
|
109
|
+
val accent = when (guide) {
|
|
110
|
+
Guide.SEARCHING -> cSearch
|
|
111
|
+
Guide.WARN -> cWarn
|
|
112
|
+
Guide.POSITIONING -> cAmber
|
|
113
|
+
Guide.ACTIVE, Guide.COMPLETE -> cGreen
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
val ovalW = min(w * 0.68f, dp(300f))
|
|
117
|
+
val ovalH = min(ovalW * 1.32f, h * 0.72f).coerceAtLeast(dp(170f))
|
|
118
|
+
val cx = w / 2f
|
|
119
|
+
val cy = h * 0.46f
|
|
120
|
+
val oval = RectF(cx - ovalW / 2f, cy - ovalH / 2f, cx + ovalW / 2f, cy + ovalH / 2f)
|
|
121
|
+
|
|
122
|
+
val pulse = 0.5f + 0.5f * sin(tMs / 520.0).toFloat()
|
|
123
|
+
|
|
124
|
+
// Faint track
|
|
125
|
+
ringPaint.pathEffect = if (guide == Guide.SEARCHING) dashEffect else null
|
|
126
|
+
ringPaint.strokeWidth = dp(2.5f)
|
|
127
|
+
ringPaint.color = withAlpha(accent, if (guide == Guide.SEARCHING) 0.7f else 0.28f)
|
|
128
|
+
canvas.drawOval(oval, ringPaint)
|
|
129
|
+
|
|
130
|
+
if (guide != Guide.SEARCHING) {
|
|
131
|
+
ringPaint.pathEffect = null
|
|
132
|
+
ringPaint.strokeWidth = dp(4f)
|
|
133
|
+
ringPaint.color = withAlpha(accent, 0.96f)
|
|
134
|
+
canvas.drawOval(oval, ringPaint)
|
|
135
|
+
|
|
136
|
+
glowPaint.strokeWidth = dp(10f + 8f * pulse)
|
|
137
|
+
glowPaint.color = withAlpha(accent, 0.08f + 0.14f * pulse)
|
|
138
|
+
canvas.drawOval(oval, glowPaint)
|
|
139
|
+
} else {
|
|
140
|
+
// Rotating scan arc while looking for a face.
|
|
141
|
+
val head = (tMs / 6f) % 360f
|
|
142
|
+
strokePaint.pathEffect = null
|
|
143
|
+
strokePaint.strokeWidth = dp(4f)
|
|
144
|
+
for (i in 0 until 4) {
|
|
145
|
+
strokePaint.color = withAlpha(cSearch, 0.6f - i * 0.13f)
|
|
146
|
+
canvas.drawArc(oval, head - i * 14f - 22f, 20f, false, strokePaint)
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Corner brackets frame the target while the face is being placed.
|
|
151
|
+
if (guide == Guide.SEARCHING || guide == Guide.POSITIONING || guide == Guide.WARN) {
|
|
152
|
+
drawCornerBrackets(canvas, oval, accent)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Edge progress arc for the current step's confidence — drawn on the
|
|
156
|
+
// frame edge only, never across the face.
|
|
157
|
+
if (guide == Guide.ACTIVE && p.actionProgress > 0.01f) {
|
|
158
|
+
strokePaint.pathEffect = null
|
|
159
|
+
strokePaint.strokeWidth = dp(5f)
|
|
160
|
+
strokePaint.color = withAlpha(cGreen, 0.98f)
|
|
161
|
+
canvas.drawArc(oval, -90f, 360f * p.actionProgress.coerceIn(0f, 1f), false, strokePaint)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Completed-step ticks along the top of the frame.
|
|
165
|
+
if (p.stepCount in 1..6) {
|
|
166
|
+
drawStepTicks(canvas, oval, p)
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
private fun resolveGuide(f: FaceFrameResult, p: LivenessProgress): Guide = when {
|
|
171
|
+
p.isComplete -> Guide.COMPLETE
|
|
172
|
+
f.hasMultipleFaces -> Guide.WARN
|
|
173
|
+
!f.hasFace -> Guide.SEARCHING
|
|
174
|
+
f.antiSpoof?.status == AntiSpoofStatus.ERROR -> Guide.WARN
|
|
175
|
+
f.antiSpoof?.status == AntiSpoofStatus.SPOOF -> Guide.WARN
|
|
176
|
+
f.antiSpoof == null -> Guide.POSITIONING
|
|
177
|
+
!f.isPositioned && p.cueAction == null -> Guide.POSITIONING
|
|
178
|
+
else -> Guide.ACTIVE
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
private fun drawCornerBrackets(canvas: Canvas, oval: RectF, accent: Int) {
|
|
182
|
+
val len = dp(24f)
|
|
183
|
+
val gap = dp(12f)
|
|
184
|
+
strokePaint.pathEffect = null
|
|
185
|
+
strokePaint.color = withAlpha(accent, 0.9f)
|
|
186
|
+
strokePaint.strokeWidth = dp(3f)
|
|
187
|
+
val l = oval.left - gap
|
|
188
|
+
val t = oval.top - gap
|
|
189
|
+
val r = oval.right + gap
|
|
190
|
+
val b = oval.bottom + gap
|
|
191
|
+
canvas.drawLine(l, t + len, l, t, strokePaint); canvas.drawLine(l, t, l + len, t, strokePaint)
|
|
192
|
+
canvas.drawLine(r - len, t, r, t, strokePaint); canvas.drawLine(r, t, r, t + len, strokePaint)
|
|
193
|
+
canvas.drawLine(l, b - len, l, b, strokePaint); canvas.drawLine(l, b, l + len, b, strokePaint)
|
|
194
|
+
canvas.drawLine(r - len, b, r, b, strokePaint); canvas.drawLine(r, b, r, b - len, strokePaint)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private fun drawStepTicks(canvas: Canvas, oval: RectF, p: LivenessProgress) {
|
|
198
|
+
val count = p.stepCount
|
|
199
|
+
val done = p.stepIndex.coerceIn(0, count)
|
|
200
|
+
val dotR = dp(3.5f)
|
|
201
|
+
val gap = dp(13f)
|
|
202
|
+
val totalW = (count - 1) * gap
|
|
203
|
+
var x = oval.centerX() - totalW / 2f
|
|
204
|
+
val y = oval.top - dp(30f)
|
|
205
|
+
strokePaint.pathEffect = null
|
|
206
|
+
for (i in 0 until count) {
|
|
207
|
+
when {
|
|
208
|
+
i < done -> {
|
|
209
|
+
strokePaint.style = Paint.Style.FILL
|
|
210
|
+
strokePaint.color = withAlpha(cGreen, 1f)
|
|
211
|
+
canvas.drawCircle(x, y, dotR, strokePaint)
|
|
212
|
+
}
|
|
213
|
+
i == done -> {
|
|
214
|
+
strokePaint.style = Paint.Style.FILL
|
|
215
|
+
strokePaint.color = withAlpha(cAmber, 0.95f)
|
|
216
|
+
canvas.drawCircle(x, y, dotR + dp(1.5f), strokePaint)
|
|
217
|
+
}
|
|
218
|
+
else -> {
|
|
219
|
+
strokePaint.style = Paint.Style.STROKE
|
|
220
|
+
strokePaint.strokeWidth = dp(1.5f)
|
|
221
|
+
strokePaint.color = withAlpha(cInk, 0.4f)
|
|
222
|
+
canvas.drawCircle(x, y, dotR, strokePaint)
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
x += gap
|
|
226
|
+
}
|
|
227
|
+
strokePaint.style = Paint.Style.STROKE
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
private fun withAlpha(color: Int, alpha: Float): Int = Color.argb(
|
|
231
|
+
(alpha.coerceIn(0f, 1f) * 255f).toInt(),
|
|
232
|
+
Color.red(color),
|
|
233
|
+
Color.green(color),
|
|
234
|
+
Color.blue(color)
|
|
235
|
+
)
|
|
236
|
+
}
|