Add hero gallery figure
Browse filesCo-Authored-By: Claude Fable 5 <noreply@anthropic.com>
- assets/hero_gallery.png +3 -0
- scripts/make_gallery.py +88 -0
assets/hero_gallery.png
ADDED
|
Git LFS Details
|
scripts/make_gallery.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Render the hero gallery: 4 sample images x (raw | cluster masks | berry keypoints)."""
|
| 2 |
+
import json
|
| 3 |
+
import sys
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
import matplotlib
|
| 7 |
+
|
| 8 |
+
matplotlib.use("Agg")
|
| 9 |
+
import matplotlib.pyplot as plt
|
| 10 |
+
import numpy as np
|
| 11 |
+
import pandas as pd
|
| 12 |
+
from matplotlib.patches import Polygon
|
| 13 |
+
from PIL import Image
|
| 14 |
+
|
| 15 |
+
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
| 16 |
+
from vivid_utils import STYLE
|
| 17 |
+
|
| 18 |
+
ROOT = Path(__file__).resolve().parents[1]
|
| 19 |
+
DATA = ROOT / "data"
|
| 20 |
+
GRAPE_CATEGORY_ID = 1
|
| 21 |
+
MASK_COLORS = ["#6366F1", "#F59E0B", "#10B981", "#EF4444", "#8B5CF6", "#06B6D4"]
|
| 22 |
+
|
| 23 |
+
# Override with specific file_names after eyeballing candidates; None = auto-pick
|
| 24 |
+
GALLERY_FILES = None
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def pick_samples(meta):
|
| 28 |
+
if GALLERY_FILES:
|
| 29 |
+
return list(GALLERY_FILES)
|
| 30 |
+
picks = []
|
| 31 |
+
pool = meta[meta.cluster_count.between(2, 8)].sort_values("berry_count")
|
| 32 |
+
for q in (0.15, 0.45, 0.7, 0.95):
|
| 33 |
+
row = pool.iloc[min(int(q * len(pool)), len(pool) - 1)]
|
| 34 |
+
if row.file_name not in picks:
|
| 35 |
+
picks.append(row.file_name)
|
| 36 |
+
return picks
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def main():
|
| 40 |
+
meta = pd.read_csv(DATA / "metadata.csv")
|
| 41 |
+
samples = pick_samples(meta)
|
| 42 |
+
print("gallery samples:", samples)
|
| 43 |
+
|
| 44 |
+
print("Loading COCO json...")
|
| 45 |
+
coco = json.loads((DATA / "anns" / "instances_updated.json").read_text())
|
| 46 |
+
ids = {im["file_name"]: im["id"] for im in coco["images"] if im["file_name"] in samples}
|
| 47 |
+
polys = {name: [] for name in samples}
|
| 48 |
+
id_to_name = {v: k for k, v in ids.items()}
|
| 49 |
+
for ann in coco["annotations"]:
|
| 50 |
+
if ann["category_id"] != GRAPE_CATEGORY_ID or ann["image_id"] not in id_to_name:
|
| 51 |
+
continue
|
| 52 |
+
for seg in ann["segmentation"]:
|
| 53 |
+
pts = np.asarray(seg, dtype=float).reshape(-1, 2)
|
| 54 |
+
polys[id_to_name[ann["image_id"]]].append(pts)
|
| 55 |
+
|
| 56 |
+
fig, axes = plt.subplots(len(samples), 3, figsize=(9, 3.9 * len(samples)),
|
| 57 |
+
dpi=STYLE["dpi"], facecolor="white")
|
| 58 |
+
col_titles = ["image", "cluster masks", "berry keypoints"]
|
| 59 |
+
for r, name in enumerate(samples):
|
| 60 |
+
img = Image.open(DATA / "imgs" / name).convert("RGB")
|
| 61 |
+
img.thumbnail((900, 900))
|
| 62 |
+
scale = img.width / meta.set_index("file_name").loc[name, "width"]
|
| 63 |
+
pts = np.load(DATA / "anns" / "points" / (Path(name).stem + ".npy")) * scale
|
| 64 |
+
for c in range(3):
|
| 65 |
+
ax = axes[r, c]
|
| 66 |
+
ax.imshow(img)
|
| 67 |
+
ax.set_xticks([]), ax.set_yticks([])
|
| 68 |
+
for spine in ax.spines.values():
|
| 69 |
+
spine.set_visible(False)
|
| 70 |
+
if r == 0:
|
| 71 |
+
ax.set_title(col_titles[c], fontsize=12, color=STYLE["text"])
|
| 72 |
+
for i, poly in enumerate(polys[name]):
|
| 73 |
+
axes[r, 1].add_patch(Polygon(poly * scale, closed=True, alpha=0.45,
|
| 74 |
+
facecolor=MASK_COLORS[i % len(MASK_COLORS)],
|
| 75 |
+
edgecolor="white", linewidth=1.0))
|
| 76 |
+
axes[r, 2].scatter(pts[:, 0], pts[:, 1], s=4, color="#FDE047",
|
| 77 |
+
edgecolors="#B45309", linewidths=0.3)
|
| 78 |
+
axes[r, 2].text(0.02, 0.02, f"{len(pts)} berries", transform=axes[r, 2].transAxes,
|
| 79 |
+
fontsize=11, fontweight="bold", color="white",
|
| 80 |
+
bbox=dict(facecolor="black", alpha=0.6, pad=3))
|
| 81 |
+
fig.tight_layout()
|
| 82 |
+
out = ROOT / "assets" / "hero_gallery.png"
|
| 83 |
+
fig.savefig(out, dpi=STYLE["dpi"], facecolor="white", bbox_inches="tight")
|
| 84 |
+
print("wrote", out)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
main()
|