Upload ONNX export
Browse files- fetch_example_images.py +105 -0
fetch_example_images.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Fetch open-source document images with dense, varied layouts for the model card gallery.
|
| 2 |
+
|
| 3 |
+
Pulls sample pages from the CDLA-Permissive-1.0-licensed `creative-graphic-design/PubLayNet`
|
| 4 |
+
dataset on the Hugging Face Hub (a re-hosting of PubLayNet: Zhong et al., 2019,
|
| 5 |
+
https://arxiv.org/abs/1908.07836 — scientific articles from PubMed Central Open Access).
|
| 6 |
+
Scores a scan window of rows by their COCO-annotation count (a proxy for layout density —
|
| 7 |
+
how many text/title/list/table/figure regions are on the page) and downloads the densest ones,
|
| 8 |
+
so the gallery shows the model handling as many layout elements as possible per image.
|
| 9 |
+
|
| 10 |
+
Uses only the HF `datasets-server` `/rows` REST API (plain paginated JSON + signed image URLs)
|
| 11 |
+
— no `datasets`/torch/pyarrow dependency needed.
|
| 12 |
+
|
| 13 |
+
Usage:
|
| 14 |
+
python fetch_example_images.py --count 5 --scan 3000 --out-dir examples/inputs
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
from __future__ import annotations
|
| 18 |
+
|
| 19 |
+
import argparse
|
| 20 |
+
import json
|
| 21 |
+
import urllib.request
|
| 22 |
+
from pathlib import Path
|
| 23 |
+
|
| 24 |
+
DATASET = "creative-graphic-design/PubLayNet"
|
| 25 |
+
ROWS_URL = "https://datasets-server.huggingface.co/rows"
|
| 26 |
+
PAGE = 100
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def scan(scan_rows: int, split: str) -> list[dict]:
|
| 30 |
+
candidates = []
|
| 31 |
+
for offset in range(0, scan_rows, PAGE):
|
| 32 |
+
url = (
|
| 33 |
+
f"{ROWS_URL}?dataset={DATASET.replace('/', '%2F')}&config=default"
|
| 34 |
+
f"&split={split}&offset={offset}&length={PAGE}"
|
| 35 |
+
)
|
| 36 |
+
try:
|
| 37 |
+
with urllib.request.urlopen(url, timeout=30) as r:
|
| 38 |
+
payload = json.load(r)
|
| 39 |
+
except Exception as exc:
|
| 40 |
+
print(f" offset {offset}: skipped ({exc})")
|
| 41 |
+
continue
|
| 42 |
+
for item in payload["rows"]:
|
| 43 |
+
row = item["row"]
|
| 44 |
+
candidates.append(
|
| 45 |
+
{
|
| 46 |
+
"file_name": row["file_name"],
|
| 47 |
+
"width": row["width"],
|
| 48 |
+
"height": row["height"],
|
| 49 |
+
"n_boxes": len(row["annotations"]["bbox"]),
|
| 50 |
+
"n_classes": len(set(row["annotations"]["category_id"])),
|
| 51 |
+
"src": row["image"]["src"],
|
| 52 |
+
}
|
| 53 |
+
)
|
| 54 |
+
print(f" offset {offset}: scanned ({len(candidates)} candidates so far)")
|
| 55 |
+
return candidates
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def main() -> int:
|
| 59 |
+
p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
| 60 |
+
p.add_argument("--count", type=int, default=5, help="how many images to download")
|
| 61 |
+
p.add_argument("--scan", type=int, default=2000, help="how many dataset rows to scan for density")
|
| 62 |
+
p.add_argument("--split", default="train")
|
| 63 |
+
p.add_argument("--out-dir", type=Path, default=Path("examples/inputs"))
|
| 64 |
+
args = p.parse_args()
|
| 65 |
+
|
| 66 |
+
args.out_dir.mkdir(parents=True, exist_ok=True)
|
| 67 |
+
print(f"Scanning {args.scan} rows of {DATASET} ({args.split}) for the densest layouts...")
|
| 68 |
+
candidates = scan(args.scan, args.split)
|
| 69 |
+
if not candidates:
|
| 70 |
+
raise SystemExit("no candidates found — dataset-server may be unreachable")
|
| 71 |
+
|
| 72 |
+
candidates.sort(key=lambda c: (-c["n_boxes"], -c["n_classes"]))
|
| 73 |
+
chosen = candidates[: args.count]
|
| 74 |
+
|
| 75 |
+
manifest = []
|
| 76 |
+
print("\nDownloading:")
|
| 77 |
+
for c in chosen:
|
| 78 |
+
dest = args.out_dir / c["file_name"]
|
| 79 |
+
urllib.request.urlretrieve(c["src"], dest)
|
| 80 |
+
print(f" {c['n_boxes']:3d} boxes, {c['n_classes']} classes -> {dest.name} ({c['width']}x{c['height']})")
|
| 81 |
+
manifest.append({k: v for k, v in c.items() if k != "src"})
|
| 82 |
+
|
| 83 |
+
(args.out_dir / "SOURCE.json").write_text(
|
| 84 |
+
json.dumps(
|
| 85 |
+
{
|
| 86 |
+
"dataset": DATASET,
|
| 87 |
+
"dataset_url": f"https://huggingface.co/datasets/{DATASET}",
|
| 88 |
+
"license": "CDLA-Permissive-1.0",
|
| 89 |
+
"citation": (
|
| 90 |
+
"Zhong, X., Tang, J., & Yepes, A. J. (2019). "
|
| 91 |
+
"PubLayNet: largest dataset ever for document layout analysis. "
|
| 92 |
+
"arXiv:1908.07836"
|
| 93 |
+
),
|
| 94 |
+
"note": "Pages sourced from PubMed Central open-access scientific articles.",
|
| 95 |
+
"images": manifest,
|
| 96 |
+
},
|
| 97 |
+
indent=2,
|
| 98 |
+
)
|
| 99 |
+
)
|
| 100 |
+
print(f"\n{len(chosen)} image(s) -> {args.out_dir}")
|
| 101 |
+
return 0
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
if __name__ == "__main__":
|
| 105 |
+
raise SystemExit(main())
|