turkic-ocr-code / scripts /k1_labels_all.py
treoa's picture
Release 1
24aaa76
Raw History Blame Contribute Delete
932 Bytes
# K1 (runs on the cluster, ZEN4): read every KOHTD ann file (JSON with the label) and write one TSV: file stem, label.
# Text only; no image is read. usage: python k1_labels_all.py <HK_dataset/ann dir> <out_tsv>
import json
import os
import sys
from concurrent.futures import ThreadPoolExecutor
ann, out = sys.argv[1], sys.argv[2]
names = sorted(e.name for e in os.scandir(ann) if e.name.endswith(".json"))
print("ann files:", len(names), flush=True)
def read(n):
with open(os.path.join(ann, n), encoding="utf-8") as f:
d = json.load(f)
return n[:-5], str(d.get("description", "")).replace("\t", " ").replace("\n", " ")
with ThreadPoolExecutor(32) as ex, open(out, "w", encoding="utf-8") as f:
f.write("stem\tlabel\n")
for i, (stem, lab) in enumerate(ex.map(read, names, chunksize=64)):
f.write(stem + "\t" + lab + "\n")
if i % 20000 == 0:
print(i, flush=True)
print("done")