Spaces:
Running
Running
Download make_space_data.py from iisys-hof/situate-diffuse: direct link, hf CLI and curl.
- Browser
- Download file 2.14 kB
-
https://huggingface.co/spaces/iisys-hof/situate-diffuse/resolve/main/make_space_data.py
- Command line
-
hf download hf://spaces/iisys-hof/situate-diffuse/make_space_data.py
-
curl -L -o make_space_data.py https://huggingface.co/spaces/iisys-hof/situate-diffuse/resolve/main/make_space_data.py
2.14 kB
| """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}") | |