Datasets:
Download examples/render_annotation.py from govtech/municipalbench-full: direct link, hf CLI and curl.
- Browser
- Download file 1.33 kB
-
https://huggingface.co/datasets/govtech/municipalbench-full/resolve/main/examples/render_annotation.py
- Command line
-
hf download hf://datasets/govtech/municipalbench-full/examples/render_annotation.py
-
curl -L -o render_annotation.py https://huggingface.co/datasets/govtech/municipalbench-full/resolve/main/examples/render_annotation.py
1.33 kB
| """Render a positive example from the native ground truth without modifying data.""" | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| from PIL import Image, ImageDraw | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--root", type=Path, default=Path(__file__).resolve().parents[1]) | |
| parser.add_argument("--image-id") | |
| parser.add_argument("--output", type=Path, default=Path("/tmp/municipalbench.png")) | |
| args = parser.parse_args() | |
| rows = json.loads((args.root / "annotations/ground_truth.json").read_text()) | |
| record = next(r for r in rows if r["image_id"] == args.image_id) if args.image_id else next( | |
| r for r in rows if r["gold"]["violations"]) | |
| manifest = {r["image_id"]: r for r in map(json.loads, | |
| (args.root / "release/image_manifest.jsonl").read_text().splitlines())} | |
| image = Image.open(args.root / manifest[record["image_id"]]["file_name"]).convert("RGB") | |
| draw = ImageDraw.Draw(image) | |
| for v in record["gold"]["violations"]: | |
| x, y, w, h = v["bbox"]; iw, ih = image.size | |
| draw.rectangle((x*iw, y*ih, (x+w)*iw, (y+h)*ih), outline="red", width=3) | |
| draw.text((x*iw+3, y*ih+3), v["use_case_id"], fill="red", stroke_width=1, stroke_fill="white") | |
| image.save(args.output) | |
| print(args.output) | |