File size: 1,088 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 27 | """Smoke-test sample list from KOHTD train only (kohtd_split_v1/train.tsv): N crops, seeded shuffle.
usage: smoke_seq.py TRAIN_TSV IMG_DIR OUT_TSV N SEED
"""
import os
import random
import sys
import unicodedata
train_tsv, img_dir, out_tsv, n, seed = sys.argv[1], sys.argv[2], sys.argv[3], int(sys.argv[4]), int(sys.argv[5])
assert os.path.basename(train_tsv) == "train.tsv"
rows = []
with open(train_tsv, encoding="utf-8") as f:
hdr = f.readline().rstrip("\n").split("\t")
assert hdr[:3] == ["crop_file", "form_id", "label"], hdr
for line in f:
p = line.rstrip("\n").split("\t")
if p[2].strip():
rows.append(p)
random.Random(seed).shuffle(rows)
with open(out_tsv, "w", encoding="utf-8") as fo:
fo.write("id\timage\ttext\tlang\tscript\tkind\tsource\tsplit\tgroup\tunit\n")
for p in rows[:n]:
fo.write("\t".join([p[0], os.path.join(img_dir, p[0]), unicodedata.normalize("NFC", p[2]), "kk", "Cyrl",
"hand_real", "kohtd", "train", p[1], "word"]) + "\n")
print("wrote", min(n, len(rows)), "of", len(rows))
|