"""Recompute the published SYNTHETIC coordinate audit. Python standard library only. Run: python3 reproduce.py [directory containing dataset.json and results.json] No photos, detector models, biometric data or network requests are used. """ import json import math import sys from pathlib import Path def quantize(value): # Match the engine's half-away-from-zero rounding of normalized input. return math.copysign(math.floor(abs(value) * 1_000_000 + 0.5), value) / 1_000_000 def measure(case, dataset, rounded=True): width, height = case['imageWidth'], case['imageHeight'] points = {int(i): (quantize(p['x']) * width, quantize(p['y']) * height) if rounded else (p['x'] * width, p['y'] * height) for i, p in case['inputCoordinates'].items()} right, left = [points[i] for i in dataset['iris']] dx, dy = left[0] - right[0], left[1] - right[1] length = math.hypot(dx, dy) if length <= 0: raise ValueError('Coincident pupil coordinates') ox, oy = (right[0] + left[0]) / 2, (right[1] + left[1]) / 2 points = {i: ((dx * (x - ox) + dy * (y - oy)) / length, (-dy * (x - ox) + dx * (y - oy)) / length) for i, (x, y) in points.items()} mid = sum(points[i][0] for i in dataset['midline']) / len(dataset['midline']) cheek_r, cheek_l = [points[i] for i in dataset['cheeks']] face_width = math.dist(cheek_r, cheek_l) if face_width <= 0: raise ValueError('Zero face width') gaps = [math.dist(points[r], (2 * mid - points[l][0], points[l][1])) for r, l in dataset['pairs']] return sum(gaps) / (len(gaps) * face_width) if __name__ == '__main__': root = Path(sys.argv[1]) if len(sys.argv) > 1 else Path(__file__).resolve().parent data = json.loads((root / 'dataset.json').read_text()) results = json.loads((root / 'results.json').read_text()) assert data['synthetic'] and results['humanSubjects'] == 0 errors = [] for case in data['cases']: value = measure(case, data) error = abs(value - case['asymmetryIndex']) assert error < 1e-12, (case['id'], error) if case['expected'] is not None: assert abs(value - case['expected']) < 1e-12 assert abs(measure(case, data, False) - case['withoutInputQuantization']) < 1e-12 errors.append(error) assert len(errors) == results['caseCount'] print(json.dumps({'verifiedCases': len(errors), 'maxErrorAgainstPinnedEngine': max(errors), 'synthetic': True, 'photosProcessed': 0, 'detectorAccuracyTested': False}, indent=2))