Skip to content

Commit 2856562

Browse files
authored
feat(liveness): Use backend-provided values for some FaceDectector values (#191)
1 parent 00283d4 commit 2856562

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

liveness/src/main/java/com/amplifyframework/ui/liveness/ml/FaceDetector.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.amplifyframework.ui.liveness.ml
1818
import android.content.Context
1919
import android.graphics.RectF
2020
import androidx.annotation.VisibleForTesting
21+
import com.amplifyframework.predictions.aws.models.FaceTargetChallenge
2122
import com.amplifyframework.predictions.aws.models.FaceTargetMatchingParameters
2223
import com.amplifyframework.ui.liveness.R
2324
import com.amplifyframework.ui.liveness.camera.LivenessCoordinator.Companion.TARGET_HEIGHT
@@ -42,10 +43,12 @@ internal class FaceDetector(private val livenessState: LivenessState) {
4243
outputScores: Array<Array<FloatArray>>
4344
): List<Detection> {
4445
val detections = mutableListOf<Detection>()
46+
val faceTargetChallenge = livenessState.faceTargetChallenge ?: return emptyList()
4547
for (i in 0 until NUM_BOXES) {
4648
var score = outputScores[0][i][0]
4749
score = computeSigmoid(score)
48-
if (score < MIN_SCORE_THRESHOLD) {
50+
51+
if (score < faceTargetChallenge.faceTargetMatching.faceDetectionThreshold) {
4952
continue
5053
}
5154

@@ -159,6 +162,7 @@ internal class FaceDetector(private val livenessState: LivenessState) {
159162
scaledMouth,
160163
scaledLeftEar,
161164
scaledRightEar,
165+
faceTargetChallenge.faceTargetMatching.targetHeightWidthRatio
162166
)
163167
renormalizedDetections.add(
164168
Detection(
@@ -183,13 +187,14 @@ internal class FaceDetector(private val livenessState: LivenessState) {
183187
nose: Landmark,
184188
mouth: Landmark,
185189
leftEar: Landmark,
186-
rightEar: Landmark
190+
rightEar: Landmark,
191+
heightWidthRatio: Float
187192
): RectF {
188193
val pupilDistance = calculatePupilDistance(leftEye, rightEye)
189194
val faceHeight = calculateFaceHeight(leftEye, rightEye, mouth)
190195

191196
val ow = (ALPHA * pupilDistance + GAMMA * faceHeight) / 2
192-
val oh = GOLDEN_RATIO * ow
197+
val oh = heightWidthRatio * ow
193198

194199
val eyeCenterX = (leftEye.x + rightEye.x) / 2
195200
val eyeCenterY = (leftEye.y + rightEye.y) / 2
@@ -450,7 +455,6 @@ internal class FaceDetector(private val livenessState: LivenessState) {
450455

451456
companion object {
452457
private const val MIN_SUPPRESSION_THRESHOLD = 0.3f
453-
private const val MIN_SCORE_THRESHOLD = 0.7f
454458
private val strides = listOf(8, 16, 16, 16)
455459
private const val ASPECT_RATIOS_SIZE = 1
456460
private const val MIN_SCALE = 0.1484375f
@@ -459,7 +463,6 @@ internal class FaceDetector(private val livenessState: LivenessState) {
459463
private const val ANCHOR_OFFSET_Y = 0.5f
460464
private const val INPUT_SIZE_HEIGHT = 128
461465
private const val INPUT_SIZE_WIDTH = 128
462-
private const val GOLDEN_RATIO = 1.618f
463466
private const val ALPHA = 2.0f
464467
private const val GAMMA = 1.8f
465468
const val X_SCALE = 128f
@@ -479,7 +482,6 @@ internal class FaceDetector(private val livenessState: LivenessState) {
479482
* 14, 15 - right eye tragion
480483
*/
481484
const val NUM_COORDS = 16
482-
const val INITIAL_FACE_DISTANCE_THRESHOLD = 0.32f
483485

484486
fun loadModel(context: Context): Interpreter {
485487
val modelFileDescriptor =

liveness/src/main/java/com/amplifyframework/ui/liveness/state/LivenessState.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ internal data class LivenessState(
222222
leftEye, rightEye, mouth,
223223
LivenessCoordinator.TARGET_WIDTH, LivenessCoordinator.TARGET_HEIGHT
224224
)
225-
if (faceDistance >= FaceDetector.INITIAL_FACE_DISTANCE_THRESHOLD) {
225+
if (faceDistance >= faceTargetChallenge!!.faceTargetMatching.faceDistanceThresholdMin) {
226226
livenessCheckState =
227227
LivenessCheckState.Initial.withMoveFaceFurtherAwayMessage()
228228
} else {

liveness/src/test/java/com/amplifyframework/ui/liveness/ml/FaceDetectorTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ internal class FaceDetectorTest {
3737
private val mouth = Landmark(0.5062596f, 0.68926525f)
3838
private val leftEar = Landmark(0.78989476f, 0.5973732f)
3939
private val rightEar = Landmark(0.16585289f, 0.5668279f)
40-
private val width = 0.65490234f
41-
private val height = 0.49117205f
40+
private val heightWidthRatio = 1.618f
4241

4342
@Before
4443
fun setup() {
@@ -68,7 +67,8 @@ internal class FaceDetectorTest {
6867
nose,
6968
mouth,
7069
leftEar,
71-
rightEar
70+
rightEar,
71+
heightWidthRatio
7272
)
7373

7474
// then

0 commit comments

Comments
 (0)