-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathocr_test.py
More file actions
57 lines (48 loc) · 1.83 KB
/
Copy pathocr_test.py
File metadata and controls
57 lines (48 loc) · 1.83 KB
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
"""
Quick smoke-test for ccsolver.ocr.
Parses a handful of portrait board images and prints the extracted grid.
Run with:
nix-shell -p python3 python3Packages.opencv4Full \
python3Packages.networkx ... --run "python3 ocr_test.py"
"""
from pathlib import Path
from ccsolver.ocr import ocr_image
SAMPLES = [
"assets_pdf/002.png", # simple 4-tile single-chain (portrait)
"assets_pdf/004.png", # L-shape
"assets_pdf/006.png", # C-shape
"assets_pdf/008.png",
"assets_pdf/010.png",
"assets_pdf/020.png", # has digits 2
"assets_pdf/100.png", # special square 871×871
"assets_pdf/102.png", # multi-chain portrait
"assets_pdf/200.png", # special square, multi-chain
]
for path in SAMPLES:
p = Path(path)
if not p.exists():
print(f"{path}: NOT FOUND")
continue
level = ocr_image(path)
if level is None:
print(f"{path}: FAILED (could not parse)")
continue
print(f"\n{'─'*60}")
print(f"{path} → {level.n_cols}×{level.n_rows} grid "
f"({len(level.color_tiles)} X, {len(level.number_tiles)} numbers)")
print(level.to_game_string())
print("\n\nBatch test: parsing all portrait images …")
from ccsolver.ocr import ocr_directory
results = ocr_directory("assets_pdf", orientation="portrait")
failed_total = sum(
1 for p in sorted(Path("assets_pdf").glob("*.png"))
if p.stat().st_size > 0
and __import__("cv2").imread(str(p)) is not None
and __import__("cv2").imread(str(p)).shape[0] > __import__("cv2").imread(str(p)).shape[1]
) - len(results)
print(f"Parsed: {len(results)} | failed/skipped: {failed_total}")
# Show distribution of grid sizes
from collections import Counter
sizes = Counter(f"{r.n_cols}×{r.n_rows}" for _, r in results)
for size, count in sorted(sizes.items()):
print(f" {size}: {count} levels")