Spaces:
Running
Running
Model V4: 2-class 800px domain-focused (drain 0.67, nhận diện VN tốt hơn)
Browse files1 unmodified line2345567891030 unmodified lines41424344434445464546474849505152535453555657581 unmodified line606162616364656627 unmodified lines949596959798991008 unmodified lines1091101111101121131141151 unmodified line Không cần backend. Dữ liệu (camera + báo cáo) lưu trong bộ nhớ trang (mất khi tải lại). */
// ============ CẤU HÌNH ============const CLASSES = ["floating_trash", "drain", "trash_pile"];const CLASSES = ["trash", "drain"]; // model V4: 2 classconst CONF = 0.25; // ngưỡng thấp -> bắt được nhiều rác hơn (app giám sát ưu tiên không bỏ sót)const MODEL_SIZE = 800; // độ phân giải model V4 (khớp lúc train)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)30 unmodified lines// ============ SUY LUẬN AI ============// vẽ ảnh vào canvas 640x640 -> tensor float32 [1,3,640,640]function preprocess(source){ const S = MODEL_SIZE; const c = document.createElement("canvas"); c.width = 640; c.height = 640; const ctx = c.getContext("2d"); ctx.drawImage(source, 0, 0, 640, 640); const d = ctx.getImageData(0, 0, 640, 640).data; // RGBA const area = 640 * 640; const c = document.createElement("canvas"); c.width = S; c.height = S; const ctx = c.getContext("2d"); ctx.drawImage(source, 0, 0, S, S); const d = ctx.getImageData(0, 0, S, S).data; // RGBA const area = S * S; const f = new Float32Array(3 * area); for (let i = 0; i < area; i++){ f[i] = d[i*4] / 255; // R f[i + area] = d[i*4+1] / 255; // G f[i + 2*area] = d[i*4+2] / 255; // B } return new ort.Tensor("float32", f, [1, 3, 640, 640]); return new ort.Tensor("float32", f, [1, 3, S, S]);}
// trả về list {cls, conf, box=[x1,y1,x2,y2]} theo kích thước gốc origW×origH1 unmodified line const t = preprocess(source); const res = await session.run({ [session.inputNames[0]]: t }); const data = res[session.outputNames[0]].data; // [300*6]: x1,y1,x2,y2,score,cls (toạ độ 640) const sx = origW / 640, sy = origH / 640; const sx = origW / MODEL_SIZE, sy = origH / MODEL_SIZE; const dets = []; for (let i = 0; i < 300; i++){ const s = data[i*6+4];27 unmodified lines}function analyze(dets, iw, ih){ // Gộp mọi loại rác thành "rác" (rác nổi + đống rác) cho nhất quán const trashBoxes = dets.filter(d=>d.cls==="floating_trash"||d.cls==="trash_pile").map(d=>d.box); const trashBoxes = dets.filter(d=>d.cls==="trash").map(d=>d.box); const nTrash = trashBoxes.length; const drains = dets.filter(d=>d.cls==="drain"); const hasDrain = drains.length>0;8 unmodified lines}
// vẽ box lên ảnh -> dataURL để hiển thịconst BOX_COLOR = { floating_trash:"#ffa500", trash_pile:"#ffa500", drain:"#1e90ff" };const BOX_COLOR = { trash:"#ffa500", drain:"#1e90ff" };function drawAnnotated(source, dets, w, h){ const c = document.createElement("canvas"); c.width=w; c.height=h; const ctx = c.getContext("2d"); ctx.drawImage(source,0,0,w,h);app.js+11-9