Download index.html from CarterBenjamin/cs2-detect-static: direct link, hf CLI and curl.
- Browser
- Download file 11.8 kB
-
https://huggingface.co/spaces/CarterBenjamin/cs2-detect-static/resolve/main/index.html
- Command line
-
hf download hf://spaces/CarterBenjamin/cs2-detect-static/index.html
-
curl -L -o index.html https://huggingface.co/spaces/CarterBenjamin/cs2-detect-static/resolve/main/index.html
11.8 kB
| <html lang="zh-CN"> | |
| <head> | |
| <meta charset="utf-8" /> | |
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | |
| <title>CS2 目标检测 demo(浏览器内推理)</title> | |
| <style> | |
| :root { --bg:#14151a; --card:#1d1f26; --fg:#e8e8e8; --dim:#8a8f98; --ok:#39ff88; --bad:#ff5b52; } | |
| * { box-sizing: border-box; } | |
| body { margin:0; background:var(--bg); color:var(--fg); font:14px/1.6 "Microsoft YaHei UI",system-ui,sans-serif; } | |
| header { padding:18px 22px 8px; } | |
| h1 { margin:0 0 4px; font-size:18px; } | |
| .sub { color:var(--dim); font-size:13px; } | |
| main { display:grid; grid-template-columns: 340px 1fr; gap:18px; padding:14px 22px 40px; } | |
| @media (max-width: 900px) { main { grid-template-columns: 1fr; } } | |
| .card { background:var(--card); border:1px solid #2a2d36; border-radius:10px; padding:14px; } | |
| label { display:block; color:var(--dim); font-size:12px; margin:10px 0 4px; } | |
| select,input[type=text],input[type=number] { width:100%; background:#0f1014; color:var(--fg); | |
| border:1px solid #2f333d; border-radius:6px; padding:7px 9px; font:inherit; } | |
| input[type=range]{ width:100%; } | |
| button { background:#2b2f39; color:var(--fg); border:1px solid #3a3f4b; border-radius:6px; | |
| padding:9px 14px; font:inherit; cursor:pointer; } | |
| button.primary { background:var(--ok); color:#0b1410; border-color:var(--ok); font-weight:600; } | |
| button:disabled { opacity:.45; cursor:not-allowed; } | |
| .row { display:flex; gap:8px; margin-top:12px; } | |
| .row > * { flex:1; } | |
| canvas { max-width:100%; border-radius:8px; display:block; background:#0b0c10; } | |
| table { width:100%; border-collapse:collapse; font-size:13px; margin-top:10px; } | |
| th,td { text-align:left; padding:5px 6px; border-bottom:1px solid #262932; } | |
| th { color:var(--dim); font-weight:500; } | |
| #log { color:var(--dim); font-size:12px; margin-top:10px; white-space:pre-wrap; } | |
| .hint { color:#e8c05a; font-size:12px; margin-top:8px; } | |
| a { color:#7fd1ff; } | |
| </style> | |
| <script src="https://cdn.jsdelivr.net/npm/onnxruntime-web@1.19.2/dist/ort.min.js"></script> | |
| </head> | |
| <body> | |
| <header> | |
| <h1>CS2 目标检测 demo</h1> | |
| <div class="sub">纯浏览器推理(onnxruntime-web / WASM),不经过服务器,图片不上传。</div> | |
| </header> | |
| <main> | |
| <section class="card"> | |
| <label>选择输入图</label> | |
| <input id="file" type="file" accept="image/*" /> | |
| <label>或试一张示例</label> | |
| <div class="row"> | |
| <button id="ex1">中距离</button> | |
| <button id="ex2">贴脸</button> | |
| </div> | |
| <label>模型</label> | |
| <select id="model"> | |
| <option value="11s">Vombit yolo11s_cs2(默认,准)</option> | |
| <option value="11n">Vombit yolo11n_cs2(快)</option> | |
| </select> | |
| <label>HF token(这两个都是 gated 仓库,必填)</label> | |
| <input id="token" type="text" placeholder="hf_xxx(只留在你浏览器里)" autocomplete="off" /> | |
| <div class="hint">页面带你的 token 直接去 huggingface.co 取权重,本站不转发、不存储、不重新分发。 | |
| 前提:你账号已在这两个仓库页点过 Agree。</div> | |
| <label>置信度阈值 <span id="cfv">0.35</span></label> | |
| <input id="conf" type="range" min="0.05" max="0.9" step="0.05" value="0.35" /> | |
| <label>NMS IoU <span id="iouv">0.45</span></label> | |
| <input id="iou" type="range" min="0.1" max="0.9" step="0.05" value="0.45" /> | |
| <div class="row"> | |
| <button class="primary" id="run">检测</button> | |
| </div> | |
| <div id="log">等模型就绪…</div> | |
| </section> | |
| <section class="card"> | |
| <canvas id="cv" width="640" height="640"></canvas> | |
| <table id="tbl"> | |
| <thead><tr><th>类别</th><th>置信度</th><th>框 (x1,y1,x2,y2)</th><th>高(px)</th></tr></thead> | |
| <tbody></tbody> | |
| </table> | |
| </section> | |
| </main> | |
| <script> | |
| const MODELS = { | |
| "11s": { url: "https://huggingface.co/Vombit/yolo11s_cs2/resolve/main/yolo11s_cs2.onnx", | |
| labels: ["c", "ch", "t", "th"], size: [640, 640], gated: true }, | |
| "11n": { url: "https://huggingface.co/Vombit/yolo11n_cs2/resolve/main/yolo11n_cs2.onnx", | |
| labels: ["c", "ch", "t", "th"], size: [640, 640], gated: true }, | |
| }; | |
| const ZH = { head:"头部", body:"身体", c:"CT 身体", ch:"CT 头", t:"T 身体", th:"T 头" }; | |
| const COLORS = { head:"#ff5b52", body:"#7fd1ff", c:"#7fd1ff", ch:"#ff5b52", t:"#e8c05a", th:"#ff78dc" }; | |
| ort.env.wasm.numThreads = 1; | |
| ort.env.wasm.wasmPaths = "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.19.2/dist/"; | |
| const $ = (id) => document.getElementById(id); | |
| const cv = $("cv"), ctx = cv.getContext("2d"); | |
| const sessions = {}; | |
| let img = null; | |
| let meta = null; // {mw, mh, labels} | |
| function log(s) { $("log").textContent = s; } | |
| async function getSession(key, token) { | |
| if (sessions[key]) return sessions[key]; | |
| const spec = MODELS[key]; | |
| log("下载模型 " + key + " …"); | |
| const headers = spec.gated && token ? { Authorization: "Bearer " + token } : {}; | |
| const res = await fetch(spec.url, { headers }); | |
| if (!res.ok) throw new Error(`取模型失败 HTTP ${res.status}` + (res.status === 401 ? "(需要 token,或先在网页点 Agree)" : "")); | |
| const buf = new Uint8Array(await res.arrayBuffer()); | |
| log("模型 " + key + " 到手," + (buf.length / 1048576).toFixed(1) + " MB,正在初始化…"); | |
| const sess = await ort.InferenceSession.create(buf, { executionProviders: ["wasm"] }); | |
| const inputName = sess.inputNames[0]; | |
| // Vombit 那两个是动态输入(height/width 是符号),所以用预设尺寸;输出 N 由运行时给的真实 dims 决定。 | |
| const [mw, mh] = spec.size; | |
| sessions[key] = { sess, inputName, mw, mh }; | |
| return sessions[key]; | |
| } | |
| function drawBase() { | |
| if (!img) return; | |
| cv.width = img.width; cv.height = img.height; | |
| ctx.drawImage(img, 0, 0); | |
| } | |
| function letterboxTo(size) { // 返回 {canvas, r, left, top} | |
| const [mw, mh] = size; | |
| const r = Math.min(mw / img.width, mh / img.height); | |
| const nw = Math.max(1, Math.round(img.width * r)); | |
| const nh = Math.max(1, Math.round(img.height * r)); | |
| const c = document.createElement("canvas"); | |
| c.width = mw; c.height = mh; | |
| const g = c.getContext("2d"); | |
| g.fillStyle = "rgb(114,114,114)"; | |
| g.fillRect(0, 0, mw, mh); | |
| const left = Math.floor((mw - nw) / 2), top = Math.floor((mh - nh) / 2); | |
| g.drawImage(img, left, top, nw, nh); | |
| return { canvas: c, r, left, top }; | |
| } | |
| function toTensor(canvas, mw, mh) { | |
| const d = canvas.getContext("2d").getImageData(0, 0, mw, mh).data; | |
| const n = mw * mh; | |
| const out = new Float32Array(3 * n); | |
| for (let i = 0; i < n; i++) { | |
| const p = i * 4; | |
| out[i] = d[p + 2] / 255; // B | |
| out[n + i] = d[p + 1] / 255; // G | |
| out[2 * n + i] = d[p] / 255; // R | |
| } | |
| return new ort.Tensor("float32", out, [1, 3, mh, mw]); | |
| } | |
| function iou(a, b) { | |
| const x1 = Math.max(a[0], b[0]), y1 = Math.max(a[1], b[1]); | |
| const x2 = Math.min(a[2], b[2]), y2 = Math.min(a[3], b[3]); | |
| const inter = Math.max(0, x2 - x1) * Math.max(0, y2 - y1); | |
| const aa = (a[2]-a[0])*(a[3]-a[1]), bb = (b[2]-b[0])*(b[3]-b[1]); | |
| return inter / Math.max(aa + bb - inter, 1e-9); | |
| } | |
| function nms(boxes, scores, thr) { | |
| const idx = scores.map((s, i) => i).sort((a, b) => scores[b] - scores[a]); | |
| const keep = []; | |
| const used = new Array(boxes.length).fill(false); | |
| for (const i of idx) { | |
| if (used[i]) continue; | |
| keep.push(i); | |
| for (const j of idx) { | |
| if (!used[j] && j !== i && iou(boxes[i], boxes[j]) > thr) used[j] = true; | |
| } | |
| } | |
| return keep; | |
| } | |
| function decode(out, nc) { | |
| const dims = out.dims; // [1, C, N] 或 [1, N, C] | |
| const C = dims[1], N = dims[2]; | |
| const data = out.data; | |
| const get = (c, n) => data[c * N + n]; | |
| if (C !== 4 + nc) throw new Error(`输出通道 ${C} 与类别数 ${nc} 不匹配`); | |
| const boxes = [], scores = [], cls = []; | |
| for (let n = 0; n < N; n++) { | |
| let best = 0, bi = 0; | |
| for (let c = 0; c < nc; c++) { | |
| const s = get(4 + c, n); | |
| if (s > best) { best = s; bi = c; } | |
| } | |
| const cx = get(0, n), cy = get(1, n), w = get(2, n), h = get(3, n); | |
| boxes.push([cx - w / 2, cy - h / 2, cx + w / 2, cy + h / 2]); | |
| scores.push(best); cls.push(bi); | |
| } | |
| return { boxes, scores, cls }; | |
| } | |
| async function detect() { | |
| if (!img) return; | |
| const key = $("model").value; | |
| const token = $("token").value.trim(); | |
| if (!token) { log("先把 HF token 填进来(这两个模型都是 gated,没有 token 取不到权重)。"); return; } | |
| const conf = parseFloat($("conf").value), iouThr = parseFloat($("iou").value); | |
| const t0 = performance.now(); | |
| try { | |
| const { sess, inputName, mw, mh } = await getSession(key, token); | |
| const lb = letterboxTo([mw, mh]); | |
| const feeds = {}; feeds[inputName] = toTensor(lb.canvas, mw, mh); | |
| const out = (await sess.run(feeds))[sess.outputNames[0]]; | |
| const labels = MODELS[key].labels; | |
| const { boxes, scores, cls } = decode(out, labels.length); | |
| const picked = []; | |
| for (let c = 0; c < labels.length; c++) { | |
| const idx = []; | |
| for (let i = 0; i < scores.length; i++) if (cls[i] === c && scores[i] >= conf) idx.push(i); | |
| if (!idx.length) continue; | |
| const b = idx.map(i => boxes[i]), s = idx.map(i => scores[i]); | |
| for (const k of nms(b, s, iouThr)) picked.push({ box: b[k], score: s[k], cls: c }); | |
| } | |
| picked.sort((a, b) => b.score - a.score); | |
| drawBase(); | |
| const tb = $("tbl").querySelector("tbody"); | |
| tb.innerHTML = ""; | |
| picked.slice(0, 30).forEach(({ box, score, cls: c }) => { | |
| const x1 = (box[0] - lb.left) / lb.r, y1 = (box[1] - lb.top) / lb.r; | |
| const x2 = (box[2] - lb.left) / lb.r, y2 = (box[3] - lb.top) / lb.r; | |
| const name = labels[c]; | |
| const color = COLORS[name] || "#fff"; | |
| ctx.strokeStyle = color; ctx.lineWidth = 2; | |
| ctx.strokeRect(x1, y1, x2 - x1, y2 - y1); | |
| ctx.fillStyle = color; | |
| ctx.font = "12px Consolas,monospace"; | |
| ctx.fillText(`${ZH[name] || name} ${score.toFixed(2)}`, x1 + 2, Math.max(11, y1 - 4)); | |
| const tr = document.createElement("tr"); | |
| tr.innerHTML = `<td>${ZH[name] || name}</td><td>${score.toFixed(3)}</td> | |
| <td>${[x1,y1,x2,y2].map(v => v.toFixed(1)).join(", ")}</td><td>${(y2 - y1).toFixed(1)}</td>`; | |
| tb.appendChild(tr); | |
| }); | |
| const ms = (performance.now() - t0).toFixed(0); | |
| log(`检出 ${picked.length} 个框 · 整段 ${ms} ms(含首次建会话时会更久)`); | |
| } catch (e) { | |
| log("出错了:" + e.message); | |
| } | |
| } | |
| function loadImage(src) { | |
| return new Promise((ok, bad) => { | |
| const im = new Image(); | |
| im.crossOrigin = "anonymous"; | |
| im.onload = () => { img = im; drawBase(); ok(); }; | |
| im.onerror = bad; | |
| im.src = src; | |
| }); | |
| } | |
| $("file").addEventListener("change", async (ev) => { | |
| const f = ev.target.files[0]; | |
| if (!f) return; | |
| const url = URL.createObjectURL(f); | |
| await loadImage(url); | |
| detect(); | |
| }); | |
| $("ex1").onclick = async () => { await loadImage("examples/sample01.png"); detect(); }; | |
| $("ex2").onclick = async () => { await loadImage("examples/sample02.png"); detect(); }; | |
| $("conf").oninput = () => { $("cfv").textContent = $("conf").value; }; | |
| $("iou").oninput = () => { $("iouv").textContent = $("iou").value; }; | |
| $("run").onclick = detect; | |
| $("model").addEventListener("change", async () => { | |
| const key = $("model").value; | |
| if (MODELS[key].gated && !$("token").value.trim()) { | |
| log("这个模型是 gated:把 HF token 填进去再点检测。"); | |
| } else { | |
| log("模型已切换,点「检测」重跑。"); | |
| } | |
| }); | |
| (async () => { | |
| try { | |
| await loadImage("examples/sample01.png"); | |
| log("示例图已载入。填好 HF token 后点「检测」(权重会在那一步下载,约 36 MB)。"); | |
| } catch (e) { | |
| log("示例图没载入:" + e.message); | |
| } | |
| })(); | |
| </script> | |
| </body> | |
| </html> | |