|
| 1 | +// |
| 2 | +// Copyright Amazon.com Inc. or its affiliates. |
| 3 | +// All Rights Reserved. |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: Apache-2.0 |
| 6 | +// |
| 7 | + |
| 8 | +import XCTest |
| 9 | +@testable import FaceLiveness |
| 10 | + |
| 11 | + |
| 12 | +final class DetectedFaceTests: XCTestCase { |
| 13 | + var detectedFace: DetectedFace! |
| 14 | + var expectedNormalizeFace: DetectedFace! |
| 15 | + let normalizeWidth = 414.0 |
| 16 | + let normalizeHeight = 552.0 |
| 17 | + |
| 18 | + override func setUp() { |
| 19 | + let boundingBox = CGRect( |
| 20 | + x: 0.15805082494171963, |
| 21 | + y: 0.3962942063808441, |
| 22 | + width: 0.6549023386310235, |
| 23 | + height: 0.49117204546928406 |
| 24 | + ) |
| 25 | + let leftEye = CGPoint(x: 0.6686329891870315, y: 0.48738187551498413) |
| 26 | + let rightEye = CGPoint(x: 0.35714725227596134, y: 0.4664449691772461) |
| 27 | + let nose = CGPoint(x: 0.5283648181467697, y: 0.5319401621818542) |
| 28 | + let mouth = CGPoint(x: 0.5062596005080024, y: 0.689265251159668) |
| 29 | + let rightEar = CGPoint(x: 0.1658528943614037, y: 0.5668278932571411) |
| 30 | + let leftEar = CGPoint(x: 0.7898947484263203, y: 0.5973731875419617) |
| 31 | + let confidence: Float = 0.94027895 |
| 32 | + detectedFace = DetectedFace( |
| 33 | + boundingBox: boundingBox, |
| 34 | + leftEye: leftEye, |
| 35 | + rightEye: rightEye, |
| 36 | + nose: nose, |
| 37 | + mouth: mouth, |
| 38 | + rightEar: rightEar, |
| 39 | + leftEar: leftEar, |
| 40 | + confidence: confidence |
| 41 | + ) |
| 42 | + |
| 43 | + let normalizedBoundingBox = CGRect( |
| 44 | + x: 0.15805082494171963 * normalizeWidth, |
| 45 | + y: 0.3962942063808441 * normalizeHeight, |
| 46 | + width: 0.6549023386310235 * normalizeWidth, |
| 47 | + height: 0.49117204546928406 * normalizeHeight |
| 48 | + ) |
| 49 | + let normalizedLeftEye = CGPoint( |
| 50 | + x: 0.6686329891870315 * normalizeWidth, |
| 51 | + y: 0.48738187551498413 * normalizeHeight |
| 52 | + ) |
| 53 | + let normalizedRightEye = CGPoint( |
| 54 | + x: 0.35714725227596134 * normalizeWidth, |
| 55 | + y: 0.4664449691772461 * normalizeHeight) |
| 56 | + let normalizedNose = CGPoint( |
| 57 | + x: 0.5283648181467697 * normalizeWidth, |
| 58 | + y: 0.5319401621818542 * normalizeHeight |
| 59 | + ) |
| 60 | + let normalizedMouth = CGPoint( |
| 61 | + x: 0.5062596005080024 * normalizeWidth, |
| 62 | + y: 0.689265251159668 * normalizeHeight |
| 63 | + ) |
| 64 | + let normalizedRightEar = CGPoint( |
| 65 | + x: 0.1658528943614037 * normalizeWidth, |
| 66 | + y: 0.5668278932571411 * normalizeHeight |
| 67 | + ) |
| 68 | + let normalizedLeftEar = CGPoint( |
| 69 | + x: 0.7898947484263203 * normalizeWidth, |
| 70 | + y: 0.5973731875419617 * normalizeHeight |
| 71 | + ) |
| 72 | + |
| 73 | + expectedNormalizeFace = DetectedFace( |
| 74 | + boundingBox: normalizedBoundingBox, |
| 75 | + leftEye: normalizedLeftEye, |
| 76 | + rightEye: normalizedRightEye, |
| 77 | + nose: normalizedNose, |
| 78 | + mouth: normalizedMouth, |
| 79 | + rightEar: normalizedRightEar, |
| 80 | + leftEar: normalizedLeftEar, |
| 81 | + confidence: confidence |
| 82 | + ) |
| 83 | + } |
| 84 | + |
| 85 | + /// Given: A `DetectedFace` |
| 86 | + /// When: when the struct is initialized |
| 87 | + /// Then: the calculated landmarks are available and calculated as expected |
| 88 | + func testDetectedFaceLandmarks() { |
| 89 | + XCTAssertEqual(detectedFace.eyeCenterX, 0.5128901207314964) |
| 90 | + XCTAssertEqual(detectedFace.eyeCenterY, 0.4769134223461151) |
| 91 | + XCTAssertEqual(detectedFace.faceDistance, 0.31218859419592454) |
| 92 | + XCTAssertEqual(detectedFace.pupilDistance, 0.31218859419592454) |
| 93 | + XCTAssertEqual(detectedFace.faceHeight, 0.21245532000610062) |
| 94 | + } |
| 95 | + |
| 96 | + /// Given: A `DetectedFace` |
| 97 | + /// When: when boundingBoxFromLandmarks is called |
| 98 | + /// Then: the calculated bounding box is returned |
| 99 | + func testDetectedFaceBoundingBoxFromLandmarks() { |
| 100 | + let ovalRect = CGRect.zero |
| 101 | + let expectedBoundingBox = CGRect( |
| 102 | + x: 0.1658528943614037, |
| 103 | + y: 0.041756969751750916, |
| 104 | + width: 0.6240418540649166, |
| 105 | + height: 0.8457092820983773 |
| 106 | + ) |
| 107 | + let boundingBox = detectedFace.boundingBoxFromLandmarks(ovalRect: ovalRect) |
| 108 | + XCTAssertEqual(boundingBox.origin.x, expectedBoundingBox.origin.x) |
| 109 | + XCTAssertEqual(boundingBox.origin.y, expectedBoundingBox.origin.y) |
| 110 | + XCTAssertEqual(boundingBox.width, expectedBoundingBox.width) |
| 111 | + XCTAssertEqual(boundingBox.height, expectedBoundingBox.height) |
| 112 | + } |
| 113 | + |
| 114 | + /// Given: A `DetectedFace` |
| 115 | + /// When: when normalize is called with a view dimension |
| 116 | + /// Then: the normalized face calculates the correct landmark distances |
| 117 | + func testDetectedFaceNormalize() { |
| 118 | + let normalizedFace = detectedFace.normalize(width: normalizeWidth, height: normalizeHeight) |
| 119 | + XCTAssertEqual(normalizedFace.eyeCenterX, expectedNormalizeFace.eyeCenterX) |
| 120 | + XCTAssertEqual(normalizedFace.eyeCenterY, expectedNormalizeFace.eyeCenterY) |
| 121 | + XCTAssertEqual(normalizedFace.faceDistance, expectedNormalizeFace.faceDistance) |
| 122 | + XCTAssertEqual(normalizedFace.pupilDistance, expectedNormalizeFace.pupilDistance) |
| 123 | + XCTAssertEqual(normalizedFace.faceHeight, expectedNormalizeFace.faceHeight) |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments