-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
351 lines (319 loc) · 16 KB
/
index.html
File metadata and controls
351 lines (319 loc) · 16 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
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>RAZR</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns"></script>
<script src="https://cdn.jsdelivr.net/npm/flowbite@2.4.1/dist/flowbite.min.js"></script>
</head>
<body class="bg-gray-900 text-gray-100 min-h-screen">
<!-- HEADER -->
<header class="bg-gray-900 border-b border-gray-800">
<nav class="mx-auto flex max-w-7xl items-center justify-between p-3 lg:px-3">
<div class="flex lg:flex-1">
<a href="#" class="-m-1.5 p-1.5 text-xl font-semibold text-blue-400">🐧 RAZR</a>
</div>
<div class="lg:flex lg:gap-x-12">
<a href="#" class="text-sm font-semibold text-white">Inicio</a>
<a href="#" class="text-sm font-semibold text-white">Info</a>
<a href="#" class="text-sm font-semibold text-white">Documentación</a>
</div>
<div class="lg:flex lg:flex-1 lg:justify-end">
<a href="#" class="text-sm font-semibold text-white">Login <span aria-hidden="true">→</span></a>
</div>
</nav>
</header>
<!-- MAIN -->
<main class="flex flex-col items-center justify-center px-4 py-6 space-y-6">
<!-- VIDEO -->
<div class="w-full max-w-4xl relative rounded-lg overflow-hidden border border-gray-700 shadow-md">
<div id="placeholder" role="status"
class="absolute inset-0 flex items-center justify-center bg-gray-800 animate-pulse z-10">
<svg class="w-10 h-10 text-gray-400" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 16 20">
<path d="M5 5V.13a2.96 2.96 0 0 0-1.293.749L.879 3.707A2.98 2.98 0 0 0 .13 5H5Z" />
<path
d="M14.066 0H7v5a2 2 0 0 1-2 2H0v11a1.97 1.97 0 0 0 1.934 2h12.132A1.97 1.97 0 0 0 16 18V2a1.97 1.97 0 0 0-1.934-2Z" />
</svg>
</div>
<video id="videoStream" autoplay muted playsinline
class="w-full object-contain transition-opacity duration-500 opacity-0 scale-x-[-1]"></video>
<div id="earOverlay"
class="absolute bottom-4 right-4 bg-gray-900/80 backdrop-blur-lg border border-gray-600 text-green-400 px-5 py-3 rounded-xl text-lg font-mono shadow-lg hidden transition-all duration-300">
EAR: <span id="earValue">0.000</span>
</div>
</div>
<!-- CONTROLES -->
<section class="w-full max-w-4xl mt-2 bg-gray-800/40 p-4 rounded-xl backdrop-blur-md border border-gray-700">
<div class="flex flex-wrap justify-center gap-6">
<!-- Dropdown Modo -->
<div>
<button id="dropdownModoBtn" data-dropdown-toggle="dropdownModo"
class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 inline-flex items-center">
Modo de Visualización
<svg class="w-2.5 h-2.5 ms-3" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 10 6">
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="m1 1 4 4 4-4" />
</svg>
</button>
<div id="dropdownModo"
class="z-10 hidden w-48 bg-white divide-y divide-gray-100 rounded-lg shadow-sm dark:bg-gray-700 dark:divide-gray-600">
<ul class="p-3 space-y-1 text-sm text-gray-700 dark:text-gray-200">
<li>
<div class="flex items-center p-2 hover:bg-gray-100 dark:hover:bg-gray-600">
<input id="modo-poligonos" type="radio" name="modo" value="poligonos" class="w-4 h-4 text-blue-600"
checked>
<label for="modo-poligonos" class="ms-2 dark:text-gray-300">Polígonos</label>
</div>
</li>
<li>
<div class="flex items-center p-2 hover:bg-gray-100 dark:hover:bg-gray-600">
<input id="modo-puntos" type="radio" name="modo" value="puntos" class="w-4 h-4 text-blue-600">
<label for="modo-puntos" class="ms-2 dark:text-gray-300">Puntos</label>
</div>
</li>
<li>
<div class="flex items-center p-2 hover:bg-gray-100 dark:hover:bg-gray-600">
<input id="modo-crudo" type="radio" name="modo" value="crudo" class="w-4 h-4 text-blue-600">
<label for="modo-crudo" class="ms-2 dark:text-gray-300">Crudo</label>
</div>
</li>
</ul>
</div>
</div>
<!-- Dropdown Calidad -->
<div>
<button id="dropdownCalidadBtn" data-dropdown-toggle="dropdownCalidad"
class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 inline-flex items-center">
Calidad de Video
<svg class="w-2.5 h-2.5 ms-3" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 10 6">
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="m1 1 4 4 4-4" />
</svg>
</button>
<div id="dropdownCalidad"
class="z-10 hidden w-48 bg-white divide-y divide-gray-100 rounded-lg shadow-sm dark:bg-gray-700 dark:divide-gray-600">
<ul class="p-3 space-y-1 text-sm text-gray-700 dark:text-gray-200">
<li><div class="flex items-center p-2 hover:bg-gray-100 dark:hover:bg-gray-600"><input type="radio" id="q240" name="calidad" value="240" class="w-4 h-4 text-blue-600"><label for="q240" class="ms-2">240p</label></div></li>
<li><div class="flex items-center p-2 hover:bg-gray-100 dark:hover:bg-gray-600"><input type="radio" id="q480" name="calidad" value="480" checked class="w-4 h-4 text-blue-600"><label for="q480" class="ms-2">480p</label></div></li>
<li><div class="flex items-center p-2 hover:bg-gray-100 dark:hover:bg-gray-600"><input type="radio" id="q720" name="calidad" value="720" class="w-4 h-4 text-blue-600"><label for="q720" class="ms-2">720p</label></div></li>
<li><div class="flex items-center p-2 hover:bg-gray-100 dark:hover:bg-gray-600"><input type="radio" id="q1080" name="calidad" value="1080" class="w-4 h-4 text-blue-600"><label for="q1080" class="ms-2">1080p</label></div></li>
</ul>
</div>
</div>
</div>
<p id="resInfo" class="text-center text-xs text-gray-400 mt-3"></p>
</section>
<!-- CHART -->
<div id="chartContainer" class="w-full max-w-4xl relative border border-gray-700 rounded-lg shadow-md p-3">
<canvas id="earChart" class="h-32"></canvas>
</div>
</main>
<!-- FOOTER -->
<footer class="bg-gray-800 border-t border-gray-700 mt-6">
<div class="w-full mx-auto max-w-screen-xl p-4 flex items-center justify-between text-sm text-gray-400">
<span>© 2025 Los Andes Labs. Todos los derechos reservados.</span>
<span>Versión 0.2</span>
</div>
</footer>
<!-- MODAL -->
<div id="permisoModal" tabindex="-1"
class="fixed top-0 left-0 right-0 z-50 w-full p-4 overflow-x-hidden overflow-y-auto h-full bg-black/60 backdrop-blur-sm">
<div class="relative w-full max-w-md mx-auto mt-32">
<div class="relative bg-gray-800 rounded-lg shadow-lg border border-gray-700">
<div class="p-6 text-center">
<h3 class="mb-5 text-lg font-semibold text-white">Permisos requeridos</h3>
<p class="mb-4 text-gray-400 text-sm">El sistema necesita permiso de cámara y audio para operar.</p>
<button id="permitirBtn"
class="text-white bg-blue-600 hover:bg-blue-700 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5">
Permitir cámara y audio
</button>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/camera_utils/camera_utils.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/drawing_utils/drawing_utils.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/face_mesh/face_mesh.js"></script>
<script>
document.addEventListener("DOMContentLoaded", () => {
const videoOut = document.getElementById("videoStream");
const earOverlay = document.getElementById("earOverlay");
const earValue = document.getElementById("earValue");
const placeholder = document.getElementById("placeholder");
const resInfo = document.getElementById("resInfo");
const permisoModal = document.getElementById("permisoModal");
const permitirBtn = document.getElementById("permitirBtn");
let modoVisual = "puntos";
let selectedQuality = "480";
let aspectRatio = 4 / 3;
let camera = null;
let firstDraw = false;
let fps = 30;
const UMBRAL_EAR = 0.16;
let TIEMPO_ALERTA_MS = 1500;
let tiempoInferior = null;
let alarmaActiva = false;
const beep = new Audio("alarm.mp3");
beep.preload = "auto";
beep.load();
permitirBtn.addEventListener("click", async () => {
try {
const constraints = {
audio: false,
video: { facingMode: { ideal: "user" }, width: { ideal: 640 }, height: { ideal: 480 } }
};
const stream = await navigator.mediaDevices.getUserMedia(constraints);
stream.getTracks().forEach(t => t.stop());
permisoModal.classList.add("hidden");
iniciarSistema();
} catch (err) {
console.error("❌ Error al acceder a la cámara:", err);
resInfo.textContent = "Permiso de cámara denegado o en uso.";
}
});
async function iniciarSistema() {
async function detectarAspectoYFPS() {
try {
const stream = await navigator.mediaDevices.getUserMedia({ video: true });
const track = stream.getVideoTracks()[0];
const s = track.getSettings();
if (s.width && s.height) aspectRatio = s.width / s.height;
fps = s.frameRate ? Math.min(s.frameRate, 60) : 30;
resInfo.textContent = `Relación: ${aspectRatio.toFixed(2)} | ${s.width}x${s.height} @${fps.toFixed(0)}fps`;
track.stop();
} catch {
resInfo.textContent = "No se pudo detectar cámara.";
}
}
await detectarAspectoYFPS();
const ctx = document.getElementById("earChart").getContext("2d");
const MAX_POINTS = 100;
const chart = new Chart(ctx, {
type: "line",
data: { labels: [], datasets: [
{ label: "EAR", data: [], borderColor: "#3b82f6", backgroundColor: "rgba(59,130,246,0.25)", fill: true },
{ label: "Umbral (0.16)", data: [], borderColor: "#ef4444", borderDash: [5,5], fill: false }
]},
options: { responsive: true, animation: false,
scales: { y: { min: 0, max: 0.4, ticks: { color: "#d4d4d4" } },
x: { type: "time", time: { unit: "second" }, ticks: { color: "#d4d4d4" } } },
plugins: { legend: { labels: { color: "#f3f4f6" } } }
}
});
function pushToChart(ear){
const t = new Date();
chart.data.labels.push(t);
chart.data.datasets[0].data.push(ear);
chart.data.datasets[1].data.push(UMBRAL_EAR);
if(chart.data.labels.length>MAX_POINTS){
chart.data.labels.shift();chart.data.datasets[0].data.shift();chart.data.datasets[1].data.shift();
}
chart.update("none");
}
const calcularEAR=(ojo)=>{
const d=(i,j)=>Math.hypot(ojo[i].x-ojo[j].x,ojo[i].y-ojo[j].y);
const A=d(1,5),B=d(2,4),C=d(0,3);
return C?(A+B)/(2*C):0;
};
const displayCanvas=document.createElement("canvas");
const dctx=displayCanvas.getContext("2d",{alpha:false});
const faceMesh=new FaceMesh({locateFile:(f)=>`https://cdn.jsdelivr.net/npm/@mediapipe/face_mesh/${f}`});
faceMesh.setOptions({maxNumFaces:1,refineLandmarks:true});
const indicesOjoIzq=[33,160,158,133,153,144];
const indicesOjoDer=[362,385,387,263,373,380];
faceMesh.onResults((results)=>{
if(!results.multiFaceLandmarks?.length)return;
const img=results.image;
if(!firstDraw){
displayCanvas.width=img.width;displayCanvas.height=img.height;
videoOut.srcObject=displayCanvas.captureStream(fps);
placeholder.classList.add("hidden");earOverlay.classList.remove("hidden");
videoOut.style.opacity=1;firstDraw=true;
}
dctx.save();dctx.scale(-1,1);dctx.translate(-displayCanvas.width,0);
dctx.clearRect(0,0,displayCanvas.width,displayCanvas.height);
dctx.drawImage(img,0,0,displayCanvas.width,displayCanvas.height);
const lm=results.multiFaceLandmarks[0];
if(modoVisual==="poligonos"){
drawConnectors(dctx,lm,FACEMESH_LEFT_EYE,{color:"#00ffff",lineWidth:1.5});
drawConnectors(dctx,lm,FACEMESH_RIGHT_EYE,{color:"#00ffff",lineWidth:1.5});
}else if(modoVisual==="puntos"){
dctx.fillStyle="#ff0055";
[...indicesOjoIzq,...indicesOjoDer].forEach(i=>{
const p=lm[i];
dctx.beginPath();dctx.arc(p.x*displayCanvas.width,p.y*displayCanvas.height,3,0,2*Math.PI);dctx.fill();
});
}
dctx.restore();
const toPx=(p)=>({x:(1-p.x)*displayCanvas.width,y:p.y*displayCanvas.height});
const ojoIzq=indicesOjoIzq.map(i=>toPx(lm[i]));
const ojoDer=indicesOjoDer.map(i=>toPx(lm[i]));
const earProm=(calcularEAR(ojoIzq)+calcularEAR(ojoDer))/2.0;
earValue.textContent=earProm.toFixed(3);
pushToChart(earProm);
if(earProm<UMBRAL_EAR){
if(!tiempoInferior)tiempoInferior=Date.now();
const dur=Date.now()-tiempoInferior;
earOverlay.classList.remove("text-green-400");
earOverlay.classList.add("text-red-500","border-red-500");
if(!alarmaActiva && dur>=TIEMPO_ALERTA_MS){
alarmaActiva=true;
beep.currentTime=0;
beep.play().catch(e=>console.warn(e));
setTimeout(()=>{beep.pause();beep.currentTime=0;},3000);
}
}else{
tiempoInferior=null;alarmaActiva=false;
earOverlay.classList.remove("text-red-500","border-red-500");
earOverlay.classList.add("text-green-400");
}
});
function calcularDimensiones(q){
const res={"240":426,"480":854,"720":1280,"1080":1920};
const w=res[q]||640;const h=Math.round(w/aspectRatio);
return{width:w,height:h};
}
// === Reescalado cacheado para móviles ===
function reiniciarCamara(){
if(camera)camera.stop();
const {width,height}=calcularDimensiones(selectedQuality);
const inputVideo=document.createElement("video");
inputVideo.muted=true;inputVideo.playsInline=true;
const isMobile=/Android|iPhone|iPad|iPod/i.test(navigator.userAgent);
let tempCanvas=null,tctx=null;
if(isMobile){
tempCanvas=document.createElement("canvas");
tempCanvas.width=width;
tempCanvas.height=height;
tctx=tempCanvas.getContext("2d");
}
camera=new Camera(inputVideo,{
onFrame:async()=>{
if(isMobile){
tctx.drawImage(inputVideo,0,0,width,height);
await faceMesh.send({image:tempCanvas});
}else{
await faceMesh.send({image:inputVideo});
}
},
width:isMobile?undefined:width,
height:isMobile?undefined:height
});
camera.start();
displayCanvas.width=width;
displayCanvas.height=height;
resInfo.textContent=isMobile
?`Render móvil simulado: ${width}x${height} (reescala activo)`
:`Calidad: ${width}x${height} | ${fps.toFixed(0)}fps`;
}
document.querySelectorAll("input[name='modo']").forEach(r=>r.addEventListener("change",()=>modoVisual=r.value));
document.querySelectorAll("input[name='calidad']").forEach(r=>r.addEventListener("change",()=>{selectedQuality=r.value;reiniciarCamara();}));
reiniciarCamara();
}
});
</script>
</body>
</html>