@richard.fadiora/liveness-detection 4.2.11 → 4.2.13

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.
@@ -0,0 +1,294 @@
1
+ # Liveness Detection SDK
2
+
3
+ A cross-framework **liveness detection SDK** that performs randomized user challenges and verifies real-user presence via a backend anti-spoofing API. Works with **React** and **Angular** via framework-specific wrappers while the **core engine is framework-agnostic**.
4
+
5
+ This version introduces:
6
+
7
+ - **Headless mode** for fully custom UI
8
+ - Robust **challenge sequencing**
9
+ - **Configurable thresholds** for `Smile`, `Blink`, and `Turn_Head` challenges
10
+ - Sequential challenge execution with **strict timeout handling**
11
+ - Pause between steps for user feedback
12
+ - Cross-framework integration (React hook & Angular service)
13
+
14
+ ---
15
+
16
+ ## 📌 Overview
17
+
18
+ This SDK strengthens identity verification by combining:
19
+
20
+ - Randomized challenge-response validation
21
+ - Sequential challenge execution
22
+ - Strict timeout enforcement
23
+ - Backend spoof detection
24
+ - Callback-based integration for easy usage
25
+ - Headless / fully customizable UI via render props (React)
26
+ - Configurable challenge thresholds
27
+
28
+ Protects against:
29
+
30
+ - Presentation (photo) attacks
31
+ - Screen glare attacks
32
+ - Video replay/injection attacks
33
+
34
+ ---
35
+
36
+ ## ⚙️ Architecture
37
+
38
+ ### Core Engine
39
+
40
+ - `LivenessEngine` (framework-agnostic)
41
+ - Handles:
42
+ - Challenge sequence generation
43
+ - MediaPipe face & hand model detection
44
+ - Challenge validation (`Smile`, `Blink`, `Turn_Head`, `Thumbs_Up`)
45
+ - Face cropping
46
+ - Detection loop
47
+ - Final frame capture and backend verification
48
+ - Can be used directly or via React/Angular wrappers
49
+
50
+ ### Framework Wrappers
51
+
52
+ | Framework | Wrapper | Notes |
53
+ |-----------|--------|------|
54
+ | React | `useLiveness` hook | Uses `webcamRef` and React state, supports render-prop customization |
55
+ | Angular | `LivenessService` | Injectable service, exposes engine state and control methods. Must be imported with "@richard.fadiora/liveness-detection/angular |
56
+
57
+ ---
58
+
59
+ ## ⚙️ How It Works
60
+
61
+ ### 1️⃣ Challenge Initialization
62
+
63
+ - Randomly selects **3 challenges** from the pool: `Smile`, `Blink`, `Turn_Head`, `Thumbs_Up`.
64
+ - Timer starts immediately (default **60 seconds**, configurable via `duration`).
65
+ - Each challenge is verified sequentially, with a brief pause between steps for feedback.
66
+ - If the timer expires, the session terminates and no frames are sent to the backend.
67
+
68
+ ### 2️⃣ Challenge Execution
69
+
70
+ - Real-time validation using webcam input and MediaPipe models.
71
+ - Sequential verification ensures **no steps are skipped**.
72
+ - Developers can render custom UI via the **render prop** while using the same underlying logic.
73
+ - Challenge thresholds are configurable through props:
74
+
75
+ | Prop Name | Default | Description |
76
+ |-----------------------|---------|------------------------------------------|
77
+ | `smileThreshold` | 0.20 | Minimum smile width to count as valid |
78
+ | `blinkThreshold` | 0.01 | Maximum eye aspect ratio to count as blink |
79
+ | `minturnHeadThreshold`| 0.15 | Minimum yaw for right head turn detection |
80
+ | `maxturnHeadThreshold`| 0.85 | Maximum yaw for left head turn detection |
81
+
82
+ ### 3️⃣ Backend Liveness Verification
83
+
84
+ - Captures **5 frames** from the webcam after all challenges are complete.
85
+ - Frames sent to backend API (`apiUrl` prop).
86
+ - Backend performs spoof detection, glare detection, and video injection detection.
87
+
88
+ ---
89
+
90
+ ## ✅ Success & Failure Behavior
91
+
92
+ ### Success
93
+ - UI (or custom component) shows success feedback.
94
+ - `onComplete` callback is triggered:
95
+ ```js
96
+ onComplete({ success: true, image: frame, skinConfidence });
97
+ ```
98
+
99
+ ### Failure
100
+ - UI shows failure message.
101
+ - `onError` callback is triggered:
102
+ ```js
103
+ onError({ success: false, reason, skinConfidence: null });
104
+ ```
105
+ - Component resets; user must start a new session.
106
+
107
+ ---
108
+
109
+ ## 📦 Props
110
+
111
+ | Prop Name | Type | Required | Description |
112
+ |------------|-----------------------------|----------|-------------|
113
+ | `apiUrl` | `string` | Yes | Backend endpoint used for liveness verification |
114
+ | `onComplete` | `(result: object) => void` | Yes | Callback fired after verification succeeds |
115
+ | `onError` | `(result: object) => void` | Yes | Callback fired after verification fails |
116
+ | `duration` | `number` | No | Maximum time for all challenges (default: 60s) |
117
+ | `render` | `(sdk: object) => JSX.Element` | No | Optional render prop for full UI customization |
118
+ | `classNames` | `object` | No | Optional CSS class names to customize default UI |
119
+ | `smileThreshold` | `number` | No | Minimum smile width for Smile challenge |
120
+ | `blinkThreshold` | `number` | No | Maximum eye aspect ratio for Blink challenge |
121
+ | `minturnHeadThreshold` | `number` | No | Minimum yaw for Turn_Head challenge |
122
+ | `maxturnHeadThreshold` | `number` | No | Maximum yaw for Turn_Head challenge |
123
+
124
+
125
+ ---
126
+
127
+ ## 🧩 Usage Example
128
+
129
+ ### Default UI
130
+ ```jsx
131
+ import { LivenessSDK } from "@richard.fadiora/liveness-detection";
132
+
133
+ function App() {
134
+ return (
135
+ <LivenessSDK
136
+ apiUrl="https://your-backend-api.com/liveness-check"
137
+ onComplete={(result) => console.log("Success:", result)}
138
+ onError={(error) => console.log("Failed:", error.reason)}
139
+ duration={60}
140
+ />
141
+ );
142
+ }
143
+
144
+ export default App;
145
+ ```
146
+
147
+ ### Headless / Custom UI
148
+ ```jsx
149
+ <LivenessSDK
150
+ apiUrl="https://your-backend-api.com/liveness-check"
151
+ onComplete={handleComplete}
152
+ onError={handleError}
153
+ render={(sdk) => (
154
+ <div>
155
+ <video ref={sdk.webcamRef} autoPlay playsInline muted />
156
+ <div>Step {sdk.currentStep + 1}: {sdk.sequence[sdk.currentStep]}</div>
157
+ <div>Time left: {sdk.timeLeft}s</div>
158
+ <button onClick={sdk.start}>Start</button>
159
+ </div>
160
+ )}
161
+ />
162
+ ```
163
+ ## 📤 Hook Return Values
164
+
165
+ The `useLiveness` hook exposes the following values and control functions for managing the liveness detection session.
166
+
167
+ | Name | Type | Description |
168
+ |-----|------|-------------|
169
+ | `webcamRef` | `ref` | React ref attached to the webcam component. Provides access to the live video stream used for face and hand detection as well as frame capture for verification. |
170
+ | `status` | `string` | Represents the current state of the liveness session. Possible values include `loading`, `ready`, `capturing`, `verifying`, `success`, `error`, and `expired`. |
171
+ | `errorMsg` | `string` | Contains the error message displayed when the liveness verification fails or when the system encounters an issue. |
172
+ | `sequence` | `string[]` | The randomized sequence of liveness challenges selected for the current session. Three challenges are chosen from the challenge pool. |
173
+ | `currentStep` | `number` | The index of the current challenge being performed within the challenge sequence. |
174
+ | `timeLeft` | `number` | Remaining time (in seconds) before the session expires. The timer runs while the system is in the `capturing` state. |
175
+ | `isStepTransitioning` | `boolean` | Indicates whether the system is currently transitioning between challenges. Used to briefly pause detection and provide UI feedback after a challenge is completed. |
176
+ | `start` | `function` | Starts the liveness challenge session and begins the detection loop. |
177
+ | `reset` | `function` | Resets the entire session, including the timer, challenge sequence, step index, and error state. |
178
+ | `sendFinalProof` | `function` | Sends captured face frames to the backend verification API to perform the final liveness check. Normally triggered automatically after the last challenge is completed. |
179
+
180
+
181
+
182
+
183
+ ---
184
+
185
+ ## 🎨 Styling with `classNames`
186
+
187
+ You can pass a `classNames` object to override the default UI classes:
188
+ ```js
189
+ classNames={{
190
+ container: "liveness-container",
191
+ webcam: "liveness-webcam",
192
+ button: "liveness-button",
193
+ challenge: "liveness-challenge",
194
+ timer: "liveness-timer",
195
+ error: "liveness-error",
196
+ success: "liveness-success",
197
+ }}
198
+ ```
199
+
200
+ ### CSS Example
201
+ ```css
202
+ .liveness-container {
203
+ text-align: center;
204
+ background: #121212;
205
+ padding: 24px;
206
+ border-radius: 20px;
207
+ box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
208
+ color: white;
209
+ }
210
+ .liveness-webcam {
211
+ border-radius: 20px;
212
+ border: 2px solid #007bff;
213
+ width: 100%;
214
+ }
215
+ .liveness-button {
216
+ padding: 12px 30px;
217
+ font-size: 16px;
218
+ font-weight: bold;
219
+ border-radius: 10px;
220
+ background: #28a745;
221
+ color: white;
222
+ border: none;
223
+ cursor: pointer;
224
+ margin-top: 10px;
225
+ }
226
+ .liveness-challenge {
227
+ margin: 5px 0;
228
+ font-weight: bold;
229
+ font-size: 40px;
230
+ }
231
+ .liveness-timer {
232
+ margin: 10px 0;
233
+ font-weight: bold;
234
+ font-size: 18px;
235
+ }
236
+ .liveness-error {
237
+ color: #ff4d4d;
238
+ margin-top: 20px;
239
+ }
240
+ .liveness-success {
241
+ color: #4caf50;
242
+ margin-top: 20px;
243
+ }
244
+ ```
245
+
246
+ ---
247
+
248
+ ## ⏳ Timeout Rules
249
+
250
+ - Maximum session duration: set via `duration` prop (default 60s).
251
+ - On timeout: challenge stops, state resets, backend verification is not triggered.
252
+
253
+ ---
254
+
255
+ ## 🔐 Security Architecture
256
+
257
+ - **Client-side**: Randomized challenges, real-time gesture detection
258
+ - **Server-side**: Frame-based spoof analysis, glare detection, video injection detection
259
+ - **Session control**: Timeout enforcement, manual restart on failure
260
+ - Reduces false positives and prevents replay attacks.
261
+
262
+ ---
263
+
264
+ ## 📊 Verification Criteria
265
+
266
+ - All 3 challenges completed
267
+ - All 5 frames successfully sent to backend
268
+ - Backend confirms: no spoofing, no glare, human skin texture, no video injection
269
+
270
+ ---
271
+
272
+ ## 🏗️ Integration Notes
273
+
274
+ - Webcam permissions required
275
+ - Backend must accept 5 frames in expected format
276
+ - `apiUrl` must be reachable and have an endpoint `/v1/verify`
277
+ - CORS must be configured properly
278
+
279
+ ---
280
+
281
+ ## 🚀 Ideal Use Cases
282
+
283
+ - KYC verification flows
284
+ - Identity onboarding
285
+ - Account recovery
286
+ - Secure login
287
+ - Financial / compliance apps
288
+
289
+ ---
290
+
291
+ ## 👨‍💻 Maintainer
292
+
293
+ Fadiora Richard.
294
+