| from __future__ import annotations |
|
|
| import argparse |
| import json |
| import os |
| from pathlib import Path |
|
|
| from huggingface_hub import HfApi |
|
|
|
|
| CARD_TEMPLATE = """--- |
| language: |
| - da |
| - en |
| license: other |
| task_categories: |
| - image-classification |
| - image-to-text |
| - zero-shot-image-classification |
| pretty_name: Royal Danish Library Open COP Images |
| configs: |
| - config_name: images |
| data_files: |
| - split: train |
| path: viewer_embedded/*.parquet |
| - config_name: metadata |
| data_files: |
| - split: train |
| path: metadata/*.parquet |
| - config_name: flora_danica |
| data_files: |
| - split: train |
| path: viewer_embedded_flora_danica/*.parquet |
| - config_name: flora_danica_metadata |
| data_files: |
| - split: train |
| path: metadata_flora_danica/*.parquet |
| - config_name: portraetsamlingen |
| data_files: |
| - split: train |
| path: viewer_embedded_portraetsamlingen/*.parquet |
| - config_name: portraetsamlingen_metadata |
| data_files: |
| - split: train |
| path: metadata_portraetsamlingen/*.parquet |
| --- |
| |
| # Royal Danish Library Open COP Images |
| |
| This dataset is an independently harvested research dataset from the Royal Danish Library |
| (Det Kgl. Bibliotek) COP/Digital Collections API. |
| |
| The export intentionally excludes: |
| |
| - aerial photographs / `Danmark set fra luften` |
| - newspapers, including `Danske aviser 1666-1883` |
| - text-heavy COP editions such as books, letters, manuscripts, pamphlets, catalogues and printed matter |
| |
| Included COP editions: |
| |
| - `Billeder` |
| - `Kort og Atlas` as a separate `source_subset=maps` subset, so it can be filtered out if only |
| photographic/drawing material is wanted. |
| - `Flora Danica` subject collection as `source_subset=flora_danica`. |
| - `Portraetsamlingen` subject collection as `source_subset=portraetsamlingen`. |
| |
| Records were discovered using broad license-keyword searches and then filtered by the actual MODS |
| rights/accessCondition metadata. The accepted rights buckets are: |
| |
| - `Public Domain` |
| - `No known rights` |
| - `CC BY` |
| - `CC BY-SA` |
| |
| In the current COP MODS records, KBL commonly expresses public-domain/no-known-rights material as |
| `Materialet er fri af ophavsret`. |
| |
| ## Counts |
| |
| {counts} |
| |
| ## Files |
| |
| - `metadata/*.parquet`: normalized metadata-only table, including the raw MODS XML. |
| - `metadata_flora_danica/*.parquet`: Flora Danica metadata-only table. |
| - `metadata_portraetsamlingen/*.parquet`: Portraetsamlingen metadata-only table. |
| - `metadata/*.json`: selected/excluded source definitions, license-query list and summary. |
| - `viewer_embedded/*.parquet`: Hugging Face viewer-compatible table with embedded JPEG bytes and |
| normalized metadata. |
| - `viewer_embedded_flora_danica/*.parquet`: Flora Danica viewer-compatible table with embedded JPEG bytes. |
| - `viewer_embedded_portraetsamlingen/*.parquet`: Portraetsamlingen viewer-compatible table with embedded JPEG bytes. |
| - `webdataset-jpg1600-open/*.tar`: training shards with `<sample_id>.jpg` and `<sample_id>.json`. |
| - `webdataset-jpg1600-flora_danica/*.tar`: Flora Danica training shards. |
| - `webdataset-jpg1600-portraetsamlingen/*.tar`: Portraetsamlingen training shards. |
| - `manifests/*.json`: per-shard image download manifests. |
| - `scripts/*.py`: scraper/uploader scripts used for this export. |
| |
| ## Source Notes |
| |
| KBL's public API page says the API provides machine access to datasets containing aerial photos, |
| images and literary texts. KBL's API documentation states that API metadata is CC0, while content |
| has varying licenses. This dataset therefore treats every image record according to its own MODS |
| rights/accessCondition metadata. |
| |
| Source/API references: |
| |
| - KBL COP API page: https://api.kb.dk/data/cop |
| - KBL digital-object API documentation: https://github.com/kb-dk/access-digital-objects |
| - COP backend/search docs: https://github.com/kb-dk/access-digital-objects/blob/master/cop-backend.md |
| - IIIF image delivery docs: https://github.com/kb-dk/access-digital-objects/blob/master/image-delivery.md |
| |
| For Danish CLIP training, `preferred_title`, `preferred_description`, and `preferred_text` are |
| Danish-first fields: Danish metadata is used whenever present, with English as fallback. |
| """ |
|
|
|
|
| def parse_args() -> argparse.Namespace: |
| parser = argparse.ArgumentParser() |
| parser.add_argument("--repo-id", required=True) |
| parser.add_argument("--metadata-summary", type=Path, default=Path("data/kbl_open_metadata/summary.json")) |
| parser.add_argument("--flora-summary", type=Path, default=Path("data/kbl_flora_danica_metadata/summary.json")) |
| parser.add_argument( |
| "--portraits-summary", |
| type=Path, |
| default=Path("data/kbl_portraetsamlingen_metadata/summary.json"), |
| ) |
| parser.add_argument("--out", type=Path, default=Path("README.md")) |
| parser.add_argument("--upload", action="store_true") |
| return parser.parse_args() |
|
|
|
|
| def load_summary(path: Path) -> dict: |
| if not path.exists(): |
| return {} |
| return json.loads(path.read_text(encoding="utf-8")) |
|
|
|
|
| def section_counts(label: str, summary: dict) -> list[str]: |
| if not summary: |
| return [f"- `{label}`: not exported yet"] |
| lines = [ |
| f"- `{label}` image samples: {summary.get('rows', 0):,}", |
| f"- `{label}` source records: {summary.get('records', 0):,}", |
| ] |
| for title, counts in [ |
| ("licenses", summary.get("licenses", {})), |
| ("source_subsets", summary.get("source_subsets", {})), |
| ]: |
| if counts: |
| rendered = ", ".join(f"`{key}` {value:,}" for key, value in counts.items()) |
| lines.append(f"- `{label}` {title}: {rendered}") |
| for key in ("duplicate_rows_skipped", "rejected_records", "dropped_ambiguous_rights_rows"): |
| value = summary.get(key) |
| if value: |
| lines.append(f"- `{label}` {key}: {value:,}") |
| return lines |
|
|
|
|
| def counts_text(summary_path: Path, flora_path: Path, portraits_path: Path) -> str: |
| if not summary_path.exists(): |
| return "Counts will be filled after the export finishes." |
|
|
| base = load_summary(summary_path) |
| flora = load_summary(flora_path) |
| portraits = load_summary(portraits_path) |
| total_rows = sum(summary.get("rows", 0) for summary in (base, flora, portraits)) |
| total_records = sum(summary.get("records", 0) for summary in (base, flora, portraits)) |
| lines = [ |
| f"- Total image samples across configs: {total_rows:,}", |
| f"- Total accepted source records across configs: {total_records:,}", |
| ] |
| lines.extend(section_counts("images", base)) |
| lines.extend(section_counts("flora_danica", flora)) |
| lines.extend(section_counts("portraetsamlingen", portraits)) |
| return "\n".join(lines) |
|
|
|
|
| def main() -> None: |
| args = parse_args() |
| card = CARD_TEMPLATE.format( |
| counts=counts_text(args.metadata_summary, args.flora_summary, args.portraits_summary) |
| ) |
| args.out.write_text(card, encoding="utf-8") |
| if args.upload: |
| token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_HUB_TOKEN") |
| if not token: |
| raise SystemExit("Set HF_TOKEN before uploading.") |
| HfApi(token=token).upload_file( |
| repo_id=args.repo_id, |
| repo_type="dataset", |
| path_or_fileobj=str(args.out), |
| path_in_repo="README.md", |
| commit_message="Update KBL dataset card", |
| ) |
| print("uploaded README.md") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|