Download load_shards.py from SentientNapkin/viobench-flights: direct link, hf CLI and curl.
- Browser
- Download file 981 Bytes
-
https://huggingface.co/datasets/SentientNapkin/viobench-flights/resolve/main/load_shards.py
- Command line
-
hf download hf://datasets/SentientNapkin/viobench-flights/load_shards.py
-
curl -L -o load_shards.py https://huggingface.co/datasets/SentientNapkin/viobench-flights/resolve/main/load_shards.py
981 Bytes
| """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) | |