File size: 2,167 Bytes
e923c09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""Benchmark BrailleVision on annotated images."""

from __future__ import annotations

import argparse
import json
from pathlib import Path

from braillevision.pipeline import run_pipeline
from braillevision.utils import load_image

ROOT = Path(__file__).resolve().parents[1]


def find_annotation(image_path: Path) -> Path | None:
    candidates = [
        image_path.with_suffix(".json"),
        ROOT / "data" / "annotations" / f"{image_path.stem}.json",
        ROOT / "data" / "annotations" / f"{image_path.name}.json",
    ]
    for candidate in candidates:
        if candidate.exists():
            return candidate
    return None


def levenshtein(a: str, b: str) -> int:
    prev = list(range(len(b) + 1))
    for i, ca in enumerate(a, 1):
        cur = [i]
        for j, cb in enumerate(b, 1):
            cur.append(min(prev[j] + 1, cur[-1] + 1, prev[j - 1] + (ca != cb)))
        prev = cur
    return prev[-1]


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--testset", type=Path, default=ROOT / "data" / "processed")
    args = parser.parse_args()

    rows: list[dict[str, object]] = []
    for image_path in sorted(args.testset.glob("*.*")):
        annotation_path = find_annotation(image_path)
        if annotation_path is None:
            continue
        expected = json.loads(annotation_path.read_text()).get("text", "")
        result = run_pipeline(load_image(image_path))
        distance = levenshtein(result.text, expected)
        cer = distance / max(len(expected), 1)
        rows.append(
            {
                "image": image_path.name,
                "expected": expected,
                "predicted": result.text,
                "cer": cer,
            }
        )

    if not rows:
        print("No annotated images found.")
        return

    mean_cer = sum(float(row["cer"]) for row in rows) / len(rows)
    for row in rows:
        print(
            f"{row['image']}: CER={row['cer']:.3f} "
            f"expected={row['expected']!r} predicted={row['predicted']!r}"
        )
    print(f"mean CER: {mean_cer:.3f}")


if __name__ == "__main__":
    main()