-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess.py
More file actions
53 lines (37 loc) · 1.21 KB
/
Copy pathpreprocess.py
File metadata and controls
53 lines (37 loc) · 1.21 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
"""
Preprocess all level images into structured JSON files.
Run once:
python3 preprocess.py
Reads: assets_pdf/<id>.png
Writes: assets_ocr/<id>.json
Already-processed files are skipped, so the script is safe to re-run
(e.g. after adding new images or fixing the OCR pipeline).
"""
import json
import sys
from pathlib import Path
import ocr as _ocr
INPUT_DIR = Path("assets_pdf")
OUTPUT_DIR = Path("assets_ocr")
def main():
OUTPUT_DIR.mkdir(exist_ok=True)
pngs = sorted(INPUT_DIR.glob("*.png"), key=lambda p: int(p.stem))
total = len(pngs)
ok = skipped = failed = 0
for i, png in enumerate(pngs, 1):
out = OUTPUT_DIR / f"{png.stem}.json"
if out.exists():
skipped += 1
continue
try:
result = _ocr.parse_image(str(png))
with open(out, "w") as f:
json.dump(result, f, cls=_ocr.NumpyEncoder)
ok += 1
print(f"[{i}/{total}] {png.name} -> {out.name}")
except Exception as exc:
failed += 1
print(f"[{i}/{total}] FAILED {png.name}: {exc}", file=sys.stderr)
print(f"\nDone: {ok} processed, {skipped} skipped, {failed} failed")
if __name__ == "__main__":
main()