Datasets:
File size: 3,660 Bytes
8ca7ba0 | 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 | """Frozen-region texture checks, without registration or exposure fitting."""
import json
from pathlib import Path
import numpy as np
from PIL import Image
CONTRACT = Path(__file__).resolve().parents[2] / 'scripts/source_transfer/texture-fidelity-contract.json'
def compare(reference, candidate, view, contract=None):
contract = contract or json.loads(CONTRACT.read_text(encoding='utf-8'))
a, b = [np.asarray(Image.open(p).convert('RGB'), dtype=float) for p in (reference, candidate)]
if a.shape != b.shape or a.shape[:2] != (720, 960):
raise ValueError('Frozen texture regions require matching 960x720 images')
limits = contract['thresholds']
rows = []
for label, (x0, y0, x1, y1) in contract['regions'][view].items():
gradients = []
for image in (a, b):
lum = image[y0:y1, x0:x1] @ np.array([.2126, .7152, .0722])
gradients.append(np.stack(((lum[1:-1,2:]-lum[1:-1,:-2])*.5,
(lum[2:,1:-1]-lum[:-2,1:-1])*.5), axis=-1))
ga, gb = gradients
ea, eb = [float(np.mean(g*g)) for g in gradients]
ratio = eb/ea if ea > 1e-8 else None
cosine = float(np.mean(ga*gb)/np.sqrt(ea*eb)) if ea*eb > 1e-16 else None
passed = (ratio is not None and cosine is not None and
limits['gradient_energy_ratio_min'] <= ratio <= limits['gradient_energy_ratio_max'] and
cosine >= limits['gradient_cosine_min'])
rows.append(dict(region=label, box=[x0,y0,x1,y1], reference_energy=ea,
candidate_energy=eb, gradient_energy_ratio=ratio,
gradient_cosine=cosine, passed=passed,
enough_texture_evidence=ratio is not None and cosine is not None))
return dict(view=view, mae=float(abs(a-b).mean()), regions=rows,
clarity_passed=bool(rows) and all(r['passed'] for r in rows))
def compare_directories(reference, candidate, output, contract=None):
contract = contract or json.loads(CONTRACT.read_text(encoding='utf-8'))
rows = [compare(Path(reference)/(v+'.png'), Path(candidate)/(v+'.png'), v, contract)
for v in contract['regions']]
result = dict(contract=contract, views=rows,
clarity_passed=all(r['clarity_passed'] for r in rows),
mean_error_passed=all(r['mae'] <= contract['thresholds']['full_image_rgb_mae_max'] for r in rows))
Path(output).write_text(json.dumps(result, indent=2), encoding='utf-8')
return result
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Grade complete frozen render regions without image fitting')
parser.add_argument('--reference', type=Path, required=True)
parser.add_argument('--candidate', type=Path, required=True)
parser.add_argument('--output', type=Path, required=True)
parser.add_argument('--contract', type=Path, default=CONTRACT)
args = parser.parse_args()
if args.output.exists():
parser.error('Output already exists; retain previous results and choose a new path')
args.output.parent.mkdir(parents=True, exist_ok=True)
result = compare_directories(args.reference, args.candidate, args.output,
json.loads(args.contract.read_text(encoding='utf-8')))
passed = result['clarity_passed'] and result['mean_error_passed']
print(json.dumps(dict(passed=passed, views=len(result['views']),
clarity_passed=result['clarity_passed'],
mean_error_passed=result['mean_error_passed'])))
raise SystemExit(0 if passed else 1)
|