Release Folio Orient with CPU inference, source, a sample scan and per-view evaluation results
9614d3e verified Download evaluation/sample_scans.py from foliokestrel/folio-orient: direct link, hf CLI and curl.
- Browser
- Download file 2.97 kB
-
https://huggingface.co/foliokestrel/folio-orient/resolve/main/evaluation/sample_scans.py
- Command line
-
hf download hf://foliokestrel/folio-orient/evaluation/sample_scans.py
-
curl -L -o sample_scans.py https://huggingface.co/foliokestrel/folio-orient/resolve/main/evaluation/sample_scans.py
2.97 kB
| """Fetch a seeded, stratified sample of scans for evaluation only. | |
| Source: finebooks/bhl-impact-gt (IMPACT / BHL, CC BY 3.0). Grayscale thumbnails | |
| and contact sheets are derived from the original page images. | |
| """ | |
| import argparse | |
| from concurrent.futures import ThreadPoolExecutor | |
| from io import BytesIO | |
| import json | |
| from pathlib import Path | |
| import random | |
| from time import sleep | |
| from urllib.request import urlopen | |
| from PIL import Image, ImageDraw, ImageOps | |
| REPO = "finebooks/bhl-impact-gt" | |
| REV = "b7bda5fac0471d6d2237360abc799c6d13559465" | |
| API = f"https://huggingface.co/api/datasets/{REPO}/tree/{REV}" | |
| RAW = f"https://huggingface.co/datasets/{REPO}/resolve/{REV}" | |
| def read(url): | |
| for retry in range(4): | |
| try: | |
| with urlopen(url, timeout=40) as r: | |
| return r.read() | |
| except OSError: | |
| if retry == 3: | |
| raise | |
| sleep(1 + retry * 2) | |
| def fetch(record, root): | |
| img = Image.open(BytesIO(read(record["url"]))).convert("L") | |
| img.thumbnail((2000, 2000), Image.Resampling.LANCZOS) | |
| img.save(root / record["file"]) | |
| def main(out): | |
| out.mkdir(parents=True, exist_ok=True) | |
| rng = random.Random(94135) | |
| records = [] | |
| books = sorted(obj["path"] for obj in json.loads(read(API + "/images"))) | |
| for book in books: | |
| pages = sorted( | |
| f["path"] | |
| for f in json.loads(read(API + "/" + book + "?limit=1000")) | |
| if f["path"].endswith(".webp") | |
| ) | |
| # One page per equal-sized part of the volume. Assign splits before inference. | |
| picks = [ | |
| rng.choice(pages[len(pages) * i // 20 : len(pages) * (i + 1) // 20]) | |
| for i in range(20) | |
| ] | |
| for i, page in enumerate(picks): | |
| records.append( | |
| dict( | |
| book=book.split("/")[-1], | |
| url=RAW + "/" + page, | |
| file=Path(page).stem + ".png", | |
| split="dev" if i % 5 == 0 else "test", | |
| ) | |
| ) | |
| with ThreadPoolExecutor(6) as pool: | |
| list(pool.map(lambda row: fetch(row, out), records)) | |
| (out / "manifest.json").write_text(json.dumps(records, indent=2) + "\n") | |
| for idx in range(len(books)): | |
| sheet = Image.new("RGB", (1000, 880), "white") | |
| draw = ImageDraw.Draw(sheet) | |
| for i, row in enumerate(records[idx * 20 : idx * 20 + 20]): | |
| image = Image.open(out / row["file"]) | |
| image.thumbnail((192, 188)) | |
| x, y = (i % 5) * 200, (i // 5) * 220 | |
| sheet.paste(image, (x + (200 - image.width) // 2, y)) | |
| draw.text( | |
| (x + 7, y + 190), | |
| f'{i+1:02} {row["file"].split("_")[-2]} {row["split"]}', | |
| fill="black", | |
| ) | |
| sheet.save(out / f"sheet-{idx}.jpg", quality=82) | |
| if __name__ == "__main__": | |
| p = argparse.ArgumentParser() | |
| p.add_argument("directory", type=Path) | |
| main(p.parse_args().directory) | |