Download detect.js from adarsh108/defectgen: direct link, hf CLI and curl.
- Browser
- Download file 5.71 kB
-
https://huggingface.co/spaces/adarsh108/defectgen/resolve/main/detect.js
- Command line
-
hf download hf://spaces/adarsh108/defectgen/detect.js
-
curl -L -o detect.js https://huggingface.co/spaces/adarsh108/defectgen/resolve/main/detect.js
5.71 kB
| // YOLO segmentation post-processing for ONNX Runtime (browser and Node). | |
| // Mirrors Ultralytics: letterbox -> model -> confidence filter -> per-class NMS -> mask from | |
| // prototypes, cropped to the box. Pure functions, no DOM, so Node tests can check them. | |
| export const PAD = 114; | |
| /** Letterbox geometry for a w x h image into a size x size square (Ultralytics LetterBox). */ | |
| export function letterbox(w, h, size) { | |
| const r = Math.min(size / w, size / h); | |
| const nw = Math.round(w * r); | |
| const nh = Math.round(h * r); | |
| const left = Math.round((size - nw) / 2 - 0.1); | |
| const top = Math.round((size - nh) / 2 - 0.1); | |
| return { r, nw, nh, left, top, size }; | |
| } | |
| /** RGBA pixels (size x size, already letterboxed) -> Float32 CHW in [0, 1]. */ | |
| export function toTensor(rgba, size) { | |
| const n = size * size; | |
| const out = new Float32Array(3 * n); | |
| for (let i = 0; i < n; i++) { | |
| out[i] = rgba[4 * i] / 255; | |
| out[n + i] = rgba[4 * i + 1] / 255; | |
| out[2 * n + i] = rgba[4 * i + 2] / 255; | |
| } | |
| return out; | |
| } | |
| function iou(a, b) { | |
| const x1 = Math.max(a[0], b[0]); | |
| const y1 = Math.max(a[1], b[1]); | |
| const x2 = Math.min(a[2], b[2]); | |
| const y2 = Math.min(a[3], b[3]); | |
| const inter = Math.max(0, x2 - x1) * Math.max(0, y2 - y1); | |
| const area = (r) => (r[2] - r[0]) * (r[3] - r[1]); | |
| return inter / (area(a) + area(b) - inter + 1e-9); | |
| } | |
| /** | |
| * Decode raw outputs. | |
| * out0: [4 + nc + nm, N] (cx, cy, w, h, class scores, mask coefficients), flattened row-major. | |
| * protos: [nm, mh, mw] flattened. Returns detections with boxes in letterboxed input pixels and | |
| * mask logits at prototype resolution; use maskAt() for the full-size mask. | |
| */ | |
| export function postprocess(out0, protos, opts) { | |
| const { nc, nm = 32, n, mh, mw, conf = 0.25, iouThr = 0.5, maxDet = 300 } = opts; | |
| const cands = []; | |
| for (let j = 0; j < n; j++) { | |
| let best = -1; | |
| let score = conf; | |
| for (let c = 0; c < nc; c++) { | |
| const s = out0[(4 + c) * n + j]; | |
| if (s > score) { | |
| score = s; | |
| best = c; | |
| } | |
| } | |
| if (best < 0) continue; | |
| const cx = out0[j]; | |
| const cy = out0[n + j]; | |
| const w = out0[2 * n + j]; | |
| const h = out0[3 * n + j]; | |
| cands.push({ cls: best, score, box: [cx - w / 2, cy - h / 2, cx + w / 2, cy + h / 2], j }); | |
| } | |
| cands.sort((a, b) => b.score - a.score); | |
| const kept = []; | |
| for (const d of cands) { | |
| if (kept.length >= maxDet) break; | |
| if (kept.every((k) => k.cls !== d.cls || iou(k.box, d.box) <= iouThr)) kept.push(d); | |
| } | |
| const plane = mh * mw; | |
| for (const d of kept) { | |
| const coef = new Float32Array(nm); | |
| for (let k = 0; k < nm; k++) coef[k] = out0[(4 + nc + k) * n + d.j]; | |
| const logits = new Float32Array(plane); | |
| for (let p = 0; p < plane; p++) { | |
| let v = 0; | |
| for (let k = 0; k < nm; k++) v += coef[k] * protos[k * plane + p]; | |
| logits[p] = v; | |
| } | |
| d.logits = logits; | |
| d.mh = mh; | |
| d.mw = mw; | |
| delete d.j; | |
| } | |
| return kept; | |
| } | |
| /** Box in letterboxed pixels -> original image pixels. */ | |
| export function unletterbox(box, lb) { | |
| return [ | |
| (box[0] - lb.left) / lb.r, | |
| (box[1] - lb.top) / lb.r, | |
| (box[2] - lb.left) / lb.r, | |
| (box[3] - lb.top) / lb.r, | |
| ]; | |
| } | |
| /** | |
| * Full-size mask (size x size, letterboxed space) of one detection, like Ultralytics | |
| * process_mask: bilinear upsample of the logits (align_corners=False), crop to the box, > 0. | |
| */ | |
| export function maskAt(det, size) { | |
| const { logits, mh, mw, box } = det; | |
| const out = new Uint8Array(size * size); | |
| const fy = mh / size; | |
| const fx = mw / size; | |
| const x0 = Math.max(0, Math.floor(box[0])); | |
| const y0 = Math.max(0, Math.floor(box[1])); | |
| const x1 = Math.min(size, Math.ceil(box[2])); | |
| const y1 = Math.min(size, Math.ceil(box[3])); | |
| for (let y = y0; y < y1; y++) { | |
| if (y + 0.5 <= box[1] || y + 0.5 > box[3]) continue; | |
| const sy = Math.min(Math.max((y + 0.5) * fy - 0.5, 0), mh - 1); | |
| const ya = Math.floor(sy); | |
| const yb = Math.min(ya + 1, mh - 1); | |
| const wy = sy - ya; | |
| for (let x = x0; x < x1; x++) { | |
| if (x + 0.5 <= box[0] || x + 0.5 > box[2]) continue; | |
| const sx = Math.min(Math.max((x + 0.5) * fx - 0.5, 0), mw - 1); | |
| const xa = Math.floor(sx); | |
| const xb = Math.min(xa + 1, mw - 1); | |
| const wx = sx - xa; | |
| const top = logits[ya * mw + xa] * (1 - wx) + logits[ya * mw + xb] * wx; | |
| const bot = logits[yb * mw + xa] * (1 - wx) + logits[yb * mw + xb] * wx; | |
| if (top * (1 - wy) + bot * wy > 0) out[y * size + x] = 1; | |
| } | |
| } | |
| return out; | |
| } | |
| /** | |
| * RGBA pixels of a w x h image -> letterboxed Float32 CHW tensor (size x size, [0, 1]). | |
| * Resizes like OpenCV INTER_LINEAR (what Ultralytics uses), pads with 114 grey. | |
| */ | |
| export function letterboxTensor(rgba, w, h, size) { | |
| const lb = letterbox(w, h, size); | |
| const n = size * size; | |
| const out = new Float32Array(3 * n).fill(PAD / 255); | |
| const fx = w / lb.nw; | |
| const fy = h / lb.nh; | |
| for (let y = 0; y < lb.nh; y++) { | |
| let sy = (y + 0.5) * fy - 0.5; | |
| sy = Math.min(Math.max(sy, 0), h - 1); | |
| const ya = Math.floor(sy); | |
| const yb = Math.min(ya + 1, h - 1); | |
| const wy = sy - ya; | |
| for (let x = 0; x < lb.nw; x++) { | |
| let sx = (x + 0.5) * fx - 0.5; | |
| sx = Math.min(Math.max(sx, 0), w - 1); | |
| const xa = Math.floor(sx); | |
| const xb = Math.min(xa + 1, w - 1); | |
| const wx = sx - xa; | |
| const p = (y + lb.top) * size + (x + lb.left); | |
| for (let c = 0; c < 3; c++) { | |
| const a = rgba[4 * (ya * w + xa) + c] * (1 - wx) + rgba[4 * (ya * w + xb) + c] * wx; | |
| const b = rgba[4 * (yb * w + xa) + c] * (1 - wx) + rgba[4 * (yb * w + xb) + c] * wx; | |
| out[c * n + p] = Math.round(a * (1 - wy) + b * wy) / 255; | |
| } | |
| } | |
| } | |
| return { tensor: out, lb }; | |
| } | |