Download scripts/make_gallery.py from XZhi/ViViD-5k: direct link, hf CLI and curl.
- Browser
- Download file 3.48 kB
-
https://huggingface.co/datasets/XZhi/ViViD-5k/resolve/main/scripts/make_gallery.py
- Command line
-
hf download hf://datasets/XZhi/ViViD-5k/scripts/make_gallery.py
-
curl -L -o make_gallery.py https://huggingface.co/datasets/XZhi/ViViD-5k/resolve/main/scripts/make_gallery.py
3.48 kB
| """Render the hero gallery: 4 sample images x (raw | cluster masks | berry keypoints).""" | |
| import json | |
| import sys | |
| from pathlib import Path | |
| import matplotlib | |
| matplotlib.use("Agg") | |
| import matplotlib.pyplot as plt | |
| import numpy as np | |
| import pandas as pd | |
| from matplotlib.patches import Polygon | |
| from PIL import Image | |
| sys.path.insert(0, str(Path(__file__).resolve().parent)) | |
| from vivid_utils import STYLE | |
| ROOT = Path(__file__).resolve().parents[1] | |
| DATA = ROOT / "data" | |
| GRAPE_CATEGORY_ID = 1 | |
| MASK_COLORS = ["#6366F1", "#F59E0B", "#10B981", "#EF4444", "#8B5CF6", "#06B6D4"] | |
| # Override with specific file_names after eyeballing candidates; None = auto-pick | |
| GALLERY_FILES = None | |
| def pick_samples(meta): | |
| if GALLERY_FILES: | |
| return list(GALLERY_FILES) | |
| picks = [] | |
| pool = meta[meta.cluster_count.between(2, 8)].sort_values("berry_count") | |
| for q in (0.15, 0.45, 0.7, 0.95): | |
| row = pool.iloc[min(int(q * len(pool)), len(pool) - 1)] | |
| if row.file_name not in picks: | |
| picks.append(row.file_name) | |
| return picks | |
| def main(): | |
| meta = pd.read_csv(DATA / "metadata.csv") | |
| samples = pick_samples(meta) | |
| print("gallery samples:", samples) | |
| print("Loading COCO json...") | |
| coco = json.loads((DATA / "anns" / "instances_updated.json").read_text()) | |
| ids = {im["file_name"]: im["id"] for im in coco["images"] if im["file_name"] in samples} | |
| polys = {name: [] for name in samples} | |
| id_to_name = {v: k for k, v in ids.items()} | |
| for ann in coco["annotations"]: | |
| if ann["category_id"] != GRAPE_CATEGORY_ID or ann["image_id"] not in id_to_name: | |
| continue | |
| for seg in ann["segmentation"]: | |
| pts = np.asarray(seg, dtype=float).reshape(-1, 2) | |
| polys[id_to_name[ann["image_id"]]].append(pts) | |
| fig, axes = plt.subplots(len(samples), 3, figsize=(9, 3.9 * len(samples)), | |
| dpi=STYLE["dpi"], facecolor="white") | |
| col_titles = ["image", "cluster masks", "berry keypoints"] | |
| for r, name in enumerate(samples): | |
| img = Image.open(DATA / "imgs" / name).convert("RGB") | |
| img.thumbnail((900, 900)) | |
| scale = img.width / meta.set_index("file_name").loc[name, "width"] | |
| pts = np.load(DATA / "anns" / "points" / (Path(name).stem + ".npy")) * scale | |
| for c in range(3): | |
| ax = axes[r, c] | |
| ax.imshow(img) | |
| ax.set_xticks([]), ax.set_yticks([]) | |
| for spine in ax.spines.values(): | |
| spine.set_visible(False) | |
| if r == 0: | |
| ax.set_title(col_titles[c], fontsize=12, color=STYLE["text"]) | |
| for i, poly in enumerate(polys[name]): | |
| axes[r, 1].add_patch(Polygon(poly * scale, closed=True, alpha=0.45, | |
| facecolor=MASK_COLORS[i % len(MASK_COLORS)], | |
| edgecolor="white", linewidth=1.0)) | |
| axes[r, 2].scatter(pts[:, 0], pts[:, 1], s=4, color="#FDE047", | |
| edgecolors="#B45309", linewidths=0.3) | |
| axes[r, 2].text(0.02, 0.02, f"{len(pts)} berries", transform=axes[r, 2].transAxes, | |
| fontsize=11, fontweight="bold", color="white", | |
| bbox=dict(facecolor="black", alpha=0.6, pad=3)) | |
| fig.tight_layout() | |
| out = ROOT / "assets" / "hero_gallery.png" | |
| fig.savefig(out, dpi=STYLE["dpi"], facecolor="white", bbox_inches="tight") | |
| print("wrote", out) | |
| if __name__ == "__main__": | |
| main() | |