-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv1.html
449 lines (390 loc) · 13.7 KB
/
v1.html
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Smart Face Tracker Pro</title>
<!-- Libraries -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/[email protected]"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
<style>
:root {
--primary-color: #2196F3;
--success-color: #4CAF50;
--warning-color: #FFC107;
--danger-color: #F44336;
--bg-color: #1a1a1a;
--card-bg: #2d2d2d;
--text-color: #ffffff;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
body {
background-color: var(--bg-color);
color: var(--text-color);
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
.header {
text-align: center;
margin-bottom: 30px;
padding: 20px;
background: linear-gradient(135deg, #1a1a1a 0%, #2d2d2d 100%);
border-radius: 15px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.header h1 {
font-size: 2.5em;
margin-bottom: 10px;
background: linear-gradient(45deg, var(--primary-color), #64B5F6);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.header p {
color: #888;
font-size: 1.1em;
}
.main-content {
display: grid;
grid-template-columns: 1fr 300px;
gap: 20px;
margin-top: 20px;
}
.video-container {
position: relative;
background: var(--card-bg);
border-radius: 15px;
padding: 20px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.video-wrapper {
position: relative;
width: 100%;
aspect-ratio: 4/3;
overflow: hidden;
border-radius: 10px;
background: #000;
}
video {
position: absolute;
width: 100%;
height: 100%;
object-fit: cover;
transform: scaleX(-1);
}
canvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
transform: scaleX(-1);
}
.metrics-panel {
background: var(--card-bg);
border-radius: 15px;
padding: 20px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.metric-card {
background: rgba(255, 255, 255, 0.05);
border-radius: 10px;
padding: 15px;
margin-bottom: 15px;
}
.metric-card h3 {
font-size: 0.9em;
color: #888;
margin-bottom: 10px;
}
.metric-value {
font-size: 2em;
font-weight: bold;
margin: 10px 0;
}
.status-indicator {
display: flex;
align-items: center;
gap: 10px;
padding: 10px;
border-radius: 8px;
background: rgba(255, 255, 255, 0.05);
margin-top: 10px;
}
.status-dot {
width: 10px;
height: 10px;
border-radius: 50%;
background-color: var(--success-color);
animation: pulse 2s infinite;
}
.status-dot.inactive {
background-color: var(--danger-color);
}
.gauge {
width: 200px;
height: 100px;
margin: 0 auto;
position: relative;
overflow: hidden;
}
.gauge-fill {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
background: linear-gradient(to top, var(--primary-color), #64B5F6);
transition: height 0.3s ease;
}
@keyframes pulse {
0% {
opacity: 1;
}
50% {
opacity: 0.5;
}
100% {
opacity: 1;
}
}
.tips {
background: rgba(33, 150, 243, 0.1);
border-left: 4px solid var(--primary-color);
padding: 15px;
margin-top: 20px;
border-radius: 0 10px 10px 0;
}
.quality-bar {
height: 6px;
background: rgba(255, 255, 255, 0.1);
border-radius: 3px;
margin-top: 10px;
overflow: hidden;
}
.quality-fill {
height: 100%;
background: linear-gradient(90deg, var(--danger-color) 0%, var(--warning-color) 50%, var(--success-color) 100%);
transition: width 0.3s ease;
}
@media (max-width: 1024px) {
.main-content {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>Smart Face Tracker Pro</h1>
<p>Advanced Real-time Face Detection & Analysis</p>
</div>
<div class="main-content">
<div class="video-container">
<div class="video-wrapper">
<video id="video" autoplay playsinline></video>
<canvas id="canvas"></canvas>
</div>
<div class="status-indicator">
<div id="statusDot" class="status-dot"></div>
<span id="status">Initializing system...</span>
</div>
</div>
<div class="metrics-panel">
<div class="metric-card">
<h3>FACE POSITION SCORE</h3>
<div class="metric-value" id="scoreValue">0</div>
<div class="quality-bar">
<div class="quality-fill" id="qualityFill" style="width: 0%"></div>
</div>
</div>
<div class="metric-card">
<h3>FACE DETECTION</h3>
<div class="gauge">
<div class="gauge-fill" id="detectionGauge" style="height: 0%"></div>
</div>
</div>
<div class="metric-card">
<h3>SYSTEM STATUS</h3>
<div id="systemStatus">
<div class="tips" id="tips">
Position your face in the center of the frame
</div>
</div>
</div>
</div>
</div>
</div>
<script>
const video = document.getElementById('video');
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const scoreValue = document.getElementById('scoreValue');
const statusElement = document.getElementById('status');
const statusDot = document.getElementById('statusDot');
const qualityFill = document.getElementById('qualityFill');
const detectionGauge = document.getElementById('detectionGauge');
const tips = document.getElementById('tips');
let model;
let isRunning = false;
// Set initial canvas size
canvas.width = 640;
canvas.height = 480;
function updateStatus(message, type = 'success') {
statusElement.textContent = message;
statusDot.className = 'status-dot' + (type === 'error' ? ' inactive' : '');
}
function updateTips(score) {
if (score > 80) {
tips.textContent = "Perfect position! Hold steady.";
tips.style.borderColor = 'var(--success-color)';
} else if (score > 60) {
tips.textContent = "Good position. Try to center your face more.";
tips.style.borderColor = 'var(--warning-color)';
} else {
tips.textContent = "Move closer to the center and ensure good lighting.";
tips.style.borderColor = 'var(--danger-color)';
}
}
async function setupCamera() {
try {
const stream = await navigator.mediaDevices.getUserMedia({
video: {
width: 640,
height: 480,
facingMode: 'user',
}
});
video.srcObject = stream;
updateStatus('Camera initialized');
return new Promise((resolve) => {
video.onloadedmetadata = () => resolve(video);
});
} catch (error) {
updateStatus('Camera access error: ' + error.message, 'error');
throw error;
}
}
async function loadModel() {
try {
updateStatus('Loading detection model...');
model = await blazeface.load();
updateStatus('System ready');
return model;
} catch (error) {
updateStatus('Model loading error: ' + error.message, 'error');
throw error;
}
}
function drawBox(prediction) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw detection box with gradient
const gradient = ctx.createLinearGradient(
prediction.topLeft[0],
prediction.topLeft[1],
prediction.bottomRight[0],
prediction.bottomRight[1]
);
gradient.addColorStop(0, '#2196F3');
gradient.addColorStop(1, '#64B5F6');
ctx.strokeStyle = gradient;
ctx.lineWidth = 3;
ctx.beginPath();
ctx.roundRect(
prediction.topLeft[0],
prediction.topLeft[1],
prediction.bottomRight[0] - prediction.topLeft[0],
prediction.bottomRight[1] - prediction.topLeft[1],
10
);
ctx.stroke();
// Draw landmarks
ctx.fillStyle = '#FFC107';
prediction.landmarks.forEach(landmark => {
ctx.beginPath();
ctx.arc(landmark[0], landmark[1], 4, 0, 2 * Math.PI);
ctx.fill();
});
}
function calculateScore(prediction) {
const faceX = (prediction.topLeft[0] + prediction.bottomRight[0]) / 2;
const faceY = (prediction.topLeft[1] + prediction.bottomRight[1]) / 2;
const idealX = canvas.width / 2;
const idealY = canvas.height / 2;
const distance = Math.sqrt(
Math.pow(faceX - idealX, 2) +
Math.pow(faceY - idealY, 2)
);
const maxDistance = Math.sqrt(
Math.pow(canvas.width / 2, 2) +
Math.pow(canvas.height / 2, 2)
);
let score = 100 - (distance / maxDistance * 100);
const faceWidth = prediction.bottomRight[0] - prediction.topLeft[0];
const idealWidth = canvas.width / 3;
const sizeFactor = 100 - Math.abs(faceWidth - idealWidth) / idealWidth * 50;
return Math.round((score + sizeFactor) / 2);
}
async function detectFace() {
if (!model || !isRunning) return;
try {
const predictions = await model.estimateFaces(video, false);
if (predictions.length > 0) {
const prediction = predictions[0];
drawBox(prediction);
const score = calculateScore(prediction);
scoreValue.textContent = score;
qualityFill.style.width = `${score}%`;
detectionGauge.style.height = `${score}%`;
updateTips(score);
if (score > 80) {
updateStatus('Perfect position!');
} else if (score > 60) {
updateStatus('Good position');
} else {
updateStatus('Adjust position');
}
} else {
ctx.clearRect(0, 0, canvas.width, canvas.height);
updateStatus('No face detected', 'error');
scoreValue.textContent = '0';
qualityFill.style.width = '0%';
detectionGauge.style.height = '0%';
updateTips(0);
}
} catch (error) {
console.error('Detection error:', error);
updateStatus('Detection error occurred', 'error');
}
requestAnimationFrame(detectFace);
}
async function main() {
try {
await setupCamera();
await loadModel();
isRunning = true;
detectFace();
} catch (error) {
console.error('Initialization error:', error);
updateStatus('System initialization failed', 'error');
}
}
video.addEventListener('play', () => {
if (!isRunning) {
isRunning = true;
detectFace();
}
});
main();
</script>
</body>
</html>