Hsyshasuzh/longmemory-geo / project /evaluation /render_dior_bridge_review_queue.py
Hsyshasuzh's picture
download
raw
3.38 kB
"""Render review-only DIOR-R bridge source-label contact sheets."""
from __future__ import annotations
import argparse
import json
from pathlib import Path
import xml.etree.ElementTree as ET
import zipfile
from PIL import Image, ImageDraw
ROOT = Path(__file__).resolve().parents[1]
def polygons(payload: bytes):
root = ET.fromstring(payload)
tags = ("x_left_top", "y_left_top", "x_right_top", "y_right_top", "x_right_bottom", "y_right_bottom", "x_left_bottom", "y_left_bottom")
for obj in root.findall("object"):
box = obj.find("robndbox")
if box is None:
continue
points = [(float(box.findtext(tags[i])), float(box.findtext(tags[i + 1]))) for i in range(0, len(tags), 2)]
yield obj.findtext("name", "").strip().lower(), obj.findtext("difficult", "0").strip() == "1", points
def render(queue: Path, output: Path, columns: int = 4) -> list[Path]:
records = json.loads(queue.read_text())["records"]
output.mkdir(parents=True, exist_ok=True)
rendered = []
cell, title = 384, 24
archive = ROOT / records[0]["source_annotation_archive"]
with zipfile.ZipFile(archive) as annotations:
for page_start in range(0, len(records), columns * 2):
page_records = records[page_start : page_start + columns * 2]
rows = (len(page_records) + columns - 1) // columns
page = Image.new("RGB", (columns * cell, rows * (cell + title)), "black")
for index, record in enumerate(page_records):
image = Image.open(ROOT / record["image"]).convert("RGB")
labels = list(polygons(annotations.read(record["source_annotation_member"])))
bridge_points = [point for name, difficult, points in labels if name == "bridge" and not difficult for point in points]
if not bridge_points:
raise ValueError(f"no usable bridge labels in {record['id']}")
xs, ys = [point[0] for point in bridge_points], [point[1] for point in bridge_points]
left, top = max(0, int(min(xs)) - 160), max(0, int(min(ys)) - 160)
right, bottom = min(image.width, int(max(xs)) + 160), min(image.height, int(max(ys)) + 160)
image = image.crop((left, top, right, bottom))
draw = ImageDraw.Draw(image)
for name, difficult, points in labels:
points = [(x - left, y - top) for x, y in points]
color = "lime" if name == "bridge" and not difficult else ("orange" if name == "bridge" else "red")
draw.line(points + [points[0]], fill=color, width=3)
image.thumbnail((cell, cell), Image.Resampling.LANCZOS)
x, y = (index % columns) * cell, (index // columns) * (cell + title)
page.paste(image, (x, y + title))
ImageDraw.Draw(page).text((x + 4, y + 4), record["id"], fill="white")
path = output / f"page-{page_start // (columns * 2) + 1:02d}.jpg"
page.save(path, quality=92)
rendered.append(path)
return rendered
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--queue", type=Path, required=True)
parser.add_argument("--output", type=Path, required=True)
args = parser.parse_args()
paths = render(args.queue, args.output)
print(json.dumps({"pages": [str(path) for path in paths]}))
if __name__ == "__main__":
main()

Xet Storage Details

Size:
3.38 kB
·
Xet hash:
c70dcec6a66e60ac16705cc2d1f581e199ab9ca0d8bf43d227defd42eda8939d

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.