@@ -20,7 +20,7 @@ public struct FaceLivenessDetectorView: View {
20
20
@State var displayingCameraPermissionsNeededAlert = false
21
21
22
22
let disableStartView : Bool
23
- let cameraPosition : LivenessCamera
23
+ let challengeOptions : ChallengeOptions
24
24
let onCompletion : ( Result < Void , FaceLivenessDetectionError > ) -> Void
25
25
26
26
let sessionTask : Task < FaceLivenessSession , Error >
@@ -30,14 +30,14 @@ public struct FaceLivenessDetectorView: View {
30
30
credentialsProvider: AWSCredentialsProvider ? = nil ,
31
31
region: String ,
32
32
disableStartView: Bool = false ,
33
- challengeOption : ChallengeOption ,
33
+ challengeOptions : ChallengeOptions ,
34
34
isPresented: Binding < Bool > ,
35
35
onCompletion: @escaping ( Result < Void , FaceLivenessDetectionError > ) -> Void
36
36
) {
37
37
self . disableStartView = disableStartView
38
38
self . _isPresented = isPresented
39
- self . cameraPosition = challengeOption. camera
40
39
self . onCompletion = onCompletion
40
+ self . challengeOptions = challengeOptions
41
41
42
42
self . sessionTask = Task {
43
43
let session = try await AWSPredictionsPlugin . startFaceLivenessSession (
@@ -59,31 +59,16 @@ public struct FaceLivenessDetectorView: View {
59
59
assetWriterDelegate: VideoChunker . AssetWriterDelegate ( ) ,
60
60
assetWriterInput: LivenessAVAssetWriterInput ( )
61
61
)
62
-
63
- let avCaptureDevice = AVCaptureDevice . default (
64
- . builtInWideAngleCamera,
65
- for: . video,
66
- position: cameraPosition == . front ? . front : . back)
67
-
68
- let captureSession = LivenessCaptureSession (
69
- captureDevice: . init( avCaptureDevice: avCaptureDevice) ,
70
- outputDelegate: OutputSampleBufferCapturer (
71
- faceDetector: faceDetector,
72
- videoChunker: videoChunker
73
- )
74
- )
75
62
76
63
self . _viewModel = StateObject (
77
64
wrappedValue: . init(
78
65
faceDetector: faceDetector,
79
66
faceInOvalMatching: faceInOvalStateMatching,
80
- captureSession: captureSession,
81
67
videoChunker: videoChunker,
82
68
closeButtonAction: { onCompletion ( . failure( . userCancelled) ) } ,
83
69
sessionID: sessionID,
84
70
isPreviewScreenEnabled: !disableStartView,
85
- cameraPosition: challengeOption. camera,
86
- challengeOption: challengeOption
71
+ challengeOptions: challengeOptions
87
72
)
88
73
)
89
74
}
@@ -93,15 +78,15 @@ public struct FaceLivenessDetectorView: View {
93
78
credentialsProvider: AWSCredentialsProvider ? = nil ,
94
79
region: String ,
95
80
disableStartView: Bool = false ,
96
- challengeOption : ChallengeOption ,
81
+ challengeOptions : ChallengeOptions ,
97
82
isPresented: Binding < Bool > ,
98
83
onCompletion: @escaping ( Result < Void , FaceLivenessDetectionError > ) -> Void ,
99
84
captureSession: LivenessCaptureSession
100
85
) {
101
86
self . disableStartView = disableStartView
102
87
self . _isPresented = isPresented
103
88
self . onCompletion = onCompletion
104
- self . cameraPosition = challengeOption . camera
89
+ self . challengeOptions = challengeOptions
105
90
106
91
self . sessionTask = Task {
107
92
let session = try await AWSPredictionsPlugin . startFaceLivenessSession (
@@ -121,13 +106,11 @@ public struct FaceLivenessDetectorView: View {
121
106
wrappedValue: . init(
122
107
faceDetector: captureSession. outputSampleBufferCapturer!. faceDetector,
123
108
faceInOvalMatching: faceInOvalStateMatching,
124
- captureSession: captureSession,
125
109
videoChunker: captureSession. outputSampleBufferCapturer!. videoChunker,
126
110
closeButtonAction: { onCompletion ( . failure( . userCancelled) ) } ,
127
111
sessionID: sessionID,
128
112
isPreviewScreenEnabled: !disableStartView,
129
- cameraPosition: challengeOption. camera,
130
- challengeOption: challengeOption
113
+ challengeOptions: challengeOptions
131
114
)
132
115
)
133
116
}
@@ -172,6 +155,14 @@ public struct FaceLivenessDetectorView: View {
172
155
. onAppear {
173
156
Task {
174
157
do {
158
+ let cameraPosition : LivenessCamera
159
+ switch challenge. type {
160
+ case . faceMovementAndLightChallenge:
161
+ cameraPosition = challengeOptions. faceMovementAndLightChallengeOption. camera
162
+ case . faceMovementChallenge:
163
+ cameraPosition = challengeOptions. faceMovementChallengeOption. camera
164
+ }
165
+
175
166
let newState = disableStartView
176
167
? DisplayState . displayingLiveness
177
168
: DisplayState . displayingGetReadyView ( challenge, cameraPosition)
@@ -255,7 +246,7 @@ public struct FaceLivenessDetectorView: View {
255
246
for: . video,
256
247
completionHandler: { accessGranted in
257
248
guard accessGranted == true else { return }
258
- guard let challenge = viewModel. challenge else { return }
249
+ guard let challenge = viewModel. challengeReceived else { return }
259
250
displayState = . awaitingLivenessSession( challenge)
260
251
}
261
252
)
@@ -274,7 +265,7 @@ public struct FaceLivenessDetectorView: View {
274
265
case . restricted, . denied:
275
266
alertCameraAccessNeeded ( )
276
267
case . authorized:
277
- guard let challenge = viewModel. challenge else { return }
268
+ guard let challenge = viewModel. challengeReceived else { return }
278
269
displayState = . awaitingLivenessSession( challenge)
279
270
@unknown default :
280
271
break
@@ -341,32 +332,38 @@ private func map(detectionCompletion: @escaping (Result<Void, FaceLivenessDetect
341
332
}
342
333
}
343
334
344
- enum LivenessCamera {
335
+ public enum LivenessCamera {
345
336
case front
346
337
case back
347
338
}
348
339
349
- public struct ChallengeOption {
340
+ public struct ChallengeOptions {
341
+ let faceMovementChallengeOption : FaceMovementChallengeOption
342
+ let faceMovementAndLightChallengeOption : FaceMovementAndLightChallengeOption
343
+
344
+ public init ( faceMovementChallengeOption: FaceMovementChallengeOption ,
345
+ faceMovementAndLightChallengeOption: FaceMovementAndLightChallengeOption ) {
346
+ self . faceMovementChallengeOption = faceMovementChallengeOption
347
+ self . faceMovementAndLightChallengeOption = faceMovementAndLightChallengeOption
348
+ }
349
+ }
350
+
351
+ public struct FaceMovementChallengeOption {
350
352
let challenge : Challenge
351
353
let camera : LivenessCamera
352
354
353
- init ( challenge : Challenge , camera: LivenessCamera ) {
354
- self . challenge = challenge
355
+ public init ( camera: LivenessCamera ) {
356
+ self . challenge = . init ( version : " 1.0.0 " , type : . faceMovementChallenge )
355
357
self . camera = camera
356
358
}
359
+ }
360
+
361
+ public struct FaceMovementAndLightChallengeOption {
362
+ let challenge : Challenge
363
+ let camera : LivenessCamera
357
364
358
- public static let faceMovementAndLightChallenge = Self . init (
359
- challenge: . init( version: " 2.0.0 " ,
360
- type: . faceMovementAndLightChallenge) ,
361
- camera: . front)
362
-
363
- public static let faceMovementChallengeWithFrontCamera = Self . init (
364
- challenge: . init( version: " 1.0.0 " ,
365
- type: . faceMovementAndLightChallenge) ,
366
- camera: . front)
367
-
368
- public static let faceMovementChallengeWithBackCamera = Self . init (
369
- challenge: . init( version: " 1.0.0 " ,
370
- type: . faceMovementAndLightChallenge) ,
371
- camera: . back)
365
+ public init ( ) {
366
+ self . challenge = . init( version: " 2.0.0 " , type: . faceMovementAndLightChallenge)
367
+ self . camera = . front
368
+ }
372
369
}
0 commit comments