Files
d2r-mobile/dm/verify/shotgate.py
T
2026-10-12 16:51:08 +08:00

171 lines
15 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 对真值的画面闸门:同一关键画面,真值(Ryujinx 跑官方 94012)截图 vs 宿主截图,判「整块缺失 / 缺光照 / 结构走样」。
# 用法:shotgate.py <页规格.json> <真值.png> <宿主.png> <报告目录> → 报告目录/report.html(并排两图 + 差异热图 + 读数 + 应有元素表)、result.json
# 末行 SHOTGATE <页> OK|RED …;退出码 0 = 绿,1 = 红
# 不要求逐像素相同(两边 GPU、着色器链都不同)。四条判据(任一条红即红;阈值在页规格里,缺省见 DEF,标定记录见 dm/verify/screenshot_gate.md):
# ① 结构相似度(灰度、缩到 320×180、7×7 窗口;遮罩区不计)≥ ssim_min
# ② 3D 视口非黑像素比例(亮度 > 0.06):宿主 ≥ 真值 × nonblack_ratio_min
# ③ 8×8 网格逐格:平均亮度差 |ΔL| ≤ cell_dl_max 且平均颜色距离 ≤ cell_dc_max(整格被遮罩的跳过);红格进热图
# ④ 命名区域(角色、篝火、菜单……):区域平均亮度 宿主/真值 ∈ [region_ratio_min, 1/region_ratio_min],区域结构相似度 ≥ region_ssim_min
# 应有元素表逐项按其区域的 ④ 结论打勾;「不应有」的元素(如推广框)要求该区域与真值一致(真值里本来就没有)。
# Screenshot gate against ground truth: for the same key screen, the ground-truth screenshot (Ryujinx running the official 94012) vs the host screenshot, judging "whole block missing / lighting missing / structure distorted".
# Usage: shotgate.py <page spec.json> <ground truth.png> <host.png> <report dir> → <report dir>/report.html (two images side by side + difference heat map + readings + expected-elements table), result.json
# last line SHOTGATE <page> OK|RED ...; exit code 0 = pass, 1 = fail
# Pixel-identical output is not required (GPU and shader chain differ on the two sides). Four criteria (any one failing means the page fails; thresholds are in the page spec, defaults in DEF, calibration records in dm/verify/screenshot_gate.md):
# ① Structural similarity (grayscale, scaled to 320×180, 7×7 window; masked areas excluded) ≥ ssim_min
# ② Share of non-black pixels in the 3D viewport (brightness > 0.06): host ≥ ground truth × nonblack_ratio_min
# ③ 8×8 grid, per cell: mean brightness difference |ΔL| ≤ cell_dl_max and mean color distance ≤ cell_dc_max (fully masked cells are skipped); failing cells go into the heat map
# ④ Named regions (character, campfire, menu...): region mean brightness host/ground truth ∈ [region_ratio_min, 1/region_ratio_min], region structural similarity ≥ region_ssim_min
# Each item in the expected-elements table is ticked by the ④ result of its region; "must not be present" elements (such as the promotion box) require that region to match the ground truth (where it is absent to begin with).
import sys, os, json, html, base64, io
import numpy as np
from PIL import Image
DEF = dict(ssim_min=0.60, nonblack_ratio_min=0.90, cell_dl_max=0.10, cell_dc_max=0.16, region_ratio_min=0.75, region_ssim_min=0.45)
W, H = 320, 180
def load(p):
im = Image.open(p).convert('RGB').resize((W, H), Image.BILINEAR)
return np.asarray(im, dtype=np.float64) / 255.0
def lum(a): return a[..., 0] * 0.2126 + a[..., 1] * 0.7152 + a[..., 2] * 0.0722
def box(a, r=3):
"""(2r+1)² 盒滤波(积分图),边缘按有效像素数归一。 | (2r+1)² box filter (integral image), edges normalized by the number of valid pixels."""
p = np.pad(a, r, mode='edge'); c = p.cumsum(0).cumsum(1); c = np.pad(c, ((1, 0), (1, 0)))
k = 2 * r + 1
return (c[k:, k:] - c[:-k, k:] - c[k:, :-k] + c[:-k, :-k]) / (k * k)
def ssim_map(x, y):
C1, C2 = 0.01 ** 2, 0.03 ** 2
mx, my = box(x), box(y)
vx, vy, cxy = box(x * x) - mx * mx, box(y * y) - my * my, box(x * y) - mx * my
return ((2 * mx * my + C1) * (2 * cxy + C2)) / ((mx * mx + my * my + C1) * (vx + vy + C2))
def rect_px(r): return int(r[0] * W), int(r[1] * H), max(int(r[0] * W) + 1, int(r[2] * W)), max(int(r[1] * H) + 1, int(r[3] * H))
def main():
spec_p, tp, hp, out = sys.argv[1:5]
spec = json.load(open(spec_p, encoding='utf-8')); th = {**DEF, **spec.get('thresholds', {})}
os.makedirs(out, exist_ok=True)
T, Hs = load(tp), load(hp); LT, LH = lum(T), lum(Hs)
mask = np.zeros((H, W), bool)
for r in spec.get('mask', []):
x0, y0, x1, y1 = rect_px(r); mask[y0:y1, x0:x1] = True
# 动画遮罩(页规格 dynmask = 遮罩 png,dynmask.py 由同一冻结拍两张截图之差生成;白 = 客户端在动的像素):并入遮罩,射程报占视口比例;
# 占视口 > 30% 判据失明,标「弱判」
# Animation mask (page spec dynmask = mask png, generated by dynmask.py from the difference of two screenshots at the same frozen tick; white = pixels the client is animating): merged into the mask, coverage reports its share of the viewport;
# above 30% of the viewport the criterion is blind, marked 「弱判」(weak verdict)
dyn = None
if spec.get('dynmask'):
dm = np.asarray(Image.open(os.path.expanduser(spec['dynmask'])).convert('L').resize((W, H), Image.BOX), dtype=np.float64) > 0
mask |= dm
vx0_, vy0_, vx1_, vy1_ = rect_px(spec.get('viewport', [0, 0, 1, 1])); dyn = float(dm[vy0_:vy1_, vx0_:vx1_].mean())
keep = ~mask
S = ssim_map(LT, LH); ssim = float(S[keep].mean())
vx0, vy0, vx1, vy1 = rect_px(spec.get('viewport', [0, 0, 1, 1]))
nb_t = float((LT[vy0:vy1, vx0:vx1] > 0.06).mean()); nb_h = float((LH[vy0:vy1, vx0:vx1] > 0.06).mean())
# ③ 网格
# ③ Grid
G = 8; cells = []; red_cells = 0; heat = np.zeros((G, G))
for gy in range(G):
for gx in range(G):
ys, xs = slice(gy * H // G, (gy + 1) * H // G), slice(gx * W // G, (gx + 1) * W // G)
k = keep[ys, xs]
if k.mean() < 0.5: cells.append(None); heat[gy, gx] = -1; continue
dl = float(abs(LT[ys, xs][k].mean() - LH[ys, xs][k].mean()))
dc = float(np.linalg.norm(T[ys, xs][k].mean(0) - Hs[ys, xs][k].mean(0)))
bad = dl > th['cell_dl_max'] or dc > th['cell_dc_max']; red_cells += bad
heat[gy, gx] = max(dl / th['cell_dl_max'], dc / th['cell_dc_max']); cells.append((dl, dc, bad))
# ④ 命名区域
# ④ Named regions
regs = {}
for name, r in spec.get('regions', {}).items():
rth = {}
if isinstance(r, dict): rth = r; r = r['rect'] # 区域可单设阈值:{"rect": [...], "ratio_min": …, "ssim_min": …}(动画区如装载框里的视频) | A region can set its own thresholds: {"rect": [...], "ratio_min": ..., "ssim_min": ...} (animated regions such as the video in the loading frame)
x0, y0, x1, y1 = rect_px(r)
lt, lh = float(LT[y0:y1, x0:x1].mean()), float(LH[y0:y1, x0:x1].mean())
ratio = lh / lt if lt > 1e-4 else (1.0 if lh < 1e-4 else 99.0)
rs = float(S[y0:y1, x0:x1].mean())
rmin = rth.get('ratio_min', th['region_ratio_min']); smin = rth.get('ssim_min', th['region_ssim_min'])
ok = (rmin <= 0 or rmin <= ratio <= 1 / rmin) and rs >= smin # ratio_min 0 = 只比结构不比亮度(动画区) | ratio_min 0 = compare structure only, not brightness (animated regions)
regs[name] = dict(truth_lum=round(lt, 4), host_lum=round(lh, 4), ratio=round(ratio, 3), ssim=round(rs, 3), ok=ok)
# ⑤ 细网格(页规格 fine_grid = [列, 行, 结构下限];动画遮罩过半的格用 dyn_ssim_min):640×360 灰度逐格平均 SSIM,抓「一个小元素没画出来」这类大区域平均掩盖的缺失
# ⑤ Fine grid (page spec fine_grid = [columns, rows, structure floor]; cells more than half covered by the animation mask use dyn_ssim_min): per-cell mean SSIM on 640×360 grayscale, catches omissions like "one small element was not drawn" that large-region averages hide
fine = None
if spec.get('fine_grid'):
fc, fr, fmin = spec['fine_grid']
def load2(p):
a = np.asarray(Image.open(p).convert('RGB').resize((640, 360), Image.BILINEAR), dtype=np.float64) / 255.0; return lum(a)
S2 = ssim_map(load2(tp), load2(hp)); bad_cells = []
m2 = np.zeros((360, 640), bool)
for r in spec.get('mask', []):
m2[int(r[1] * 360):max(int(r[1] * 360) + 1, int(r[3] * 360)), int(r[0] * 640):max(int(r[0] * 640) + 1, int(r[2] * 640))] = True
dm2 = (np.asarray(Image.open(os.path.expanduser(spec['dynmask'])).convert('L').resize((640, 360), Image.BOX)) > 0) if spec.get('dynmask') else np.zeros((360, 640), bool)
dmin2 = th.get('dyn_ssim_min', 0.5)
for gy in range(fr):
for gx in range(fc):
ys, xs = slice(gy * 360 // fr, (gy + 1) * 360 // fr), slice(gx * 640 // fc, (gx + 1) * 640 // fc)
k = ~m2[ys, xs]
cx, cy = (gx + 0.5) / fc, (gy + 0.5) / fr; vp = spec.get('viewport', [0, 0, 1, 1])
if k.mean() < 0.5 or not (vp[0] <= cx <= vp[2] and vp[1] <= cy <= vp[3]): continue # 只看视口内(面板外露出的 3D 背景会随墙钟动) | Only look inside the viewport (the 3D background showing outside the panel moves with the wall clock)
v = float(S2[ys, xs][k].mean())
lim = dmin2 if dm2[ys, xs].mean() >= 0.5 else fmin # 动画遮罩盖住过半的格:不比像素级,结构下限放到 dyn_ssim_min | Cells more than half covered by the animation mask: no pixel-level comparison, the structure floor is relaxed to dyn_ssim_min
if v < lim: bad_cells.append((gx, gy, round(v, 3)))
fine = (fc, fr, fmin, bad_cells)
crit = [
('① 结构相似度', ssim >= th['ssim_min'], f'{ssim:.3f}(≥ {th["ssim_min"]})'),
('② 3D 视口非黑比例', nb_h >= nb_t * th['nonblack_ratio_min'], f'宿主 {nb_h:.3f} / 真值 {nb_t:.3f}(宿主 ≥ 真值 × {th["nonblack_ratio_min"]})'),
('③ 8×8 网格', red_cells == 0, f'红格 {red_cells} / {sum(c is not None for c in cells)}(|ΔL| ≤ {th["cell_dl_max"]}、色差 ≤ {th["cell_dc_max"]})'),
('④ 命名区域', all(v['ok'] for v in regs.values()), ';'.join(f'{k} 亮度比 {v["ratio"]} 结构 {v["ssim"]}{"" if v["ok"] else " ✗"}' for k, v in regs.items()) or '无'),
]
if dyn is not None:
crit.append(('⑥ 动画遮罩射程', True, f'遮罩占视口 {dyn:.1%}' + ('(> 30%:严格判据失明,本页为「弱判」)' if dyn > 0.30 else '')))
if fine:
fc, fr, fmin, bc = fine
# fine_red_max(页规格,缺省 0):允许的红格数。只给有「客户端环境小动物」的页用——它不在服务端状态里,位置随客户端帧游走,
# 真值与宿主各在各的随机位置,正好落在某格中央时该格结构掉到约 0.58(NPC 对话页的鸡);该页关键元素另有命名区域把关
# fine_red_max (page spec, default 0): number of failing cells allowed. Only for pages with "client-side ambient critters" - they are not in server state and their position wanders with client frames,
# ground truth and host each have them at their own random positions, and when one lands right in the middle of a cell that cell's structure drops to about 0.58 (the chicken on the NPC dialogue page); that page's key elements are separately guarded by named regions
fmax = int(spec.get('fine_red_max', 0))
crit.append(('⑤ 细网格', len(bc) <= fmax, f'{fc}×{fr} 格结构 ≥ {fmin}:红格 {len(bc)}' + (f'(本页允许 ≤ {fmax})' if fmax else '') + (f',最差 {sorted(bc, key=lambda t: t[2])[:3]}(列, 行, 值)' if bc else '')))
elems = []
for e in spec.get('elements', []):
r = regs.get(e['region']); ok = bool(r and r['ok']) if e['region'] != 'viewport' else crit[1][1]
elems.append((e['name'], e.get('expect', '有'), ok))
green = all(c[1] for c in crit) and all(e[2] for e in elems)
# 热图:绿 → 黄 → 红(≥1 即超阈值),遮罩格灰
# Heat map: green → yellow → red (≥1 means over threshold), masked cells gray
hm = np.zeros((G, G, 3))
for gy in range(G):
for gx in range(G):
v = heat[gy, gx]
hm[gy, gx] = (0.5, 0.5, 0.5) if v < 0 else ((min(1, v), 1 - min(1, v) * 0.8, 0.1) if v < 1 else (0.9, 0.05, 0.05))
hmi = Image.fromarray((hm * 255).astype(np.uint8)).resize((W * 2, H * 2), Image.NEAREST)
base = Image.fromarray((Hs * 255).astype(np.uint8)).resize((W * 2, H * 2)).convert('RGB')
over = Image.blend(base, hmi, 0.55); over.save(os.path.join(out, 'heat.png'))
res = dict(page=spec['page'], dynmask_ratio=dyn, truth=os.path.abspath(tp), host=os.path.abspath(hp), green=green, ssim=round(ssim, 4),
nonblack=dict(truth=round(nb_t, 4), host=round(nb_h, 4)), red_cells=red_cells, regions=regs,
elements=[dict(name=n, expect=x, ok=o) for n, x, o in elems], thresholds=th)
json.dump(res, open(os.path.join(out, 'result.json'), 'w'), ensure_ascii=False, indent=1)
def b64(p, w=800):
im = Image.open(p).convert('RGB'); im.thumbnail((w, w)); bio = io.BytesIO(); im.save(bio, 'PNG')
return base64.b64encode(bio.getvalue()).decode()
rows = ''.join(f'<tr><td>{html.escape(n)}</td><td class={"ok" if ok else "bad"}>{"绿" if ok else "红"}</td><td>{html.escape(d)}</td></tr>' for n, ok, d in crit)
erows = ''.join(f'<tr><td>{html.escape(n)}</td><td>{html.escape(x)}</td><td class={"ok" if ok else "bad"}>{"✓" if ok else "✗"}</td></tr>' for n, x, ok in elems)
page = f'''<!doctype html><meta charset=utf-8><title>画面闸门 {html.escape(spec["page"])}</title>
<style>body{{font:14px -apple-system,sans-serif;margin:16px;background:#fff;color:#111}}img{{width:100%;border:1px solid #ccc}}
.g{{display:grid;grid-template-columns:repeat(auto-fit,minmax(300px,1fr));gap:10px}}td,th{{border:1px solid #ccc;padding:4px 8px}}table{{border-collapse:collapse}}
.ok{{color:#070;font-weight:600}}.bad{{color:#c00;font-weight:600}}h1 span{{font-size:18px}}</style>
<h1>{html.escape(spec["page"])} <span class={"ok" if green else "bad"}>{"绿" if green else "红"}</span></h1>
<div class=g><div><b>真值</b><img src="data:image/png;base64,{b64(tp)}"></div><div><b>宿主</b><img src="data:image/png;base64,{b64(hp)}"></div>
<div><b>差异热图(叠在宿主上;红 = 超阈值格,灰 = 遮罩)</b><img src="data:image/png;base64,{b64(os.path.join(out, "heat.png"))}"></div></div>
<h2>判据</h2><table><tr><th>判据</th><th>结论</th><th>读数</th></tr>{rows}</table>
<h2>应有元素</h2><table><tr><th>元素</th><th>应</th><th>结论</th></tr>{erows}</table>
<p>真值 {html.escape(os.path.abspath(tp))}<br>宿主 {html.escape(os.path.abspath(hp))}</p>'''
open(os.path.join(out, 'report.html'), 'w', encoding='utf-8').write(page)
print(f'SHOTGATE {spec["page"]} {"OK" if green else "RED"} 结构 {ssim:.3f} 非黑 宿主 {nb_h:.3f}/真值 {nb_t:.3f} 红格 {red_cells} '
+ ' '.join(f'{k}:{v["ratio"]}/{v["ssim"]}{"" if v["ok"] else "✗"}' for k, v in regs.items()) + (f' 细网格红格 {len(fine[3])}' if fine else '') + (f' 动画遮罩 {dyn:.1%}' + ('【弱判】' if dyn > 0.30 else '') if dyn is not None else '') + f' → {out}/report.html')
sys.exit(0 if green else 1)
if __name__ == '__main__': main()