File size: 2,721 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""Test pass 2: the YOLO11s stage-2 line detector on CPU, with the settings of linedet.det_eval (imgsz 1280, conf 0.25, iou 0.6,
max_det 300) on the 1280 px page copies. Boxes are in the pixels of that copy (as det_eval's stored boxes).

usage: tp2_tob_detect.py --pages TSV --image-col image_1280 --weights best.pt --out JSONL [--chunk C --chunks N] [--threads 16]

LOOPGUARD-1 copy (scripts/r5_loopguard/lg_detect.py) of scripts/r5_testpass2/tp2_tob_detect.py. One change: the csv field
size limit is raised (csv.field_size_limit(10 ** 9), as om_build_pages.py), because the det_prep page files of generated
pages carry polygon columns longer than csv's default 131,072 characters (the unchanged script stopped with
'_csv.Error: field larger than field limit' on pages_gen_val.tsv in prep job 55682). Detector, settings and output unchanged.
"""
import argparse
import csv
import json
import os
import time

os.environ.setdefault("YOLO_OFFLINE", "1")
os.environ.setdefault("YOLO_CONFIG_DIR", "/path/to/TURKIC_OCR_DATA/tmp/ultralytics_cfg")

import torch  # noqa: E402

csv.field_size_limit(10 ** 9)
ap = argparse.ArgumentParser()
ap.add_argument("--pages", required=True)
ap.add_argument("--image-col", default="image_1280")
ap.add_argument("--weights", required=True)
ap.add_argument("--out", required=True)
ap.add_argument("--chunk", type=int, default=0)
ap.add_argument("--chunks", type=int, default=1)
ap.add_argument("--max-pages", type=int, default=0)
ap.add_argument("--threads", type=int, default=16)
a = ap.parse_args()
torch.set_num_threads(a.threads)
from ultralytics import YOLO  # noqa: E402

with open(a.pages, encoding="utf-8", newline="") as f:
    pages = list(csv.DictReader(f, delimiter="\t", quoting=csv.QUOTE_NONE))
pages = pages[a.chunk::a.chunks]
if a.max_pages:
    pages = pages[:a.max_pages]
done = set()
if os.path.exists(a.out):
    for ln in open(a.out, encoding="utf-8"):
        try:
            done.add(json.loads(ln)["id"])
        except Exception:  # noqa: BLE001
            pass
det = YOLO(a.weights)
with open(a.out, "a", encoding="utf-8") as fo:
    for pg in pages:
        if pg["id"] in done:
            continue
        t0 = time.time()
        r = det.predict(pg[a.image_col], imgsz=1280, conf=0.25, iou=0.6, max_det=300, device="cpu", verbose=False)[0]
        boxes = r.boxes.xyxy.cpu().numpy().tolist()
        scores = r.boxes.conf.cpu().numpy().tolist()
        rec = {"id": pg["id"], "n_pred": len(boxes), "boxes": [[round(v, 1) for v in b] for b in boxes],
               "scores": [round(float(s), 4) for s in scores], "seconds": round(time.time() - t0, 2)}
        fo.write(json.dumps(rec) + "\n")
        fo.flush()
print("done", len(pages), "pages", flush=True)