|
| 1 | +# flake8: noqa: S101 |
| 2 | + |
1 | 3 | """ |
2 | 4 | PRAIG/SMB benchmark: measures OMR quality on the SMB dataset. |
3 | 5 |
|
|
12 | 14 |
|
13 | 15 | import argparse |
14 | 16 | import tempfile |
15 | | -from collections.abc import Iterator |
16 | 17 | from pathlib import Path |
17 | 18 |
|
18 | 19 | from validation.ned_benchmark import run_benchmark, update_ned_scores |
19 | 20 | from validation.tools import TOOLS |
20 | 21 |
|
21 | 22 |
|
22 | | -def iter_smb(split: str, image_dir: Path | None = None) -> Iterator[tuple[str, str, Path | None]]: |
| 23 | +def get_smb_samples(image_dir: Path) -> list[tuple[str, str, Path]]: |
23 | 24 | """Yield (sample_id, kern_text, image_path) for each page in PRAIG/SMB. |
24 | 25 |
|
25 | 26 | Passes the full page image to allow proper end-to-end OMR evaluation. |
26 | 27 | Ground truth priority: page.kern > top-level kern > concatenated region kern. |
27 | 28 | image_path is only set when image_dir is provided and the page has an image. |
28 | 29 | """ |
29 | | - from datasets import load_dataset # type: ignore # noqa: PLC0415 |
30 | | - |
31 | | - ds = load_dataset("PRAIG/SMB") |
32 | | - for i, sample in enumerate(ds[split]): |
33 | | - kern = "" |
34 | | - page = sample.get("page", {}) |
35 | | - if isinstance(page, dict): |
36 | | - kern = page.get("kern") or "" |
37 | | - if not kern: |
38 | | - kern = sample.get("kern") or "" |
39 | | - if not kern: |
40 | | - parts = [r.get("kern", "") for r in sample.get("regions", []) if r.get("kern")] |
41 | | - kern = "\n".join(parts) |
42 | | - if not kern: |
43 | | - continue |
44 | | - |
45 | | - image_path: Path | None = None |
46 | | - if image_dir is not None: |
47 | | - img = sample.get("image") |
48 | | - if img is not None and hasattr(img, "save"): |
49 | | - candidate = image_dir / f"{i}.png" |
50 | | - img.save(candidate) |
51 | | - image_path = candidate |
52 | | - |
53 | | - yield str(i), kern, image_path |
| 30 | + from datasets import Image, load_dataset # type: ignore # noqa: PLC0415 |
| 31 | + |
| 32 | + ds = load_dataset("PRAIG/SMB")["test"] |
| 33 | + ds = ds.cast_column("image", Image(decode=False)) # Don't decode on image colume. |
| 34 | + result = [] |
| 35 | + for i, sample in enumerate(ds): |
| 36 | + assert "kern" not in sample and isinstance( |
| 37 | + sample["regions"], list |
| 38 | + ) # not a global `kern` field, but a list of regions. |
| 39 | + assert sample["image"]["bytes"] |
| 40 | + |
| 41 | + parts = [r["kern"] for r in sample["regions"] if r["kern"]] |
| 42 | + kern = "\n".join(parts) |
| 43 | + image_path = image_dir / f"{i}.png" |
| 44 | + image_path.write_bytes(sample["image"]["bytes"]) |
| 45 | + |
| 46 | + result.append((str(i), kern, image_path)) |
| 47 | + return result |
54 | 48 |
|
55 | 49 |
|
56 | 50 | def main() -> None: |
57 | 51 | parser = argparse.ArgumentParser(description="OMR-NED benchmark for PRAIG/SMB.") |
58 | | - parser.add_argument("--split", type=str, default="test", help="Dataset split (default: test).") |
59 | 52 | parser.add_argument("--limit", type=int, default=None, help="Only process N samples.") |
60 | 53 | parser.add_argument("--verbose", action="store_true", help="Print traceback on failure.") |
61 | 54 | parser.add_argument("--output", type=str, default=None, help="Path to SQLite output file.") |
@@ -121,7 +114,7 @@ def main() -> None: |
121 | 114 | tool = TOOLS[args.tool] |
122 | 115 | with tempfile.TemporaryDirectory() as image_dir: |
123 | 116 | run_benchmark( |
124 | | - iter_smb(args.split, image_dir=Path(image_dir)), |
| 117 | + get_smb_samples(image_dir=Path(image_dir)), |
125 | 118 | tool, |
126 | 119 | limit=args.limit, |
127 | 120 | verbose=args.verbose, |
|
0 commit comments