"""Fetch the images of manga-layout's `danbooru` subset. The images are Danbooru2024 posts as packed in deepghs/danbooru2024-webp-4Mpixel on Hugging Face (WebP, at most 4 megapixels), which is where the annotations' coordinates come from. Accept that dataset's terms on its page, log in with `hf auth login`, then run from the dataset's root: pip install cheesechaser python fetch_danbooru.py [split ...] Each split defaults to train, validation and test. Images land in danbooru/images/.webp; each file is checked against the md5 its annotation records, and the script reports any post it could not fetch or that differs (a post deleted or replaced since the snapshot). """ import hashlib import json import sys from pathlib import Path from cheesechaser.datapool import Danbooru2024WebpDataPool ROOT = Path(__file__).resolve().parent / "danbooru" IMAGES = ROOT / "images" def md5(path: Path) -> str: return hashlib.md5(path.read_bytes()).hexdigest() def main(splits: list[str]) -> None: IMAGES.mkdir(parents=True, exist_ok=True) expected = {} for split in splits: coco = json.loads((ROOT / f"{split}.coco.json").read_text(encoding="utf-8")) expected.update({image["danbooru_id"]: image["md5"] for image in coco["images"]}) wanted = [post for post, digest in expected.items() if not (IMAGES / f"{post}.webp").exists() or md5(IMAGES / f"{post}.webp") != digest] if wanted: print(f"fetching {len(wanted)} of {len(expected)} posts") Danbooru2024WebpDataPool().batch_download_to_directory(resource_ids=wanted, dst_dir=str(IMAGES), max_workers=16, save_metainfo=False, silent=True) missing = [post for post in expected if not (IMAGES / f"{post}.webp").exists()] changed = [post for post in expected if post not in missing and md5(IMAGES / f"{post}.webp") != expected[post]] print(f"{len(expected) - len(missing) - len(changed)} of {len(expected)} images match; {len(missing)} missing, {len(changed)} differ") if missing or changed: (ROOT / "unavailable.json").write_text(json.dumps({"missing": missing, "changed": changed}), encoding="utf-8") print("their post ids are in danbooru/unavailable.json; score.py leaves those pages out") if __name__ == "__main__": main(sys.argv[1:] or ["train", "validation", "test"])