File size: 932 Bytes
24aaa76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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")