feat(cv): qualify templates and reliable local import processing
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
"""Separate DOM Range/PDF character-cell metrics from visible raster ink alignment.
|
||||
|
||||
This leaves the original 2mm fidelity oracle and all its failures unchanged.
|
||||
"""
|
||||
import argparse
|
||||
import json
|
||||
import math
|
||||
import re
|
||||
|
||||
from pathlib import Path
|
||||
from PIL import Image
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("directory", type=Path)
|
||||
args = parser.parse_args()
|
||||
source = json.loads((args.directory / "measurements.json").read_text(encoding="utf-8"))
|
||||
oracle = json.loads((args.directory / "fidelity-results.json").read_text(encoding="utf-8"))
|
||||
results = []
|
||||
for fixture in source["fixtures"]:
|
||||
directory = args.directory / fixture["fixture"]
|
||||
marker = next(item for item in fixture["measured"]["tokens"] if item["token"] == "S01")
|
||||
rect = next(item for item in fixture["measured"]["nameLines"] if item["text"] == "S01")
|
||||
foreground = tuple(map(int, re.findall(r"\d+", marker["color"])[:3]))
|
||||
bounds = (math.floor(rect["x"]), math.floor(rect["y"]), math.ceil(rect["x"] + rect["width"]), math.ceil(rect["y"] + rect["height"] + 12))
|
||||
boxes = {}
|
||||
for kind, filename in (("preview", "preview-1.png"), ("pdf", "pdf-1.png")):
|
||||
with Image.open(directory / filename) as image:
|
||||
crop = image.convert("RGB").crop(bounds)
|
||||
pixels = [(x, y) for y in range(crop.height) for x in range(crop.width)
|
||||
if max(abs(a - b) for a, b in zip(crop.getpixel((x, y)), foreground)) <= 50]
|
||||
if not pixels:
|
||||
raise ValueError(f"No foreground ink for {fixture['fixture']} {kind}")
|
||||
boxes[kind] = [min(x for x, y in pixels) + bounds[0], min(y for x, y in pixels) + bounds[1],
|
||||
max(x for x, y in pixels) + bounds[0], max(y for x, y in pixels) + bounds[1]]
|
||||
crop.resize((crop.width * 4, crop.height * 4)).save(directory / f"S01-{kind}-ink.png")
|
||||
original = next(item for item in oracle["fixtures"] if item["fixture"] == fixture["fixture"])
|
||||
comparison = next(item for item in original["comparisons"] if item["token"] == "S01")
|
||||
results.append({"fixture": fixture["fixture"], "domRange": rect, "foregroundRgb": foreground,
|
||||
"originalOracleRangeVsPdfCharacterCellDeltaMm": comparison["deltaYmm"],
|
||||
"visibleInkBoundsPx": boxes, "visibleInkTopDeltaPx": abs(boxes["preview"][1] - boxes["pdf"][1]),
|
||||
"visibleInkTopDeltaMm": abs(boxes["preview"][1] - boxes["pdf"][1]) * 25.4 / 96})
|
||||
report = {"qualification": "Separate diagnostic measurements only. Pixels within 50 per RGB channel of the recorded foreground identify visible ink in the S01 ROI. Original fidelity tolerances/failures are unchanged.",
|
||||
"fixtures": results, "maxVisibleInkTopDeltaPx": max(item["visibleInkTopDeltaPx"] for item in results)}
|
||||
(args.directory / "name-box-metrics.json").write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding="utf-8")
|
||||
print(f"S01 raster-ink diagnostic: {len(results)} documents, max top delta {report['maxVisibleInkTopDeltaPx']}px; original oracle unchanged")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user