Spaces:
Running
Running
File size: 2,141 Bytes
7dca415 | 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | """Writes what the page shows from the situate_diffuse parquet files: scenes.json (every column but the pictures and objects),
objects/<scene>.json (what Sam kept per model, loaded when a picture is opened), pictures/<model>/<scene>.webp and
masks/<model>/<scene>.png, so the Space needs no api.
python make_space_data.py ../situate-diffuse-hf/situate_diffuse
"""
import io
import json
import sys
from pathlib import Path
import pyarrow.parquet as pq
from PIL import Image
HERE = Path(__file__).resolve().parent
SOURCE = Path(sys.argv[1])
QUALITY = 82 # webp, about a quarter of the jpg at no visible loss
def round_boxes(objects): # whole pixels and 3 digits are all the page shows
for thing in objects:
thing["box"] = [round(v) for v in thing["box"]]
thing["score"] = round(thing["score"], 3)
return objects
scenes = []
for file in sorted(SOURCE.glob("*.parquet")):
for row in pq.read_table(file).to_pylist():
scene = {}
objects = {}
for column, value in row.items():
if column.endswith("_objects"):
objects[column.removesuffix("_objects")] = round_boxes(json.loads(value))
continue
if not isinstance(value, dict):
scene[column] = value
continue
model = column.removesuffix("_masks")
if column.endswith("_masks"):
out = HERE / "masks" / model / f"{row['scene_id']}.png"
out.parent.mkdir(parents=True, exist_ok=True)
out.write_bytes(value["bytes"])
continue
out = HERE / "pictures" / model / f"{row['scene_id']}.webp"
out.parent.mkdir(parents=True, exist_ok=True)
Image.open(io.BytesIO(value["bytes"])).save(out, "WEBP", quality=QUALITY, method=6)
(HERE / "objects").mkdir(exist_ok=True)
(HERE / "objects" / f"{row['scene_id']}.json").write_text(json.dumps(objects, separators=(",", ":")))
scenes.append(scene)
(HERE / "scenes.json").write_text(json.dumps(scenes, separators=(",", ":")))
print(f"{len(scenes)} scenes -> {HERE}")
|