import json with open("test_raw_comparison.json", "r", encoding="utf-8") as f: data = json.load(f) # The boxes are identical between default and telugu, but let's check pairing # For each detected box, find its Telugu text and Default text. # Let's map by box center (cx, cy) def get_center(box): xs = [p[0] for p in box] ys = [p[1] for p in box] return sum(xs)/len(xs), sum(ys)/len(ys) tel_boxes = [] for t in data["telugu"]: cx, cy = get_center(t["box"]) tel_boxes.append({"text": t["text"], "conf": t["conf"], "box": t["box"], "cx": cx, "cy": cy}) def_boxes = [] for d in data["default"]: cx, cy = get_center(d["box"]) def_boxes.append({"text": d["text"], "conf": d["conf"], "box": d["box"], "cx": cx, "cy": cy}) # For each telugu box, find nearest default box paired = [] for t in tel_boxes: best_d = None min_dist = 999999 for d in def_boxes: dist = (t["cx"] - d["cx"])**2 + (t["cy"] - d["cy"])**2 if dist < min_dist: min_dist = dist best_d = d paired.append({ "cy": t["cy"], "cx": t["cx"], "box": t["box"], "telugu": t["text"], "tel_conf": t["conf"], "default": best_d["text"] if min_dist < 100 else "", "def_conf": best_d["conf"] if min_dist < 100 else 0 }) # Sort paired by cy (top to bottom) paired.sort(key=lambda x: (x["cy"], x["cx"])) lines = [] for p in paired: lines.append(f"Y:{p['cy']:6.1f} X:{p['cx']:6.1f} | Tel: {p['telugu']:<25} | Def: {p['default']:<18}") with open("scratch_paired_rows.txt", "w", encoding="utf-8") as out: out.write("\n".join(lines)) print(f"Paired {len(paired)} items. Written to scratch_paired_rows.txt")