"""Load RGB Stokes/S2L2 data and masks from either polarization subset.""" from pathlib import Path import argparse import csv import numpy as np import OpenImageIO as oiio from PIL import Image SUBSETS = ('full_stokes', 'linear_stokes') def load_scene(root, scene_id, representation='thetaphi', subset=None): if representation not in {'thetaphi', 'duff', 's2l2'}: raise ValueError('representation must be thetaphi, duff, or s2l2') root = Path(root) if subset is not None: if subset not in SUBSETS: raise ValueError(f'subset must be one of {SUBSETS}') root = root / subset if not (root / 'metadata.csv').is_file(): raise ValueError('Choose a subset with subset="full_stokes" or "linear_stokes", or pass its directory as root.') with (root / 'metadata.csv').open(newline='', encoding='utf-8') as f: rows = [r for r in csv.DictReader(f) if int(r['scene_id']) == int(scene_id)] if len(rows) != 1: raise KeyError(f'Unknown scene ID: {scene_id}') metadata = rows[0] metadata['s3_measured'] = metadata['s3_measured'].lower() == 'true' if not metadata[representation].strip(): raise FileNotFoundError(f'Scene {scene_id} has no released data files; its metadata file paths are empty.') path = root / metadata[representation] inp = oiio.ImageInput.open(str(path)) if inp is None: raise IOError(f'Cannot open {path}') try: spec = inp.spec() names = list(spec.channelnames) pixels = inp.read_image(format=oiio.FLOAT) if pixels is None: raise IOError(f'Cannot decode {path}') count = 12 if representation == 's2l2' else 4 parameters = [f'S2L2{i:02d}' for i in range(count)] if representation == 's2l2' else [f'S{i}' for i in range(count)] data = np.stack([np.stack([pixels[..., names.index(f'{p}.{c}')] for p in parameters], axis=-1) for c in 'RGB'], axis=-2) finally: inp.close() result = {'data': data, 'metadata': metadata, 's3_measured': metadata['s3_measured']} for name in ['privacy_mask', 'saturation_mask', 'invalid_mask']: with Image.open(root / metadata[name]) as image: result[name] = np.asarray(image) != 0 if result[name].shape != data.shape[:2]: raise ValueError(f'Mask dimensions do not match EXR: {name}') return result if __name__ == '__main__': parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--root', default='.') parser.add_argument('--subset', choices=SUBSETS) parser.add_argument('--scene-id', required=True, type=int) parser.add_argument('--representation', default='thetaphi', choices=['thetaphi', 'duff', 's2l2']) args = parser.parse_args() scene = load_scene(args.root, args.scene_id, args.representation, args.subset) print({'scene_id': args.scene_id, 'shape': scene['data'].shape, 'dtype': str(scene['data'].dtype), 's3_measured': scene['s3_measured'], 'privacy_review_status': scene['metadata']['privacy_review_status']})