Spaces:
Running
Running
Fix canvas tainted (ảnh demo nhúng base64) + thêm chụp webcam
Browse files5 unmodified lines6789101112174 unmodified lines18718818919019119219377 unmodified lines2712722732742752765 unmodified linesconst CONF = 0.4;const CLOG_TH = { partial: 20, blocked: 60 };const MONITOR_MS = 12000; // camera tự phân tích mỗi 12sconst DEMO_FRAMES = ["demo/frame_0.jpg","demo/frame_1.jpg","demo/frame_2.jpg","demo/frame_3.jpg","demo/frame_4.jpg","demo/frame_5.jpg","demo/frame_6.jpg","demo/frame_7.jpg","demo/frame_8.jpg","demo/frame_9.jpg"];const CLOG_LABEL = { clear:{t:"🟢 Không tắc",c:"clear"}, partial:{t:"🟡 Tắc nhẹ",c:"partial"}, blocked:{t:"🔴 Tắc nặng",c:"blocked"} };const RISK_LABEL = { high:{t:"🔴 Nguy cơ ngập CAO",c:"high"}, medium:{t:"🟡 Trung bình",c:"medium"}, low:{t:"🟢 Bình thường",c:"low"} };const RISK_COLOR = { high:"#e03131", medium:"#f5b301", low:"#2e9e5b" };174 unmodified linesif (cam.type==="snapshot" && cam.url){img = await loadImage(cam.url + (cam.url.includes("?")?"&":"?") + "t=" + Date.now(), true);} else {const src = DEMO_FRAMES[cam.seq % DEMO_FRAMES.length] + "?v=2"; cam.seq++;img = await loadImage(src, false);}} catch(e){77 unmodified linesrefreshSubmit();});function refreshSubmit(){ document.getElementById("submitBtn").disabled = !(repImageEl && repCoords && modelReady); }function requestGps(){const box=document.getElementById("gpsBox");if(!navigator.geolocation){box.innerHTML='<span class="gps-warn">Trình duyệt không hỗ trợ GPS.</span>';showPinMap();return;}5 unmodified lines678910111213174 unmodified lines18818919019119219319419577 unmodified lines2732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033045 unmodified linesconst CONF = 0.4;const CLOG_TH = { partial: 20, blocked: 60 };const MONITOR_MS = 12000; // camera tự phân tích mỗi 12s// Ảnh demo nhúng base64 (cùng origin, tránh lỗi canvas tainted do HF redirect CDN)const DEMO_FRAMES = (window.DEMO_IMAGES && window.DEMO_IMAGES.length) ? window.DEMO_IMAGES : [];const CLOG_LABEL = { clear:{t:"🟢 Không tắc",c:"clear"}, partial:{t:"🟡 Tắc nhẹ",c:"partial"}, blocked:{t:"🔴 Tắc nặng",c:"blocked"} };const RISK_LABEL = { high:{t:"🔴 Nguy cơ ngập CAO",c:"high"}, medium:{t:"🟡 Trung bình",c:"medium"}, low:{t:"🟢 Bình thường",c:"low"} };const RISK_COLOR = { high:"#e03131", medium:"#f5b301", low:"#2e9e5b" };174 unmodified linesif (cam.type==="snapshot" && cam.url){img = await loadImage(cam.url + (cam.url.includes("?")?"&":"?") + "t=" + Date.now(), true);} else {if (!DEMO_FRAMES.length){ cam.err="Chưa nạp được ảnh demo."; cam.checked=Date.now(); return; }const src = DEMO_FRAMES[cam.seq % DEMO_FRAMES.length]; cam.seq++; // data-URI, không thêm ?vimg = await loadImage(src, false);}} catch(e){77 unmodified linesrefreshSubmit();});function refreshSubmit(){ document.getElementById("submitBtn").disabled = !(repImageEl && repCoords && modelReady); }// ---- Chụp ảnh bằng webcam ----let camStream=null;async function startCamera(){try{camStream = await navigator.mediaDevices.getUserMedia({ video:{ facingMode:"environment" }, audio:false });document.getElementById("camVideo").srcObject = camStream;document.getElementById("camArea").classList.remove("hidden");}catch(e){document.getElementById("repMsg").textContent = "Không mở được camera: " + e.message + " (cần HTTPS + cấp quyền).";}}function stopCamera(){if(camStream){ camStream.getTracks().forEach(t=>t.stop()); camStream=null; }document.getElementById("camArea").classList.add("hidden");}async function snapPhoto(){const v=document.getElementById("camVideo");if(!v.videoWidth){ return; }const c=document.createElement("canvas"); c.width=v.videoWidth; c.height=v.videoHeight;c.getContext("2d").drawImage(v,0,0,c.width,c.height);const dataUrl=c.toDataURL("image/jpeg",0.9);repImageEl=await loadImage(dataUrl,false);const p=document.getElementById("preview"); p.src=dataUrl; p.style.display="block";stopCamera(); document.getElementById("repMsg").textContent=""; refreshSubmit();}function requestGps(){const box=document.getElementById("gpsBox");if(!navigator.geolocation){box.innerHTML='<span class="gps-warn">Trình duyệt không hỗ trợ GPS.</span>';showPinMap();return;}
app.js+30-2