Download scripts/j2/controls2_report.py from treoa/turkic-ocr-code: direct link, hf CLI and curl.
- Browser
- Download file 4.07 kB
-
https://huggingface.co/treoa/turkic-ocr-code/resolve/main/scripts/j2/controls2_report.py
- Command line
-
hf download hf://treoa/turkic-ocr-code/scripts/j2/controls2_report.py
-
curl -L -o controls2_report.py https://huggingface.co/treoa/turkic-ocr-code/resolve/main/scripts/j2/controls2_report.py
4.07 kB
| """Score the two small controls (contrast, handwriting fonts) with the screen's scorelib and write controls_2.tsv. | |
| A printed-text row (PT Sans, 40 px, from the first positive control) is scored with the same code for comparison. | |
| usage: controls2_report.py C2_DIR PRINTED_LIST PRINTED_OUT_ROOT SCORELIB_PY OUT_TSV | |
| C2_DIR holds contrast_stored_/contrast_stretch_/fonts_screen_set_v1.tsv and out_stored/ out_stretch/ out_fonts/. | |
| """ | |
| import importlib.util | |
| import json | |
| import os | |
| import sys | |
| c2, printed_list, printed_root, scorelib_py, out_tsv = sys.argv[1:6] | |
| spec = importlib.util.spec_from_file_location("scorelib", scorelib_py) | |
| sl = importlib.util.module_from_spec(spec) | |
| spec.loader.exec_module(sl) | |
| def read_list(path): | |
| rows = [] | |
| with open(path, encoding="utf-8", newline="") as f: | |
| f.readline() | |
| for line in f: | |
| c, i, lab = line.rstrip("\n").split("\t") | |
| rows.append((c, lab)) | |
| return rows | |
| def read_preds(path): | |
| preds = {} | |
| if os.path.exists(path): | |
| for line in open(path, encoding="utf-8"): | |
| try: | |
| r = json.loads(line) | |
| except Exception: # noqa: BLE001 | |
| continue | |
| preds[r["crop_file"]] = r | |
| return preds | |
| def is_lang(c, k): | |
| return c.startswith(k + "_") or f"_{k}_" in c | |
| def block(name, model, rows, preds, pick): | |
| sc, lang = [], {"kk": [], "ky": []} | |
| for c, lab in rows: | |
| if c in preds: | |
| s = sl.score_crop(lab, preds[c]["raw"]) | |
| sc.append(s) | |
| for k in lang: | |
| if is_lang(c, k): | |
| lang[k].append(s) | |
| if not sc: | |
| return None | |
| a = sl.aggregate(sc) | |
| ak = sl.aggregate(lang["kk"]) if lang["kk"] else {} | |
| ay = sl.aggregate(lang["ky"]) if lang["ky"] else {} | |
| lab = dict(rows) | |
| five = " | ".join(f"{lab[c]} => {preds[c]['raw']!r}" for c in pick if c in preds) | |
| fmt = lambda d, k: f"{d[k]:.4f}" if k in d else "" # noqa: E731 | |
| return "\t".join([name, model, str(len(sc)), f"{a['cer_pooled']:.4f}", f"{a['cer_mean_cap1']:.4f}", f"{a['exact']:.4f}", | |
| fmt(ak, "cer_pooled"), fmt(ay, "cer_pooled"), f"{a['hr4_loop_share']:.4f}", f"{a['empty_share']:.4f}", | |
| f"{a['other_script_share']:.4f}", five]) | |
| def pick5(rows): | |
| names = [c for c, _ in rows] | |
| kk = [c for c in names if is_lang(c, "kk")][:2] | |
| ky = [c for c in names if is_lang(c, "ky")][:3] | |
| return kk + ky | |
| out = ["control\tmodel\tcrops_scored\tcer_pooled\tcer_capped_mean\texact_match\tcer_pooled_kk\tcer_pooled_ky\thr4_loop_share\tempty_share\tother_script_share\tfive_raw_outputs (reference => raw output)"] | |
| models = ["paddleocr_vl_1_6", "qwen3_vl_2b"] | |
| stored, stretch, fonts = (read_list(os.path.join(c2, n)) for n in | |
| ("contrast_stored_screen_set_v1.tsv", "contrast_stretch_screen_set_v1.tsv", "fonts_screen_set_v1.tsv")) | |
| printed40 = [(c, l) for c, l in read_list(printed_list) if "_h40_" in c] | |
| for m in models: | |
| first5 = lambda rows: [c for c, _ in rows[:5]] # noqa: E731 | |
| for name, rows, root in (("A1 KOHTD first 300 crops, as stored", stored, "out_stored"), | |
| ("A2 KOHTD first 300 crops, contrast stretch p1-p99", stretch, "out_stretch")): | |
| r = block(name, m, rows, read_preds(os.path.join(c2, root, m, "s1", "preds.jsonl")), first5(rows)) | |
| if r: | |
| out.append(r) | |
| fp = read_preds(os.path.join(c2, "out_fonts", m, "s1", "preds.jsonl")) | |
| for font, name in (("caveat", "B1 printed words in Caveat (40 px)"), ("badscript", "B2 printed words in Bad Script (40 px)")): | |
| rows = [(c, l) for c, l in fonts if c.startswith(font + "_")] | |
| r = block(name, m, rows, fp, pick5(rows)) | |
| if r: | |
| out.append(r) | |
| r = block("C printed PT Sans 40 px (first positive control)", m, printed40, | |
| read_preds(os.path.join(printed_root, m, "s1", "preds.jsonl")), pick5(printed40)) | |
| if r: | |
| out.append(r) | |
| open(out_tsv, "w", encoding="utf-8").write("\n".join(out) + "\n") | |
| print("rows", len(out) - 1) | |