-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
365 lines (333 loc) · 12.1 KB
/
script.js
File metadata and controls
365 lines (333 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
document.addEventListener("DOMContentLoaded", () => {
const idCardInput = document.getElementById("id-card-image");
const liveFaceInput = document.getElementById("live-face-image");
const idCardPreview = document.getElementById("id-card-preview");
const liveFacePreview = document.getElementById("live-face-preview");
const verifyButton = document.getElementById("verify-button");
const progressSection = document.getElementById("progress-section");
const statusOverall = document.getElementById("status-overall");
const statusOverallText = statusOverall.querySelector(".status-text");
const statusOverallSpinner = statusOverall.querySelector(".spinner");
const stepIdCard = document.getElementById("step-id-card");
const stepLiveness = document.getElementById("step-liveness");
const stepFaceVerification = document.getElementById(
"step-face-verification"
);
const stepDatabase = document.getElementById("step-database");
const resultsSection = document.getElementById("results-section");
const finalStatusMessage = document.getElementById("final-status-message");
const extractedDetailsDisplay = document.getElementById(
"extracted-details-display"
);
const extractedDetailsJson = document.getElementById(
"extracted-details-json"
);
// --- Helper: Update a single step’s status (Pending / Processing / Success / Failed) ---
function updateStepStatus(
stepElement,
statusType,
icon,
mainMessage = "",
detailsText = ""
) {
const statusTextElement = stepElement.querySelector(".status-text");
const iconElement = stepElement.querySelector(".step-icon");
const detailsElement = stepElement.querySelector(".details");
// Clear out any of the old status- classes
stepElement.className = "";
if (statusType === "processing")
stepElement.classList.add("step-status-processing");
else if (statusType === "success")
stepElement.classList.add("step-status-success");
else if (statusType === "failed")
stepElement.classList.add("step-status-failed");
else if (statusType === "pending")
stepElement.classList.add("step-status-pending");
else if (statusType === "warning")
stepElement.classList.add("step-status-failed");
statusTextElement.textContent =
mainMessage || statusType.charAt(0).toUpperCase() + statusType.slice(1);
iconElement.textContent = icon;
if (detailsText && detailsText.trim() !== "") {
detailsElement.textContent = detailsText;
detailsElement.style.display = "block";
} else {
detailsElement.style.display = "none";
}
}
// --- Helper: Update overall status banner (Processing / Success / Failed) ---
function updateOverallStatus(statusType, message) {
statusOverallText.textContent = message;
statusOverallSpinner.style.display =
statusType === "processing" ? "inline-block" : "none";
statusOverall.className = "status-indicator"; // reset
if (statusType === "processing") statusOverall.classList.add("processing");
else if (statusType === "success") statusOverall.classList.add("success");
else if (statusType === "failed") statusOverall.classList.add("failed");
else if (statusType === "warning") statusOverall.classList.add("failed");
}
// --- Helper: Show final results area and populate it ---
function updateFinalResults(statusType, message, extractedDetails = null) {
resultsSection.style.display = "block";
finalStatusMessage.textContent = message;
finalStatusMessage.className = "";
if (statusType === "success") finalStatusMessage.classList.add("success");
else if (statusType === "failed")
finalStatusMessage.classList.add("failed");
else if (statusType === "warning")
finalStatusMessage.classList.add("warning");
if (
extractedDetails &&
typeof extractedDetails === "object" &&
Object.keys(extractedDetails).length > 0 &&
!extractedDetails.error
) {
extractedDetailsDisplay.style.display = "block";
extractedDetailsJson.textContent = JSON.stringify(
extractedDetails,
null,
2
);
} else {
extractedDetailsDisplay.style.display = "none";
extractedDetailsJson.textContent = "";
}
}
// --- Reset just the step banners and overall status (don’t clear inputs) ---
async function resetSteps() {
await new Promise((resolve) => setTimeout(resolve, 10000));
progressSection.style.display = "none";
resultsSection.style.display = "none";
extractedDetailsDisplay.style.display = "none";
updateStepStatus(stepIdCard, "pending", "⏳", "Pending");
updateStepStatus(stepLiveness, "pending", "⏳", "Pending");
updateStepStatus(stepFaceVerification, "pending", "⏳", "Pending");
updateStepStatus(stepDatabase, "pending", "⏳", "Pending");
updateOverallStatus("pending", "Not Started");
finalStatusMessage.textContent = "";
finalStatusMessage.className = "";
extractedDetailsJson.textContent = "";
verifyButton.disabled = false;
verifyButton.textContent = "Verify Identity";
}
// // --- “Hard” reset: clear everything (inputs, previews, and statuses) ---
// async function resetAll() {
// console.log("I am in this ");
// await new Promise((resolve) => setTimeout(resolve, 20000));
// resetSteps();
// idCardPreview.style.display = "none";
// idCardPreview.src = "#";
// liveFacePreview.style.display = "none";
// liveFacePreview.src = "#";
// idCardInput.value = "";
// liveFaceInput.value = "";
// }
// --- Set up thumbnail preview for both inputs ---
function setupImagePreview(inputElement, previewElement) {
inputElement.addEventListener("change", (event) => {
if (event.target.files && event.target.files[0]) {
const reader = new FileReader();
reader.onload = (e) => {
previewElement.src = e.target.result;
previewElement.style.display = "block";
};
reader.readAsDataURL(event.target.files[0]);
} else {
previewElement.style.display = "none";
previewElement.src = "#";
}
});
}
setupImagePreview(idCardInput, idCardPreview);
setupImagePreview(liveFaceInput, liveFacePreview);
// On initial page load, completely reset
// resetAll();
// --- Main click handler for “Verify Identity” ---
verifyButton.addEventListener("click", async (event) => {
event.preventDefault();
verifyButton.disabled = true;
verifyButton.textContent = "Loading";
console.log("1");
const idCardFile = idCardInput.files[0];
const liveFaceFile = liveFaceInput.files[0];
console.log("1");
if (!idCardFile || !liveFaceFile) {
alert("Please select both ID card and live face images.");
return;
}
// 2) Show progress UI & disable the button
progressSection.style.display = "block";
verifyButton.disabled = true;
verifyButton.textContent = "Processing...";
updateOverallStatus("processing", "Processing…");
updateStepStatus(stepIdCard, "processing", "⚙️", "Processing ID card…");
console.log("id_card_image", idCardFile);
console.log("live_Face_image", liveFaceFile);
const formData = new FormData();
formData.append("id_card_image", idCardFile);
formData.append("live_face_image", liveFaceFile);
try {
const response = await axios.post(
"http://localhost:5000/process_and_verify",
formData,
{
headers: {
"Content-Type": "multipart/form-data",
},
}
);
const data = response.data;
console.log("data", data);
// — Update each step based on response —
// 1. ID Card Processing
if (data.id_card_processing_status) {
if (
data.id_card_processing_status.toLowerCase().includes("successfully")
) {
updateStepStatus(
stepIdCard,
"success",
"✅",
"Processed",
data.id_card_processing_status
);
} else {
updateStepStatus(
stepIdCard,
"failed",
"❌",
"Failed",
data.id_card_processing_status
);
}
} else {
updateStepStatus(
stepIdCard,
"failed",
"❌",
"Status Missing",
"ID card processing status not returned."
);
}
// 2. Liveness Check
if (data.liveness_check) {
if (data.liveness_check.passed) {
updateStepStatus(
stepLiveness,
"success",
"✅",
"Passed",
data.liveness_check.status
);
} else {
updateStepStatus(
stepLiveness,
"failed",
"❌",
"Failed",
data.liveness_check.status || "Liveness check failed."
);
}
} else {
updateStepStatus(stepLiveness, "failed", "❌", "Not Performed");
}
// 3. Face Verification
if (data.face_verification) {
if (data.face_verification.verified) {
updateStepStatus(
stepFaceVerification,
"success",
"✅",
"Verified",
data.face_verification.status
);
} else {
updateStepStatus(
stepFaceVerification,
"failed",
"❌",
"Not Verified",
data.face_verification.status || "Face verification failed."
);
}
} else {
updateStepStatus(stepFaceVerification, "failed", "❌", "Not Performed");
}
// 4. Database Storage
if (data.database_storage) {
const isWarningDb =
data.overall_status &&
data.overall_status
.toLowerCase()
.includes("warning: database storage failed");
if (data.database_storage.stored) {
updateStepStatus(
stepDatabase,
"success",
"✅",
"Stored",
data.database_storage.message
);
} else if (isWarningDb) {
updateStepStatus(
stepDatabase,
"warning",
"⚠️",
"Storage Issue",
data.database_storage.message
);
} else {
updateStepStatus(
stepDatabase,
"failed",
"❌",
"Storage Failed",
data.database_storage.message
);
}
} else {
updateStepStatus(stepDatabase, "failed", "❌", "Not Attempted");
}
// — Final overall status (Success / Warning / Failed) —
let finalOverallType = "failed";
if (data.overall_status) {
const lower = data.overall_status.toLowerCase();
if (lower.includes("success") && !lower.includes("warning")) {
finalOverallType = "success";
} else if (lower.includes("warning")) {
finalOverallType = "warning";
}
}
updateOverallStatus(
finalOverallType,
data.overall_status || "Completed with unknown status."
);
updateFinalResults(
finalOverallType,
data.overall_status || "Completed with unknown status.",
data.text_details
);
} catch (error) {
console.error("Error during verification fetch or JSON parsing:", error);
const errorMsg = "A network or server error occurred. Please try again.";
updateOverallStatus("failed", "Error!");
updateFinalResults("failed", errorMsg);
// Mark all steps as failed/not performed
updateStepStatus(
stepIdCard,
"failed",
"❌",
"Error",
"Process interrupted"
);
updateStepStatus(stepLiveness, "failed", "❌", "Not Performed");
updateStepStatus(stepFaceVerification, "failed", "❌", "Not Performed");
updateStepStatus(stepDatabase, "failed", "❌", "Not Performed");
} finally {
// Re-enable button now that everything (UI and fetch) is done,
// but don’t auto-clear the inputs. Let the user click “Verify Identity” again if needed.
verifyButton.disabled = false;
verifyButton.textContent = "Verify Identity";
}
});
});