File size: 3,403 Bytes
3af6d41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Measure per-region SSIM for every declared desktop visual checkpoint.

Reads the visual spec (scope/desktop-visual-comparison-current.json), crops
source and candidate rasters to each declared region bbox, and prints a
region-by-region SSIM table plus the checkpoint average. Diagnostic-only.

Usage:
    .venv/bin/python materials/33/clone/measure_visual_regions.py [--json OUT]
"""

from __future__ import annotations

import argparse
import json
import sys
from pathlib import Path

import numpy as np

SITE_ROOT = Path(__file__).resolve().parents[1]
SPEC_PATH = SITE_ROOT / "scope" / "desktop-visual-comparison-current.json"


def _load_rgb(path: Path) -> np.ndarray:
    from PIL import Image

    with Image.open(path) as image:
        return np.asarray(image.convert("RGB"), dtype=np.uint8)


def _ssim(a: np.ndarray, b: np.ndarray) -> float:
    from skimage.metrics import structural_similarity as ssim_fn

    return round(float(ssim_fn(a, b, channel_axis=2, data_range=255)), 4)


def main() -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--json", dest="json_out", help="write JSON summary to this path")
    args = parser.parse_args()

    spec = json.loads(SPEC_PATH.read_text(encoding="utf-8"))
    summary: dict[str, object] = {"checkpoints": []}
    all_regions: list[float] = []
    for checkpoint in spec["checkpoints"]:
        checkpoint_id = checkpoint["id"]
        source_path = (SPEC_PATH.parent / checkpoint["source"]["path"]).resolve()
        candidate_path = (SPEC_PATH.parent / checkpoint["candidate"]["path"]).resolve()
        source = _load_rgb(source_path)
        candidate = _load_rgb(candidate_path)
        if source.shape != candidate.shape:
            print(
                f"!! {checkpoint_id}: size mismatch "
                f"{source.shape} vs {candidate.shape}",
                file=sys.stderr,
            )
            continue
        rows: list[dict[str, object]] = []
        region_scores: list[float] = []
        for region in checkpoint["regions"]:
            x, y = region["x"], region["y"]
            w, h = region["width"], region["height"]
            score = _ssim(source[y : y + h, x : x + w], candidate[y : y + h, x : x + w])
            rows.append(
                {
                    "checkpoint": checkpoint_id,
                    "region": region["id"],
                    "bbox": [x, y, w, h],
                    "ssim": score,
                }
            )
            region_scores.append(score)
            all_regions.append(score)
        average = round(float(np.mean(region_scores)), 4) if region_scores else None
        print(f"{checkpoint_id}: avg={average}")
        for row in rows:
            print(f"   {row['region']:<20} {row['ssim']}")
        summary["checkpoints"].append(
            {
                "id": checkpoint_id,
                "regions": rows,
                "average_ssim": average,
            }
        )
    summary["overall_region_average"] = round(float(np.mean(all_regions)), 4)
    print(f"overall region average: {summary['overall_region_average']}")

    if args.json_out:
        out = Path(args.json_out)
        out.parent.mkdir(parents=True, exist_ok=True)
        out.write_text(json.dumps(summary, indent=2) + "\n", encoding="utf-8")
        print(f"wrote {out}")
    return 0


if __name__ == "__main__":
    sys.exit(main())