File size: 2,100 Bytes
1b140f4 0211cde 1b140f4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | """Read one observation directly from a downloaded dataset's tar archive."""
import argparse
import io
import json
import tarfile
from pathlib import Path
import numpy as np
import pyarrow.parquet as pq
from PIL import Image
def load_sample(root, subject, frame_id, camera_id):
root = Path(root)
records = pq.read_table(root / "metadata/subjects" / f"{subject}.parquet", filters=[
("subject_id", "=", subject), ("frame_id", "=", frame_id), ("camera_id", "=", camera_id)
]).to_pylist()
if len(records) != 1:
raise ValueError(f"Expected one observation, got {len(records)}")
record = records[0]
with tarfile.open(root / record["archive_path"], "r:") as archive:
with Image.open(io.BytesIO(archive.extractfile(record["image_member"]).read())) as image:
rgb = np.asarray(image).copy()
with Image.open(io.BytesIO(archive.extractfile(record["segmentation_member"]).read())) as image:
segmentation = np.asarray(image).copy()
with np.load(root / record["registration_path"], allow_pickle=False) as data:
row = record["registration_row"]
assert str(data["frame_ids"][row]) == frame_id
geometry = {key: data[key][row].copy() for key in (
"identity", "expression", "pose", "translation", "v_offsets", "vertices_world", "joints_world"
)}
cameras = json.loads((root / "metadata/cameras.json").read_text())
return {"image": rgb, "segmentation": segmentation, "foreground": segmentation > 0,
"camera": cameras[subject][camera_id], "metadata": record, **geometry}
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--root", type=Path, required=True)
parser.add_argument("--subject", required=True)
parser.add_argument("--frame", required=True)
parser.add_argument("--camera", required=True)
args = parser.parse_args()
sample = load_sample(args.root, args.subject, args.frame, args.camera)
print({k: (list(v.shape) if isinstance(v, np.ndarray) else v) for k, v in sample.items()})
|