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 line2345678930 unmodified lines40414243444546474849505152535455561 unmodified line5859606162636427 unmodified lines929394959697988 unmodified lines1071081091101111121131 unmodified lineKhô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 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 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 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; // RGBAconst area = 640 * 640;const f = new Float32Array(3 * area);for (let i = 0; i < area; i++){f[i] = d[i*4] / 255; // Rf[i + area] = d[i*4+1] / 255; // Gf[i + 2*area] = d[i*4+2] / 255; // B}return new ort.Tensor("float32", f, [1, 3, 640, 640]);}// trả về list {cls, conf, box=[x1,y1,x2,y2]} theo kích thước gốc origW×origH1 unmodified lineconst 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 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ánconst trashBoxes = dets.filter(d=>d.cls==="floating_trash"||d.cls==="trash_pile").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" };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);1 unmodified line234567891030 unmodified lines4142434445464748495051525354555657581 unmodified line6061626364656627 unmodified lines9495969798991008 unmodified lines1091101111121131141151 unmodified lineKhô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 = ["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 = 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; // RGBAconst area = S * S;const f = new Float32Array(3 * area);for (let i = 0; i < area; i++){f[i] = d[i*4] / 255; // Rf[i + area] = d[i*4+1] / 255; // Gf[i + 2*area] = d[i*4+2] / 255; // B}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 lineconst 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 / 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ánconst 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 = { 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