Files
ycode/scripts/compare-source-select-pixels.py

34 lines
2.0 KiB
Python

"""Compare exact same-state native crops. Original PNGs are exclusively saved REA frames."""
import json
from pathlib import Path
from PIL import Image, ImageChops
root = Path(__file__).resolve().parents[1]
output = root / '.tmp/source-select-native-20261016'
results = []
for query, own in [('english', 'provider-custom-query'), ('empty', 'provider-empty-query')]:
source = root / f'.tmp/rea-select-search-{query}-20261016'
layout = json.loads((source / 'layout.json').read_text(encoding='utf-8'))
frame = layout['probe']['frames'][0]
assert frame['step_id'] == {'english': 'query-custom-result', 'empty': 'query-empty-result'}[query]
region = next(region for region in frame['regions'] if region['selector'] == '.wb-select-popover')
x, y, width, height = region['bounds']
assert [x, y, width, height] == [154, 176, 592, 120 if query == 'english' else 96]
box = tuple(round(value) for value in (x, y, x + width, y + height))
original = Image.open(source / 'frame-000.png').convert('RGB').crop(box)
restored = Image.open(output / (own + '.png')).convert('RGB').crop(box)
delta = ImageChops.difference(original, restored)
pixels = list(delta.get_flattened_data())
result = {'state': query, 'sourceEvidenceId': layout['evidenceId'], 'box': box,
'pixels': len(pixels), 'different': sum(any(pixel) for pixel in pixels),
'maxChannel': max(max(pixel) for pixel in pixels),
'meanChannel': sum(sum(pixel) for pixel in pixels) / (len(pixels) * 3),
'mask': None, 'tolerance': 0}
results.append(result)
delta.point(lambda value: min(255, value * 8)).save(output / (query + '-diff.png'))
original.save(output / (query + '-source-crop.png'))
restored.save(output / (query + '-own-crop.png'))
(output / 'pixels.json').write_text(json.dumps(results, indent=2), encoding='utf-8')
print(json.dumps(results, ensure_ascii=False))
assert all(result['different'] == 0 for result in results), 'Exact crop parity changed; inspect differences without increasing tolerance'