diff --git a/web_dehaze/pipeline.py b/web_dehaze/pipeline.py index 2584826..a83bb7d 100644 --- a/web_dehaze/pipeline.py +++ b/web_dehaze/pipeline.py @@ -99,6 +99,25 @@ def post_result_path(filename: str, source: str, processor: str, params: dict[st return image_result_dir(filename) / "postprocess" / f"{_safe_slug(source)}__{_safe_slug(processor)}{suffix}.png" +def post_metadata_path(output_path: Path) -> Path: + return output_path.with_suffix(".json") + + +def read_post_metadata(output_path: Path) -> dict[str, Any]: + metadata_path = post_metadata_path(output_path) + if not metadata_path.exists(): + return {} + try: + return json.loads(metadata_path.read_text(encoding="utf-8")) + except Exception: + return {} + + +def write_post_metadata(output_path: Path, payload: dict[str, Any]) -> None: + metadata_path = post_metadata_path(output_path) + metadata_path.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8") + + def relpath(path: Path) -> str: return path.resolve().relative_to(ROOT).as_posix() @@ -206,7 +225,20 @@ def get_results(filename: str) -> dict[str, Any]: post_dir = image_result_dir(filename) / "postprocess" if post_dir.exists(): for path in sorted(post_dir.glob("*.png"), key=lambda p: p.name.lower()): - post.append({"name": path.stem, "exists": True, "path": relpath(path)}) + metadata = read_post_metadata(path) + post.append( + { + "name": path.stem, + "exists": True, + "path": relpath(path), + "meta": metadata.get("meta", {}), + "processor": metadata.get("processor", ""), + "source": metadata.get("source", ""), + "reference": metadata.get("reference", ""), + "params": metadata.get("params", {}), + "metadata_path": relpath(post_metadata_path(path)) if post_metadata_path(path).exists() else "", + } + ) return { "image": filename, @@ -249,6 +281,7 @@ def collect_download_items(filenames: list[str] | None = None, include_original: if post_dir.exists(): for result in sorted(post_dir.glob("*.png"), key=lambda p: p.name.lower()): add_file(result, f"{folder}/postprocess/{result.name}") + add_file(post_metadata_path(result), f"{folder}/postprocess/{post_metadata_path(result).name}") return items @@ -523,6 +556,19 @@ def run_postprocessors_for_source( proc_params = params.get(processor, params) output = post_result_path(filename, source_name, processor, proc_params) meta = run_postprocess(processor, source_path, output, reference_path=reference_path, params=proc_params) + write_post_metadata( + output, + { + "image": filename, + "source": source_name, + "processor": processor, + "reference": reference_path.name, + "params": proc_params, + "meta": meta, + "output": relpath(output), + "created_at": time.strftime("%Y-%m-%d %H:%M:%S"), + }, + ) outputs.append(output) log(f"后处理完成 {source_name} / {processor}: {json.dumps(meta, ensure_ascii=False)}") return outputs diff --git a/web_dehaze/server.py b/web_dehaze/server.py index ac71275..73fc6d5 100644 --- a/web_dehaze/server.py +++ b/web_dehaze/server.py @@ -130,9 +130,12 @@ def _run_default_batch_job(log, payload: dict[str, Any]) -> dict[str, Any]: baidu_output = pipeline.run_dehaze_method(filename, "Baidu_API", {}, log) auto_output = pipeline.post_result_path(filename, "Baidu_API", "auto_sv", {}) - if skip_existing and auto_output.exists(): + auto_meta = pipeline.post_metadata_path(auto_output) + if skip_existing and auto_output.exists() and auto_meta.exists(): log(f"[自动 S/V] 已存在,跳过: {pipeline.relpath(auto_output)}") else: + if auto_output.exists() and not auto_meta.exists(): + log("[自动 S/V] 已有图片但缺少 S/V 元数据,重新计算") outputs = pipeline.run_postprocessors_for_source( filename, "Baidu_API", diff --git a/web_dehaze/static/app.js b/web_dehaze/static/app.js index ce9920d..e41fee4 100644 --- a/web_dehaze/static/app.js +++ b/web_dehaze/static/app.js @@ -28,6 +28,56 @@ function formatBytes(bytes) { return `${(bytes / 1024 / 1024).toFixed(1)} MB`; } +function formatGain(value) { + const number = Number(value); + return Number.isFinite(number) ? number.toFixed(3) : ""; +} + +function svSummary(meta = {}) { + const s = formatGain(meta.s_gain); + const v = formatGain(meta.v_gain); + if (!s || !v) return ""; + return `S ${s} · V ${v}`; +} + +function updateGainLabels() { + $("sGainValue").textContent = formatGain($("sGain").value); + $("vGainValue").textContent = formatGain($("vGain").value); +} + +function setOnlyPostprocessor(processor) { + [...$("postList").querySelectorAll("input[type=checkbox]")].forEach((input) => { + input.checked = input.value === processor; + }); +} + +function setSelectIfOption(selectId, value) { + const select = $(selectId); + if ([...select.options].some((option) => option.value === value)) { + select.value = value; + } +} + +function applySvFromResult(item) { + const meta = item.meta || {}; + const sGain = Number(meta.s_gain); + const vGain = Number(meta.v_gain); + if (!Number.isFinite(sGain) || !Number.isFinite(vGain)) return; + + $("sGain").value = Math.min(2.5, Math.max(0, sGain)); + $("vGain").value = Math.min(2.5, Math.max(0, vGain)); + updateGainLabels(); + setOnlyPostprocessor("manual_sv"); + if (item.source) { + setSelectIfOption("postSource", item.source); + } + if (item.reference) { + setSelectIfOption("referenceImage", item.reference); + } + $("logBox").textContent = `已载入 ${item.name} 的参数:S=${formatGain(sGain)},V=${formatGain(vGain)}\n可在左侧手动微调后点击“生成后处理”。`; + setJobState("SV Ready", "done"); +} + function renderImages() { const box = $("imageList"); box.innerHTML = ""; @@ -104,14 +154,26 @@ async function selectImage(name) { await refreshResults(); } -function resultCard(title, path, exists = true) { +function resultCard(title, path, exists = true, item = null) { const card = document.createElement("article"); - card.className = "result-card"; + const summary = item ? svSummary(item.meta) : ""; + card.className = `result-card ${summary ? "clickable" : ""}`; const badge = exists ? 'ready' : '未生成'; const body = exists - ? `