# Is any validation form a duplicate of a test form (same ink under two form ids)? Images only, cached features. import collections import importlib.util import json import os import re import sys import time import numpy as np t0 = time.time() spec = importlib.util.spec_from_file_location("leak", "/path/to/TURKIC_OCR_CODE/scripts/kohtd_leak_scan.py") leak = importlib.util.module_from_spec(spec) sys.modules["leak"] = leak spec.loader.exec_module(leak) OUT, WORKERS = sys.argv[1], int(sys.argv[2]) NAMES = sorted(n for n in os.listdir(leak.IMG) if n.endswith(".jpg")) leak.NAMES = NAMES num = re.compile(r"^(\d+)_(\d+)_(\d+)\.jpg$") grp = np.full(len(NAMES), -1) form = np.full(len(NAMES), -1) for i, n in enumerate(NAMES): m = num.match(n) if m: form[i] = int(m.group(1)) grp[i] = leak.split_of_form(form[i]) z = np.load(os.path.join(OUT, "features.npz"), allow_pickle=True) W, H, STD, DH, PH, BAD = [z[k] for k in ("W", "H", "STD", "DH", "PH", "BAD")] AR = (W / np.maximum(H, 1)).astype(np.float32) valid = (~BAD) & (STD > leak.STD_MIN) iV = np.where((grp == leak.G_V) & valid)[0] iT = np.where((grp == leak.G_T) & valid)[0] bd, bi, _ = leak.nn(DH[iV], PH[iV], AR[iV], DH[iT], PH[iT], AR[iT], [(0, len(iT))]) d = bd[:, 0] cand = np.where(d <= 0.3751)[0] pairs = [(NAMES[iT[bi[q, 0]]], NAMES[iV[q]]) for q in cand] from multiprocessing import Pool with Pool(WORKERS) as pool: ncc = np.array(pool.map(leak.ncc_paths, pairs, chunksize=64)) sel = [k for k in range(len(cand)) if d[cand[k]] <= 0.2091 or ncc[k] >= 0.9] gp = collections.Counter((int(form[iV[cand[k]]]), int(form[iT[bi[cand[k], 0]]])) for k in sel) res = {"queries_validation": len(iV), "database_test": len(iT), "pairs_hash_le_tau_hard": int(len(cand)), "pairs_hash_le_tau_easy": int((d <= 0.2091).sum()), "pairs_ncc_ge_0.9_among_hash_le_tau_hard": int((ncc >= 0.9).sum()), "listed": len(sel), "form_pairs": len(gp), "max_pairs_on_one_form_pair": max(gp.values()) if gp else 0, "top_form_pairs_val_test": [{"val_form": a, "test_form": b, "n": c} for (a, b), c in gp.most_common(5)], "job_id": os.environ.get("SLURM_JOB_ID", "none"), "runtime_seconds": round(time.time() - t0)} json.dump(res, open(os.path.join(OUT, "result_valtest.json"), "w"), indent=1) print(json.dumps(res)) # list and draw the pairs of the top form pair from PIL import Image, ImageDraw, ImageFont top = gp.most_common(1)[0][0] if gp else None rows = [] for k in sel: q = cand[k] a, b = int(form[iV[q]]), int(form[iT[bi[q, 0]]]) rows.append({"val_crop": NAMES[iV[q]], "test_crop": NAMES[iT[bi[q, 0]]], "val_form": a, "test_form": b, "hash_distance": round(float(d[q]), 4), "ncc": round(float(ncc[k]), 4), "val_wxh": [int(W[iV[q]]), int(H[iV[q]])], "test_wxh": [int(W[iT[bi[q, 0]]]), int(H[iT[bi[q, 0]]])]}) rows.sort(key=lambda r: (r["val_form"], r["val_crop"])) json.dump(rows, open(os.path.join(OUT, "result_valtest_pairs.json"), "w"), indent=1) font = ImageFont.truetype(leak.FONT, 13) panels = [] for r in rows: _, ga = leak.load(os.path.join(leak.IMG, r["val_crop"])) _, gb = leak.load(os.path.join(leak.IMG, r["test_crop"])) hh = 60 A = Image.fromarray(ga).resize((max(1, int(ga.shape[1] * hh / ga.shape[0])), hh)) B = Image.fromarray(gb).resize((max(1, int(gb.shape[1] * hh / gb.shape[0])), hh)) c = Image.new("L", (A.width + B.width + 50, hh + 20), 255) c.paste(A, (5, 18)) c.paste(B, (A.width + 40, 18)) ImageDraw.Draw(c).text((5, 1), "val %s | test %s | d %.3f ncc %.3f" % (r["val_crop"], r["test_crop"], r["hash_distance"], r["ncc"]), font=font, fill=0) panels.append(c) img = Image.new("L", (max(p.width for p in panels), sum(p.height + 4 for p in panels)), 255) y = 0 for p in panels: img.paste(p, (0, y)) y += p.height + 4 img.save(os.path.join(OUT, "valtest_pairs_panel.png"))