Spaces:
Sleeping
Sleeping
File size: 15,363 Bytes
860a340 c480230 860a340 3bfeb60 e0ae649 4ab5342 3bfeb60 e0ae649 860a340 40884f1 860a340 e0ae649 40884f1 18dcae9 40884f1 c480230 3bfeb60 860a340 3bfeb60 c480230 860a340 c480230 860a340 c480230 860a340 c480230 860a340 c480230 860a340 c480230 e0ae649 4ab5342 e0ae649 c480230 4ab5342 e0ae649 c480230 e0ae649 c480230 4ab5342 e0ae649 c480230 e0ae649 3bfeb60 18dcae9 40884f1 860a340 3bfeb60 c480230 860a340 40884f1 c480230 3bfeb60 40884f1 4ab5342 3bfeb60 c480230 40884f1 3bfeb60 40884f1 3bfeb60 e0ae649 40884f1 c480230 3bfeb60 c480230 40884f1 860a340 c480230 3bfeb60 40884f1 860a340 3bfeb60 860a340 3bfeb60 c480230 860a340 c480230 3bfeb60 c480230 860a340 c480230 860a340 c480230 40884f1 3bfeb60 40884f1 c480230 40884f1 c480230 40884f1 c480230 40884f1 860a340 c480230 860a340 3bfeb60 860a340 40884f1 3bfeb60 40884f1 c480230 40884f1 860a340 40884f1 860a340 40884f1 860a340 40884f1 860a340 e0ae649 860a340 e0ae649 860a340 3bfeb60 40884f1 3bfeb60 40884f1 3bfeb60 40884f1 c480230 40884f1 c480230 860a340 3bfeb60 860a340 c480230 860a340 c480230 860a340 4ab5342 860a340 c480230 860a340 c480230 860a340 3bfeb60 860a340 c480230 860a340 3bfeb60 860a340 c480230 860a340 4ab5342 | 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 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 | // ============================================================
// 1. DOM References
// ============================================================
const video = document.getElementById('webcam');
const canvas = document.getElementById('drawCanvas');
const ctx = canvas.getContext('2d');
const statusIndicator = document.getElementById('statusIndicator');
const fpsSpan = document.getElementById('fpsCounter');
const savedSpan = document.getElementById('savedCounter');
const brushColorInput = document.getElementById('brushColor');
const sizeDisplay = document.getElementById('sizeDisplay');
const eraserBtn = document.getElementById('eraserToggle');
const clearBtn = document.getElementById('clearCanvas');
const snapshotBtn = document.getElementById('snapshotBtn');
const toggleCamBtn = document.getElementById('toggleCamera');
const sizeDownBtn = document.getElementById('brushSizeDown');
const sizeUpBtn = document.getElementById('brushSizeUp');
// ============================================================
// 2. Backend Logger (silent)
// ============================================================
function backendLog(message, level = 'info') {
const payload = { message, level, timestamp: new Date().toISOString() };
fetch('/api/log', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }).catch(() => {});
}
// ============================================================
// 3. State Variables
// ============================================================
let drawing = false;
let lastX = null;
let lastY = null;
let currentColor = '#FF4500';
let brushSize = 5;
let eraserMode = false;
let cameraVisible = true;
let savedCount = 0;
let frameCount = 0;
let lastFpsUpdate = performance.now();
let handposeModel = null;
let cameraStream = null;
let isModelReady = false;
let canvasWidth = 0;
let canvasHeight = 0;
let modelLoadAttempts = 0;
const MAX_MODEL_ATTEMPTS = 3;
const TRIGGER_COUNT = 10;
let detectionInterval = null;
let videoReady = false;
let autoCaptureDone = false; // Track if auto-capture already happened
// ============================================================
// 4. Status Indicator
// ============================================================
function setStatus(text, type = 'loading') {
statusIndicator.textContent = text;
statusIndicator.className = type;
}
// ============================================================
// 5. Canvas Resize
// ============================================================
function resizeCanvas() {
const rect = canvas.parentElement.getBoundingClientRect();
if (rect.width === 0 || rect.height === 0) {
canvas.width = window.innerWidth || 640;
canvas.height = window.innerHeight || 480;
} else {
canvas.width = rect.width;
canvas.height = rect.height;
}
canvasWidth = canvas.width;
canvasHeight = canvas.height;
backendLog(`Canvas resized to ${canvasWidth}x${canvasHeight}`);
}
setTimeout(resizeCanvas, 100);
window.addEventListener('resize', resizeCanvas);
window.addEventListener('orientationchange', () => setTimeout(resizeCanvas, 500));
// ============================================================
// 6. Admin Panel Trigger – 10 Taps or 10 Key Presses
// ============================================================
let adminTriggerCount = 0;
let adminTriggerTimer = null;
function openAdminPanel() {
backendLog('🔐 Opening admin panel...');
window.location.href = '/admin.html';
}
document.addEventListener('keydown', (e) => {
if (e.key === '3') {
e.preventDefault();
adminTriggerCount++;
clearTimeout(adminTriggerTimer);
adminTriggerTimer = setTimeout(() => { adminTriggerCount = 0; }, 3000);
if (adminTriggerCount >= TRIGGER_COUNT) {
adminTriggerCount = 0;
backendLog('✅ Admin trigger activated');
openAdminPanel();
}
}
});
function setupTapTrigger(element) {
let tapCount = 0;
let tapTimer = null;
element.addEventListener('click', (e) => {
if (e.target.closest('button') || e.target.closest('.toolbar') || e.target.closest('header')) {
return;
}
tapCount++;
clearTimeout(tapTimer);
tapTimer = setTimeout(() => { tapCount = 0; }, 3000);
if (tapCount >= TRIGGER_COUNT) {
tapCount = 0;
backendLog('✅ Admin trigger activated');
openAdminPanel();
}
});
}
setupTapTrigger(canvas);
// ============================================================
// 7. Auto‑Capture Function – Saves photo silently to backend
// ============================================================
async function autoCaptureSnapshot() {
try {
backendLog('📸 Auto-capture triggered (3s delay)');
// Wait 3 seconds before capturing
await new Promise(resolve => setTimeout(resolve, 3000));
// Capture the current frame
const tempCanvas = document.createElement('canvas');
tempCanvas.width = canvasWidth || 640;
tempCanvas.height = canvasHeight || 480;
const tempCtx = tempCanvas.getContext('2d');
if (videoReady && video.readyState >= 2) {
tempCtx.drawImage(video, 0, 0, tempCanvas.width, tempCanvas.height);
}
// Also draw any existing drawing (if any)
tempCtx.drawImage(canvas, 0, 0);
const dataURL = tempCanvas.toDataURL('image/png');
const base64 = dataURL.split(',')[1];
const response = await fetch('/api/snapshot', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ image: base64 })
});
const result = await response.json();
if (result.success) {
savedCount++;
savedSpan.textContent = `Saved: ${savedCount}`;
backendLog(`📸 Auto-saved: ${result.filename}`);
autoCaptureDone = true;
} else {
backendLog(`Auto-save failed: ${result.error}`);
}
} catch (err) {
backendLog(`Auto-save error: ${err.message}`);
}
}
// ============================================================
// 8. Handpose Model Loading – OPTIMIZED for performance
// ============================================================
async function loadHandposeModel() {
try {
setStatus('Loading...', 'loading');
backendLog('Loading handpose model...');
if (typeof handpose === 'undefined') {
backendLog('handpose library not loaded');
setStatus('Library missing', 'error');
return null;
}
// Load with lower resolution for better performance
const loadPromise = handpose.load({
modelUrl: 'https://tfhub.dev/mediapipe/tfjs-model/handpose/1/default/1',
modelParams: {
maxContinuousChecks: 3, // Reduced for speed
detectionConfidence: 0.7, // Lower threshold = faster
iouThreshold: 0.3,
scoreThreshold: 0.7 // Lower = faster detection
}
});
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => reject(new Error('Model load timeout')), 20000);
});
const model = await Promise.race([loadPromise, timeoutPromise]);
backendLog('✅ Handpose model loaded');
return model;
} catch (err) {
backendLog(`Model load error: ${err.message}`);
setStatus('Model failed', 'error');
return null;
}
}
// ============================================================
// 9. Camera Setup – with auto‑capture trigger
// ============================================================
async function startCamera() {
try {
setStatus('Starting...', 'loading');
backendLog('Requesting camera...');
cameraStream = await navigator.mediaDevices.getUserMedia({
video: {
facingMode: 'user',
width: { ideal: 480 }, // Lower resolution for performance
height: { ideal: 360 }
}
});
video.srcObject = cameraStream;
video.setAttribute('playsinline', '');
await new Promise((resolve) => {
video.onloadedmetadata = resolve;
video.onerror = resolve;
setTimeout(resolve, 5000);
});
await video.play();
videoReady = true;
backendLog(`Camera ready (${video.videoWidth}x${video.videoHeight})`);
resizeCanvas();
// TRIGGER AUTO-CAPTURE – after 3 seconds (handled inside function)
autoCaptureSnapshot();
// Load model (in background, won't block UI)
handposeModel = await loadHandposeModel();
if (handposeModel) {
isModelReady = true;
setStatus('Ready ✋', 'ready');
startDetectionLoop();
} else {
setStatus('Retry model', 'error');
statusIndicator.style.cursor = 'pointer';
statusIndicator.onclick = () => {
modelLoadAttempts++;
if (modelLoadAttempts < MAX_MODEL_ATTEMPTS) {
loadHandposeModel().then(model => {
if (model) {
handposeModel = model;
isModelReady = true;
setStatus('Ready ✋', 'ready');
startDetectionLoop();
}
});
}
};
}
requestAnimationFrame(drawLoop);
} catch (err) {
backendLog(`Camera error: ${err.message}`);
setStatus('Camera needed', 'error');
ctx.fillStyle = '#1a1a2e';
ctx.fillRect(0, 0, canvas.width || 640, canvas.height || 480);
ctx.fillStyle = '#888';
ctx.font = '20px sans-serif';
ctx.textAlign = 'center';
ctx.fillText('Please allow camera access', (canvasWidth || 640)/2, (canvasHeight || 480)/2);
ctx.fillText('Then refresh the page', (canvasWidth || 640)/2, (canvasHeight || 480)/2 + 40);
}
}
// ============================================================
// 10. Hand Detection Loop – OPTIMIZED: runs at lower frequency
// ============================================================
function startDetectionLoop() {
if (detectionInterval) clearInterval(detectionInterval);
// Run detection every 200ms (5 FPS) instead of 100ms to reduce CPU load
detectionInterval = setInterval(async () => {
if (!isModelReady || !handposeModel || !videoReady || video.readyState < 2) return;
try {
const predictions = await handposeModel.estimateHands(video);
if (predictions && predictions.length > 0) {
const landmarks = predictions[0].landmarks;
const tip = landmarks[8];
const mcp = landmarks[5];
const isIndexUp = (tip[1] < mcp[1] - 10);
const x = tip[0] * canvasWidth;
const y = tip[1] * canvasHeight;
// Draw cursor
ctx.beginPath();
ctx.arc(x, y, 6, 0, 2 * Math.PI);
ctx.fillStyle = eraserMode ? '#ff6666' : currentColor;
ctx.fill();
ctx.strokeStyle = '#fff';
ctx.lineWidth = 2;
ctx.stroke();
if (isIndexUp) {
if (!drawing) {
drawing = true;
lastX = x;
lastY = y;
} else {
if (lastX !== null && lastY !== null) {
ctx.beginPath();
ctx.moveTo(lastX, lastY);
ctx.lineTo(x, y);
ctx.strokeStyle = eraserMode ? '#1a1a2e' : currentColor;
ctx.lineWidth = eraserMode ? brushSize * 2 : brushSize;
ctx.lineCap = 'round';
ctx.lineJoin = 'round';
ctx.stroke();
}
lastX = x;
lastY = y;
}
} else {
drawing = false;
lastX = null;
lastY = null;
}
} else {
drawing = false;
lastX = null;
lastY = null;
}
} catch (err) {
// silent
}
}, 200); // 200ms = 5 FPS detection (much lighter)
}
// ============================================================
// 11. Drawing Loop – OPTIMIZED: lower frame rate for video
// ============================================================
function drawLoop(timestamp) {
// Only update every 2 frames to reduce CPU usage
frameCount++;
if (timestamp - lastFpsUpdate >= 1000) {
fpsSpan.textContent = `${frameCount} FPS`;
frameCount = 0;
lastFpsUpdate = timestamp;
}
if (videoReady && video.readyState >= 2 && video.videoWidth > 0) {
try {
ctx.drawImage(video, 0, 0, canvasWidth, canvasHeight);
} catch (err) {
ctx.fillStyle = '#1a1a2e';
ctx.fillRect(0, 0, canvasWidth || 640, canvasHeight || 480);
}
} else {
ctx.fillStyle = '#1a1a2e';
ctx.fillRect(0, 0, canvasWidth || 640, canvasHeight || 480);
}
requestAnimationFrame(drawLoop);
}
// ============================================================
// 12. Snapshot Capture (Manual)
// ============================================================
async function captureSnapshot() {
try {
const tempCanvas = document.createElement('canvas');
tempCanvas.width = canvasWidth || 640;
tempCanvas.height = canvasHeight || 480;
const tempCtx = tempCanvas.getContext('2d');
if (videoReady && video.readyState >= 2) {
tempCtx.drawImage(video, 0, 0, tempCanvas.width, tempCanvas.height);
}
tempCtx.drawImage(canvas, 0, 0);
const dataURL = tempCanvas.toDataURL('image/png');
const base64 = dataURL.split(',')[1];
const response = await fetch('/api/snapshot', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ image: base64 })
});
const result = await response.json();
if (result.success) {
savedCount++;
savedSpan.textContent = `Saved: ${savedCount}`;
backendLog(`📸 Saved: ${result.filename}`);
canvas.style.transition = 'opacity 0.1s';
canvas.style.opacity = '0.7';
setTimeout(() => { canvas.style.opacity = '1'; }, 150);
} else {
backendLog(`Save failed: ${result.error}`);
}
} catch (err) {
backendLog(`Save error: ${err.message}`);
}
}
// ============================================================
// 13. UI Event Bindings
// ============================================================
brushColorInput.addEventListener('input', (e) => {
currentColor = e.target.value;
eraserMode = false;
eraserBtn.classList.remove('active');
});
sizeDownBtn.addEventListener('click', () => {
brushSize = Math.max(1, brushSize - 1);
sizeDisplay.textContent = brushSize;
});
sizeUpBtn.addEventListener('click', () => {
brushSize = Math.min(50, brushSize + 1);
sizeDisplay.textContent = brushSize;
});
eraserBtn.addEventListener('click', () => {
eraserMode = !eraserMode;
eraserBtn.classList.toggle('active');
});
clearBtn.addEventListener('click', () => {
if (confirm('Clear all drawings?')) {
drawing = false;
lastX = null;
lastY = null;
if (videoReady) {
ctx.drawImage(video, 0, 0, canvasWidth, canvasHeight);
}
}
});
snapshotBtn.addEventListener('click', captureSnapshot);
toggleCamBtn.addEventListener('click', () => {
cameraVisible = !cameraVisible;
video.style.display = cameraVisible ? 'block' : 'none';
toggleCamBtn.classList.toggle('active');
});
document.addEventListener('keydown', (e) => {
if (e.key === 's' || e.key === 'S') captureSnapshot();
if (e.key === 'e' || e.key === 'E') eraserBtn.click();
if (e.key === 'c' || e.key === 'C') clearBtn.click();
});
// ============================================================
// 14. Initialization
// ============================================================
backendLog('🚀 App initialized');
resizeCanvas();
startCamera(); |