"""Read viobench-flights shards (no webdataset dependency). for f in flights('shards/train-0000.tar'): f['frames'], f['imu'], ...""" import io, json, tarfile import numpy as np, cv2 def flights(tar): cur, d = None, {} def finish(i, d): off, b = np.load(io.BytesIO(d['jpg_offsets.npy'])), d['jpg.bin'] out = json.loads(d['json']); out['id'] = i for k in ('imu', 'gt', 'frame_times'): out[k] = np.load(io.BytesIO(d[k + '.npy'])) out['frames'] = np.stack([cv2.imdecode(np.frombuffer(b[off[k]:off[k + 1]], np.uint8), 0) for k in range(len(off) - 1)]) return out with tarfile.open(tar) as t: for m in t: i, ext = next((m.name[:-len(e) - 1], e) for e in ('jpg_offsets.npy', 'frame_times.npy', 'imu.npy', 'gt.npy', 'jpg.bin', 'json') if m.name.endswith('.' + e)) if cur not in (None, i): yield finish(cur, d); d = {} cur = i; d[ext] = t.extractfile(m).read() if d: yield finish(cur, d)