1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
| <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>OCR 结果预览</title> <style> :root { --bg: #0f1419; --panel: #1a2332; --border: #2d3a4f; --text: #e7ecf3; --muted: #8b9cb3; --accent: #3b82f6; --accent-hover: #2563eb; --highlight: #f59e0b; --danger: #ef4444; } * { box-sizing: border-box; margin: 0; padding: 0; } html, body { height: 100%; overflow: hidden; } body { font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif; background: var(--bg); color: var(--text); display: flex; flex-direction: column; } header { flex-shrink: 0; padding: 14px 20px; border-bottom: 1px solid var(--border); background: var(--panel); } header h1 { font-size: 17px; font-weight: 600; margin-bottom: 12px; } .controls { display: flex; gap: 10px; align-items: end; } .field { flex: 1; min-width: 0; } .field label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; } .field-row { display: flex; gap: 6px; } .field-row input[type="text"] { flex: 1; min-width: 0; padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border); background: var(--bg); color: var(--text); font-size: 13px; } .field-row input[type="text"]:focus { outline: none; border-color: var(--accent); } .btn { padding: 8px 14px; border-radius: 8px; border: 1px solid var(--border); background: var(--bg); color: var(--text); font-size: 13px; cursor: pointer; white-space: nowrap; } .btn:hover { border-color: var(--accent); } .btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 500; } .btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); } .hint { margin-top: 8px; font-size: 12px; color: var(--muted); } .hint.error { color: var(--danger); } .meta { margin-top: 8px; font-size: 12px; color: var(--muted); word-break: break-all; } main { flex: 1; min-height: 0; display: grid; grid-template-columns: 1fr 360px; overflow: hidden; } .canvas-wrap { min-height: 0; overflow: auto; padding: 24px; background: #0a0e14; display: flex; align-items: flex-start; justify-content: center; } .placeholder { color: var(--muted); font-size: 14px; padding: 48px; text-align: center; } .stage { position: relative; display: none; line-height: 0; box-shadow: 0 8px 32px rgba(0, 0, 0, 0.45); } .stage.visible { display: inline-block; } .stage img { display: block; max-width: 100%; height: auto; user-select: none; } .stage svg { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; } .stage svg polygon { fill: rgba(59, 130, 246, 0.12); stroke: rgba(59, 130, 246, 0.85); stroke-width: 2; vector-effect: non-scaling-stroke; pointer-events: all; cursor: pointer; transition: fill 0.15s, stroke 0.15s; } .stage svg polygon:hover, .stage svg polygon.active { fill: rgba(245, 158, 11, 0.25); stroke: var(--highlight); stroke-width: 2.5; } .sidebar { min-height: 0; border-left: 1px solid var(--border); background: var(--panel); display: flex; flex-direction: column; overflow: hidden; } .sidebar-head { padding: 14px; border-bottom: 1px solid var(--border); } .sidebar-head input { width: 100%; padding: 8px 12px; border-radius: 8px; border: 1px solid var(--border); background: var(--bg); color: var(--text); font-size: 13px; } .sidebar-head input:focus { outline: none; border-color: var(--accent); } .stats { margin-top: 8px; font-size: 12px; color: var(--muted); } .list { flex: 1; min-height: 0; overflow-y: auto; padding: 8px; } .item { padding: 10px 12px; border-radius: 8px; border: 1px solid transparent; cursor: pointer; margin-bottom: 4px; } .item:hover { background: rgba(255, 255, 255, 0.04); } .item.active { background: rgba(245, 158, 11, 0.12); border-color: rgba(245, 158, 11, 0.35); } .item .idx { font-size: 11px; color: var(--muted); margin-bottom: 4px; } .item .text { font-size: 14px; line-height: 1.4; word-break: break-all; } .item .score { margin-top: 4px; font-size: 11px; color: var(--muted); } .empty { padding: 24px; text-align: center; color: var(--muted); font-size: 13px; } input[type="file"] { display: none; } @media (max-width: 900px) { .controls { flex-direction: column; align-items: stretch; } main { grid-template-columns: 1fr; grid-template-rows: 1fr 40vh; } .sidebar { border-left: none; border-top: 1px solid var(--border); } } </style> </head> <body> <header> <h1>OCR 识别结果预览</h1> <div class="controls"> <div class="field"> <label for="jsonPath">JSON 路径</label> <div class="field-row"> <input id="jsonPath" type="text" placeholder="例如 output/general_ocr_002_res.json" /> <button class="btn" type="button" id="jsonBrowse">浏览</button> <input id="jsonFile" type="file" accept=".json,application/json" /> </div> </div> <button class="btn btn-primary" type="button" id="renderBtn">渲染</button> </div> <div class="hint" id="hint">填写 JSON 路径后点击「渲染」,图片路径将从 JSON 的 input_path 自动读取。需通过本地 HTTP 服务打开(如 uv run python -m http.server 8080)。</div> <div class="meta" id="meta"></div> </header>
<main> <div class="canvas-wrap"> <div class="placeholder" id="placeholder">请设置 JSON 路径,然后点击「渲染」</div> <div class="stage" id="stage"> <img id="image" alt="OCR 原图" /> <svg id="overlay" viewBox="0 0 1 1" preserveAspectRatio="none"></svg> </div> </div> <aside class="sidebar"> <div class="sidebar-head"> <input id="filter" type="search" placeholder="搜索识别文字..." disabled /> <div class="stats" id="stats"></div> </div> <div class="list" id="list"> <div class="empty">暂无数据</div> </div> </aside> </main>
<script> const jsonPathEl = document.getElementById("jsonPath"); const jsonFileEl = document.getElementById("jsonFile"); const renderBtn = document.getElementById("renderBtn"); const hintEl = document.getElementById("hint"); const metaEl = document.getElementById("meta"); const placeholderEl = document.getElementById("placeholder"); const stageEl = document.getElementById("stage"); const imageEl = document.getElementById("image"); const overlayEl = document.getElementById("overlay"); const listEl = document.getElementById("list"); const statsEl = document.getElementById("stats"); const filterEl = document.getElementById("filter");
let texts = []; let scores = []; let polys = []; let boxes = []; let polygons = []; let imageObjectUrl = null;
const PROJECT_DIRS = ["samples", "output", "images", "data", "input"];
function escapeHtml(str) { return String(str) .replace(/&/g, "&") .replace(/</g, "<") .replace(/>/g, ">") .replace(/"/g, """); }
function setHint(msg, isError = false) { hintEl.textContent = msg; hintEl.classList.toggle("error", isError); }
function normalizePath(path) { return String(path).replace(/\\/g, "/"); }
function getSiteRoot() { const url = new URL(location.href); const path = url.pathname; url.pathname = path.endsWith("/") ? path : path.replace(/\/[^/]*$/, "/"); url.search = ""; url.hash = ""; return url; }
function toFetchUrl(projectRelativePath) { const norm = normalizePath(projectRelativePath).replace(/^\//, ""); return new URL(norm, getSiteRoot()).href; }
function resolveImagePath(inputPath, jsonPath) { if (!inputPath) { throw new Error("JSON 中缺少 input_path 字段"); } if (/^https?:\/\//i.test(inputPath)) { return inputPath; }
const norm = normalizePath(inputPath);
if (/^[a-zA-Z]:\//.test(norm) || norm.startsWith("/")) { const lower = norm.toLowerCase(); for (const dir of PROJECT_DIRS) { const marker = `${dir}/`; const idx = lower.indexOf(marker); if (idx >= 0) { return norm.slice(idx); } } const fileName = norm.split("/").pop(); if (jsonPath) { const jsonDir = normalizePath(jsonPath).replace(/\/[^/]*$/, ""); return jsonDir ? `${jsonDir}/${fileName}` : fileName; } return fileName; }
if (jsonPath) { const jsonDir = normalizePath(jsonPath).replace(/\/[^/]*$/, ""); return jsonDir ? `${jsonDir}/${norm}` : norm; } return norm; }
function polyToPoints(poly) { if (!poly || !poly.length) return ""; return poly.map(p => p.join(",")).join(" "); }
function boxToPoly(box) { const [x1, y1, x2, y2] = box; return [[x1, y1], [x2, y1], [x2, y2], [x1, y2]]; }
function getRegions() { if (polys.length) return polys; return boxes.map(boxToPoly); }
function setActive(index) { polygons.forEach((pg, i) => pg.classList.toggle("active", i === index)); listEl.querySelectorAll(".item").forEach((el, i) => el.classList.toggle("active", i === index)); const activeItem = listEl.querySelector(".item.active"); if (activeItem) activeItem.scrollIntoView({ block: "nearest" }); }
function renderList(keyword = "") { listEl.innerHTML = ""; const kw = keyword.trim().toLowerCase(); let visible = 0;
texts.forEach((text, i) => { if (kw && !String(text).toLowerCase().includes(kw)) return; visible += 1; const score = scores[i] != null ? (scores[i] * 100).toFixed(1) : "-"; const item = document.createElement("div"); item.className = "item"; item.innerHTML = ` <div class="idx">#${i + 1}</div> <div class="text">${escapeHtml(text || "(空)")}</div> <div class="score">置信度 ${score}%</div> `; item.addEventListener("click", () => setActive(i)); listEl.appendChild(item); });
if (visible === 0) { listEl.innerHTML = '<div class="empty">无匹配结果</div>'; } }
function renderOverlay(imgW, imgH) { overlayEl.setAttribute("viewBox", `0 0 ${imgW} ${imgH}`); overlayEl.innerHTML = ""; polygons = []; getRegions().forEach((poly, i) => { const pg = document.createElementNS("http://www.w3.org/2000/svg", "polygon"); pg.setAttribute("points", polyToPoints(poly)); pg.dataset.index = String(i); pg.addEventListener("click", () => setActive(i)); overlayEl.appendChild(pg); polygons.push(pg); }); }
function applyData(ocrData, imageSrc, jsonPath, imagePath) { texts = ocrData.rec_texts || []; scores = ocrData.rec_scores || []; polys = ocrData.dt_polys || ocrData.rec_polys || []; boxes = ocrData.rec_boxes || [];
metaEl.textContent = `JSON: ${jsonPath} · 图片: ${imagePath}`; const count = texts.length; const avg = scores.length ? (scores.reduce((a, b) => a + b, 0) / scores.length * 100).toFixed(1) : "-"; statsEl.textContent = `共 ${count} 个文本区域 · 平均置信度 ${avg}%`; filterEl.disabled = false;
imageEl.onload = () => { placeholderEl.style.display = "none"; stageEl.classList.add("visible"); renderOverlay(imageEl.naturalWidth, imageEl.naturalHeight); renderList(filterEl.value); setHint("渲染完成"); }; imageEl.onerror = () => { setHint(`图片加载失败: ${imagePath} · ${imageSrc}`, true); }; imageEl.src = imageSrc; }
async function loadImage(imagePath) { if (imageObjectUrl) { URL.revokeObjectURL(imageObjectUrl); imageObjectUrl = null; } const imageUrl = /^https?:\/\//i.test(imagePath) ? imagePath : toFetchUrl(imagePath); if (location.protocol === "file:") { throw new Error("请通过 HTTP 服务打开页面(uv run python -m http.server 8080),不要直接双击 index.html"); } const res = await fetch(imageUrl); if (!res.ok) { throw new Error(`图片加载失败: ${imagePath} (${res.status}) · ${imageUrl}`); } const blob = await res.blob(); const url = URL.createObjectURL(blob); imageObjectUrl = url; return url; }
async function fetchJson(jsonPath) { const candidates = [jsonPath]; if (!jsonPath.includes("/") && !jsonPath.includes("\\")) { candidates.push(`output/${jsonPath}`); } for (const candidate of candidates) { const res = await fetch(toFetchUrl(candidate)); if (res.ok) { return { data: await res.json(), path: candidate }; } } throw new Error(`JSON 加载失败: ${jsonPath}`); }
async function loadFromJsonPath(jsonPath) { if (!jsonPath) { throw new Error("请填写 JSON 路径"); } setHint("正在加载 JSON..."); const { data: ocrData, path: resolvedJsonPath } = await fetchJson(jsonPath); const imagePath = resolveImagePath(ocrData.input_path, resolvedJsonPath); setHint(`正在加载图片: ${imagePath}`); const imageSrc = await loadImage(imagePath); applyData(ocrData, imageSrc, resolvedJsonPath, imagePath); }
async function loadFromJsonFile(jsonFile) { setHint("正在读取 JSON..."); const text = await jsonFile.text(); const ocrData = JSON.parse(text); const jsonHint = jsonPathEl.value.trim() || `output/${jsonFile.name}`; const imagePath = resolveImagePath(ocrData.input_path, jsonHint); setHint(`正在加载图片: ${imagePath}`); const imageSrc = await loadImage(imagePath); applyData(ocrData, imageSrc, jsonFile.name, imagePath); }
async function render() { try { renderBtn.disabled = true; const jsonFile = jsonFileEl.files[0]; if (jsonFile) { await loadFromJsonFile(jsonFile); return; } await loadFromJsonPath(jsonPathEl.value.trim()); } catch (err) { setHint(err.message || String(err), true); } finally { renderBtn.disabled = false; } }
function initFromQuery() { const json = new URLSearchParams(location.search).get("json"); if (json) { jsonPathEl.value = json; render(); } }
document.getElementById("jsonBrowse").addEventListener("click", () => jsonFileEl.click()); jsonFileEl.addEventListener("change", () => { if (jsonFileEl.files[0]) { jsonPathEl.value = jsonFileEl.files[0].name; render(); } }); renderBtn.addEventListener("click", render); filterEl.addEventListener("input", (e) => renderList(e.target.value)); jsonPathEl.addEventListener("keydown", (e) => { if (e.key === "Enter") render(); });
initFromQuery(); </script> </body> </html>
|