from __future__ import annotations import json from pathlib import Path import numpy as np from PIL import Image, ImageFilter GRID_HW = (294, 518) DATASET_DIR = 'masks/predicted' DATASET_FIELD = 'occluder_pred_hard' SOURCES = {'waymo_front_crop169_v1': 'waymo', 'waymo': 'waymo', 'nuscenes_cam_front_v1': 'nuscenes', 'nuscenes_front_v1': 'nuscenes', 'nuscenes': 'nuscenes'} def binary(mask): mask = np.asarray(mask) if mask.shape != GRID_HW or not np.isin(mask, (0, 1)).all(): raise ValueError('mask must be binary [294,518]') return mask.astype(bool) def dilate_mask(occluder, pixels): if type(pixels) is not int or pixels < 0: raise ValueError('mask dilation must be a nonnegative integer') if pixels == 0: return occluder.astype(bool).copy() return np.asarray(Image.fromarray(occluder.astype(np.uint8)).filter(ImageFilter.MaxFilter(2 * pixels + 1))).astype(bool) def source_of(clip): source = clip.get('source_id') or clip.get('source') or clip.get('sample_key', {}).get('source_id') if source is None: source = SOURCES.get(clip.get('sample_key', {}).get('dataset_id', '')) if source is None: raise ValueError('clip must carry source_id') return source class DatasetMasks: def __init__(self, data_root, dilate=0, directory=DATASET_DIR): self.root = Path(data_root) / directory self.dilate = dilate def load(self, clip): import h5py with h5py.File(self.root / source_of(clip) / clip['scene_id'] / 'mask.h5', 'r') as f: occluder = f[DATASET_FIELD][:] if occluder.dtype != np.bool_: raise ValueError('stored mask must be bool') occluder = binary(occluder) return occluder, dilate_mask(binary(occluder), self.dilate) class MaskDir: def __init__(self, directory, dilate=0): self.directory = Path(directory) self.dilate = dilate self.manifest = json.loads((self.directory / 'manifest.json').read_text()) if self.manifest.get('grid_hw') != list(GRID_HW) or self.manifest.get('context_positions') != [0, 5, 10, 15]: raise ValueError('mask manifest grid/context mismatch') def load(self, clip): with np.load(self.directory / f'{clip["clip_id"]}.npz', allow_pickle=False) as z: occluder, per_frame, votes = (z[k] for k in ('solid_mask_hard', 'per_frame', 'votes')) for name, array, shape in (('mask', occluder, GRID_HW), ('per_frame', per_frame, (4, *GRID_HW))): if array.dtype != np.uint8 or array.shape != shape or not np.isin(array, (0, 1)).all(): raise ValueError(f'{name}: expected binary uint8 {shape}') if not np.array_equal(occluder, votes >= self.manifest['vote_min']): raise ValueError('mask disagrees with vote_min') edit = dilate_mask(occluder, self.dilate) return edit, edit class MaskImages: def __init__(self, directory, dilate=0): self.directory = Path(directory) self.dilate = dilate def load(self, clip): with Image.open(self.directory / f'{clip["clip_id"]}.png') as image: mask = np.asarray(image.convert('L')) if mask.shape != GRID_HW or not np.isin(mask, (0, 255)).all(): raise ValueError('mask image must be 518x294 with values 0 / 255') edit = dilate_mask(mask == 255, self.dilate) return edit, edit