File size: 19,873 Bytes
d0f2b17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
"""RAQEEB on AutoCAD drawings (DWG or DXF): labelled boxes on the rendered drawing and inside the drawing itself.

  python cad_raqeeb.py drawing.dwg --weights raqeeb-cctv-v1.0.0.pt --thresholds thresholds.json --out out_dir ^
      --region ground=194976,1278147,273726,1365497 [--region ...] [--px-per-unit 0.1] [--plot] [--tarch-check]

Steps
 1. DWG -> DXF with AutoCAD's headless accoreconsole (a DXF input is used as is).
 2. Each region (drawing units) is rendered black on white at --px-per-unit pixels per drawing unit; 0.1 means
    100 px per metre for a millimetre drawing, the scale of the CAD (FloorPlanCAD) training images.
 3. RAQEEB runs in overlapping --tile px tiles with the validation-selected per-class thresholds (the same
    infer_raqeeb.infer_page used for PNG/JPEG/PDF) and every detection is drawn as a box with its class name.
 4. Each box is mapped back to drawing coordinates and added to a copy of the drawing, one layer per class
    (RAQEEB-WALL, RAQEEB-DOOR, ...): a closed rectangle plus a 'door 0.91' text -> <name>_raqeeb.dxf and, through
    AutoCAD, <name>_raqeeb.dwg.
 5. --plot: AutoCAD plots each region of the annotated DWG to PDF (DWG To PDF.pc3), i.e. the boxes and labels as
    AutoCAD itself draws them.
 6. --tarch-check: for drawings made with TArch (layers WINDOW, COLUMN, WALL, STAIR, SPACE) the detections are scored
    against the drawing's own objects: door/window/column blocks, wall lines, stair lines and room-name texts.
"""
import argparse
import glob
import json
import re
import subprocess
import time
from pathlib import Path
from types import SimpleNamespace

import numpy as np
from PIL import Image

from annotate_raqeeb import COLORS, NAMES, before_after, boxes_from_detections, draw_boxes

Image.MAX_IMAGE_PIXELS = 400_000_000
ACI = [1, 3, 5, 30, 6, 4, 200, 40, 221, 94, 150]  # fallback AutoCAD colour index per class


# ----------------------------------------------------------------------------------------------- AutoCAD
def find_accoreconsole():
    hits = sorted(glob.glob(r'C:\Program Files\Autodesk\AutoCAD 20*\accoreconsole.exe'))
    if not hits:
        raise FileNotFoundError('AutoCAD accoreconsole.exe not found; pass --accore or give a DXF')
    return hits[-1]


def acad(accore, infile, lines, workdir, tag):
    """Run an AutoCAD script headless on `infile`; returns the console log text."""
    scr = workdir / f'_{tag}.scr'
    scr.write_text('\n'.join(lines) + '\n', encoding='ascii')
    r = subprocess.run([accore, '/i', str(infile), '/s', str(scr), '/l', 'en-US'], cwd=str(workdir),
                       capture_output=True, timeout=3600)
    log = (r.stdout + r.stderr).replace(b'\x00', b'').decode('utf-8', 'replace')
    (workdir / f'_{tag}.log').write_text(log, encoding='utf-8')
    return log


def dwg_to_dxf(accore, dwg, dxf, workdir):
    dxf.unlink(missing_ok=True)
    acad(accore, dwg, ['_.FILEDIA', '0', '_.CMDDIA', '0', '_.SAVEAS', 'DXF', 'V', '2018', '16',
                       f'"{dxf.as_posix()}"', '_.QUIT', 'Y'], workdir, 'dwg_to_dxf')
    if not dxf.exists():
        raise RuntimeError(f'AutoCAD did not write {dxf}; see {workdir / "_dwg_to_dxf.log"}')


def dxf_to_dwg(accore, dxf, dwg, workdir):
    dwg.unlink(missing_ok=True)
    acad(accore, dxf, ['_.FILEDIA', '0', '_.CMDDIA', '0', '_.SAVEAS', '2018', f'"{dwg.as_posix()}"',
                       '_.QUIT', 'Y'], workdir, 'dxf_to_dwg')
    if not dwg.exists():
        raise RuntimeError(f'AutoCAD did not write {dwg}; see {workdir / "_dxf_to_dwg.log"}')


def plot_pdf(accore, dwg, region, pdf, workdir, tag):
    """Plot a model-space window of `dwg` to PDF with AutoCAD (object colours, lineweights)."""
    x0, y0, x1, y1 = region
    portrait = (y1 - y0) > (x1 - x0)
    pdf.unlink(missing_ok=True)
    acad(accore, dwg, [
        '_.FILEDIA', '0', '_.CMDDIA', '0', '_.BACKGROUNDPLOT', '0',
        '-PLOT', 'Y', 'Model', 'DWG To PDF.pc3', 'ISO full bleed A1 (841.00 x 594.00 MM)', 'M',
        'P' if portrait else 'L', 'N', 'W', f'{x0},{y0}', f'{x1},{y1}', 'F', 'C', 'Y', '.', 'Y', 'A',
        f'"{pdf.as_posix()}"', 'N', 'Y', '_.QUIT', 'Y'], workdir, tag)
    return pdf.exists()


def pdf_to_png(pdf, png, max_side=5000):
    import pypdfium2 as pdfium
    doc = pdfium.PdfDocument(str(pdf))
    page = doc[0]
    scale = max_side / max(page.get_width(), page.get_height())
    page.render(scale=scale).to_pil().convert('RGB').save(png)
    doc.close()


# ----------------------------------------------------------------------------------------------- rendering
def load_for_render(dxf, font):
    import ezdxf
    from ezdxf import bbox
    doc = ezdxf.readfile(dxf)
    for s in doc.styles:  # SHX/big fonts are not available outside AutoCAD; use one TrueType CJK-capable font
        s.dxf.font = font
        s.dxf.bigfont = ''
    cache = bbox.Cache()
    ents = []
    for e in doc.modelspace():
        b = bbox.extents([e], fast=True, cache=cache)
        if b.has_data:
            ents.append((e, (b.extmin.x, b.extmin.y, b.extmax.x, b.extmax.y)))
    return doc, ents


def render(doc, ents, region, ppu, out_png, lineweight):
    import matplotlib
    matplotlib.use('Agg')
    import matplotlib.pyplot as plt
    from ezdxf.addons.drawing import Frontend, RenderContext
    from ezdxf.addons.drawing.config import BackgroundPolicy, ColorPolicy, Configuration
    from ezdxf.addons.drawing.matplotlib import MatplotlibBackend
    x0, y0, x1, y1 = region
    W, H = round((x1 - x0) * ppu), round((y1 - y0) * ppu)
    sel = [e for e, b in ents if b[2] >= x0 and b[0] <= x1 and b[3] >= y0 and b[1] <= y1]
    fig = plt.figure(figsize=(W / 100, H / 100), dpi=100)
    ax = fig.add_axes([0, 0, 1, 1])
    ax.set_axis_off()
    cfg = Configuration(color_policy=ColorPolicy.BLACK, background_policy=BackgroundPolicy.WHITE,
                        lineweight_scaling=0, min_lineweight=lineweight)
    Frontend(RenderContext(doc), MatplotlibBackend(ax), config=cfg).draw_entities(sel)
    ax.set_xlim(x0, x1)
    ax.set_ylim(y0, y1)
    fig.savefig(out_png, dpi=100, facecolor='white')
    plt.close(fig)
    im = Image.open(out_png).convert('RGB')
    if im.size != (W, H):
        im = im.resize((W, H), Image.LANCZOS)
        im.save(out_png)
    return im, len(sel)


# ----------------------------------------------------------------------------------------------- geometry
def px_to_world(box, region, ppu):
    x0, y0, x1, y1 = box
    rx0, _, _, ry1 = region
    return [rx0 + x0 / ppu, ry1 - y1 / ppu, rx0 + x1 / ppu, ry1 - y0 / ppu]


def world_to_px(pt, region, ppu):
    return ((pt[0] - region[0]) * ppu, (region[3] - pt[1]) * ppu)


def write_annotated(dxf_in, dxf_out, dets, text_h):
    import ezdxf
    doc = ezdxf.readfile(dxf_in)
    msp = doc.modelspace()
    if 'RAQEEB' not in doc.styles:
        doc.styles.new('RAQEEB', dxfattribs={'font': 'arial.ttf'})
    for c, n in enumerate(NAMES):
        name = f'RAQEEB-{n.upper()}'
        if name not in doc.layers:
            lay = doc.layers.add(name, color=ACI[c])
            lay.rgb = COLORS[c]
            lay.dxf.lineweight = 35
    for d in dets:
        layer = f"RAQEEB-{d['label'].upper()}"
        X0, Y0, X1, Y1 = d['box_world']
        msp.add_lwpolyline([(X0, Y0), (X1, Y0), (X1, Y1), (X0, Y1)], close=True, dxfattribs={'layer': layer})
        t = msp.add_text(f"{d['label']} {d['confidence']:.2f}", height=text_h,
                         dxfattribs={'layer': layer, 'style': 'RAQEEB'})
        t.set_placement((X0, Y1 + 0.25 * text_h))
    doc.saveas(dxf_out)


# ----------------------------------------------------------------------------------------------- TArch check
DOOR_BLOCK = re.compile(r'DOOR|DorLib|LDOR', re.I)
WIN_BLOCK = re.compile(r'WIN', re.I)


def tarch_truth(ents, region):
    x0, y0, x1, y1 = region
    inside = lambda b: x0 <= (b[0] + b[2]) / 2 <= x1 and y0 <= (b[1] + b[3]) / 2 <= y1
    gt = dict(door=[], window=[], column=[], wall_lines=[], stair_lines=[], room_points=[])
    for e, b in ents:
        if not inside(b):
            continue
        t, layer = e.dxftype(), e.dxf.layer.upper()
        if t == 'INSERT':
            name = e.dxf.name
            if WIN_BLOCK.search(name):
                gt['window'].append(b)
            elif DOOR_BLOCK.search(name):
                gt['door'].append(b)
            elif layer == 'COLUMN':
                gt['column'].append(b)
        elif t == 'LINE' and layer == 'WALL':
            gt['wall_lines'].append((e.dxf.start.x, e.dxf.start.y, e.dxf.end.x, e.dxf.end.y))
        elif t in ('LINE', 'LWPOLYLINE') and layer in ('STAIR', '梯道'):
            pts = [(e.dxf.start.x, e.dxf.start.y), (e.dxf.end.x, e.dxf.end.y)] if t == 'LINE' else \
                [(p[0], p[1]) for p in e.get_points('xy')]
            gt['stair_lines'].extend((*pts[i], *pts[i + 1]) for i in range(len(pts) - 1))
        elif t == 'TEXT' and layer == 'SPACE' and e.dxf.text.strip():
            p = e.dxf.insert
            gt['room_points'].append((p.x, p.y))
    return gt


def _iou_ios(p, g):
    ix = max(0.0, min(p[2], g[2]) - max(p[0], g[0]))
    iy = max(0.0, min(p[3], g[3]) - max(p[1], g[1]))
    inter = ix * iy
    ap, ag = (p[2] - p[0]) * (p[3] - p[1]), (g[2] - g[0]) * (g[3] - g[1])
    return inter / max(ap + ag - inter, 1e-9), inter / max(min(ap, ag), 1e-9)


def match(preds, gts, which, thr=0.5):
    """Greedy one-to-one matching by confidence; which=0 -> IoU, 1 -> intersection over the smaller box."""
    used, tp = set(), 0
    for p in sorted(preds, key=lambda d: -d['confidence']):
        best, bv = None, thr
        for j, g in enumerate(gts):
            if j in used:
                continue
            v = _iou_ios(p['box_world'], g)[which]
            if v >= bv:
                best, bv = j, v
        if best is not None:
            used.add(best)
            tp += 1
    return tp


def prf(tp, n_pred, n_gt):
    p = tp / n_pred if n_pred else None
    r = tp / n_gt if n_gt else None
    f = 2 * p * r / (p + r) if p and r else (0.0 if p is not None and r is not None else None)
    return dict(true_positives=tp, predictions=n_pred, truth=n_gt, precision=None if p is None else round(p, 3),
                recall=None if r is None else round(r, 3), f1=None if f is None else round(f, 3))


def tarch_check(gt, dets, raw_dets, region, ppu, size):
    import cv2
    from shapely.geometry import Point
    from shapely.ops import unary_union
    W, H = size
    out = {}
    by = lambda n: [d for d in dets if d['label'] == n]
    for n in ('door', 'window', 'column'):
        out[n] = dict(IoU_0_5=prf(match(by(n), gt[n], 0), len(by(n)), len(gt[n])),
                      overlap_0_5=prf(match(by(n), gt[n], 1), len(by(n)), len(gt[n])))
    # walls: pixel agreement between predicted wall masks and the drawing's WALL lines
    lines = np.zeros((H, W), np.uint8)
    for x0, y0, x1, y1 in gt['wall_lines']:
        a, b = world_to_px((x0, y0), region, ppu), world_to_px((x1, y1), region, ppu)
        cv2.line(lines, tuple(int(round(v)) for v in a), tuple(int(round(v)) for v in b), 1, 1)
    pred = np.zeros((H, W), np.uint8)
    for d in raw_dets:
        if d['class_id'] != 0:
            continue
        geoms = getattr(d['geometry'], 'geoms', [d['geometry']])
        for g in geoms:
            if g.geom_type == 'Polygon' and not g.is_empty:
                cv2.fillPoly(pred, [np.round(np.asarray(g.exterior.coords)).astype(np.int32)], 1)
    if lines.any() and pred.any():
        tol_r, tol_p = max(1, round(50 * ppu)), max(1, round(250 * ppu))
        near_pred = cv2.dilate(pred, np.ones((2 * tol_r + 1, 2 * tol_r + 1), np.uint8))
        dist = cv2.distanceTransform((1 - lines).astype(np.uint8), cv2.DIST_L2, 3)
        out['wall'] = dict(line_pixels_covered=round(float(near_pred[lines > 0].mean()), 3),
                           predicted_pixels_on_walls=round(float((dist[pred > 0] <= tol_p).mean()), 3),
                           note='recall-like: share of WALL-layer line pixels within 50 units of a predicted '
                                'wall mask; precision-like: share of predicted wall pixels within 250 units of a '
                                'WALL-layer line (drawing units, mm here)')
    # stairs: connected groups of STAIR-layer lines -> boxes
    st = np.zeros((H, W), np.uint8)
    for x0, y0, x1, y1 in gt['stair_lines']:
        a, b = world_to_px((x0, y0), region, ppu), world_to_px((x1, y1), region, ppu)
        cv2.line(st, tuple(int(round(v)) for v in a), tuple(int(round(v)) for v in b), 1, 1)
    k = max(3, round(400 * ppu)) | 1
    st = cv2.morphologyEx(st, cv2.MORPH_CLOSE, np.ones((k, k), np.uint8))
    n, _, stats, _ = cv2.connectedComponentsWithStats(st)
    stair_gt = []
    for i in range(1, n):
        x, y, w, h = stats[i, :4]
        if min(w, h) >= 800 * ppu:
            stair_gt.append(px_to_world((x, y, x + w, y + h), region, ppu))
    out['stair'] = dict(overlap_0_5=prf(match(by('stair'), stair_gt, 1), len(by('stair')), len(stair_gt)))
    # rooms: are the room-name texts inside a predicted space_region?
    spaces = [d['geometry'] for d in raw_dets if d['class_id'] == 10]
    if gt['room_points']:
        u = unary_union(spaces) if spaces else None
        hit = sum(1 for p in gt['room_points'] if u is not None and u.contains(Point(*world_to_px(p, region, ppu))))
        out['space_region'] = dict(room_labels=len(gt['room_points']), inside_a_predicted_space=hit,
                                   share=round(hit / len(gt['room_points']), 3), predicted_spaces=len(spaces))
    out['truth_definition'] = ('door: TArch door blocks (*DOOR*, DorLib*, LDOR*), window: *WIN* blocks, column: '
                               'blocks on layer COLUMN, stair: connected STAIR/梯道 line groups, rooms: SPACE texts. '
                               'IoU_0_5 = box IoU >= 0.5; overlap_0_5 = intersection over the smaller box >= 0.5 '
                               '(tolerates door swing arcs being in or out of a box).')
    return out


# ----------------------------------------------------------------------------------------------- main
def main():
    ap = argparse.ArgumentParser()
    ap.add_argument('drawing', type=Path)
    ap.add_argument('--weights', type=Path, required=True)
    ap.add_argument('--thresholds', type=Path, required=True)
    ap.add_argument('--out', type=Path, required=True)
    ap.add_argument('--region', action='append', default=[], help='name=x0,y0,x1,y1 in drawing units')
    ap.add_argument('--px-per-unit', type=float, default=0.1)
    ap.add_argument('--tile', type=int, default=1024)
    ap.add_argument('--overlap', type=int, default=256)
    ap.add_argument('--imgsz', type=int, default=1024)
    ap.add_argument('--device', default='0')
    ap.add_argument('--lineweight', type=float, default=4, help='rendered line width in 1/300 inch at 100 dpi')
    ap.add_argument('--font', default='msyh.ttc')
    ap.add_argument('--text-height', type=float, help='label height in drawing units (default: 0.25%% of region)')
    ap.add_argument('--plot', action='store_true', help='plot the annotated DWG regions to PDF with AutoCAD')
    ap.add_argument('--tarch-check', action='store_true')
    ap.add_argument('--no-dwg', action='store_true', help='skip writing the annotated DWG')
    ap.add_argument('--accore', help='path to accoreconsole.exe')
    a = ap.parse_args()
    a.out.mkdir(parents=True, exist_ok=True)
    out = a.out.resolve()
    accore = None
    if a.drawing.suffix.lower() == '.dwg' or a.plot or not a.no_dwg:
        accore = a.accore or find_accoreconsole()
    stem = a.drawing.stem
    if a.drawing.suffix.lower() == '.dwg':
        dxf = out / f'{stem}.dxf'
        if not dxf.exists():
            print('DWG -> DXF with', accore, flush=True)
            dwg_to_dxf(accore, a.drawing.resolve(), dxf, out)
    else:
        dxf = a.drawing.resolve()
    t = time.time()
    doc, ents = load_for_render(dxf, a.font)
    print(f'loaded {len(ents)} entities in {time.time() - t:.0f}s', flush=True)
    regions = []
    for spec in a.region or []:
        name, coords = spec.split('=')
        regions.append((name, [float(v) for v in coords.split(',')]))
    if not regions:
        xs = [b for _, b in ents]
        regions = [('extents', [min(b[0] for b in xs), min(b[1] for b in xs), max(b[2] for b in xs),
                                max(b[3] for b in xs)])]
    from ultralytics import YOLO
    from infer_raqeeb import infer_page
    model = YOLO(str(a.weights))
    config = json.loads(a.thresholds.read_text())
    args = SimpleNamespace(tile=a.tile, overlap=a.overlap, imgsz=a.imgsz, device=a.device)
    ppu = a.px_per_unit
    all_dets, summary = [], dict(drawing=str(a.drawing), dxf=str(dxf), px_per_unit=ppu, tile=a.tile,
                                 overlap=a.overlap, thresholds=str(a.thresholds), weights=str(a.weights), regions={})
    for name, region in regions:
        t = time.time()
        im, n_ent = render(doc, ents, region, ppu, out / f'{name}_input.png', a.lineweight)
        t_render = time.time() - t
        raw, _ = infer_page(model, im, config, args)
        t_det = time.time() - t - t_render
        boxes = boxes_from_detections(raw)
        for b in boxes:
            b['box_world'] = [round(v, 1) for v in px_to_world(b['box_xyxy'], region, ppu)]
        all_dets.extend(boxes)
        draw_boxes(im, boxes, line=2, font_px=15).save(out / f'{name}_annotated_full.jpg', quality=90)
        s = min(1.0, 2400 / max(im.size))
        small = im.resize((round(im.width * s), round(im.height * s)), Image.LANCZOS)
        after = draw_boxes(small, [dict(b, box_xyxy=[v * s for v in b['box_xyxy']]) for b in boxes], font_px=12)
        after.save(out / f'{name}_annotated.jpg', quality=92)
        before_after(small, after, title_left='AutoCAD drawing (rendered)').save(out / f'{name}_before_after.jpg',
                                                                                 quality=90)
        counts = {}
        for b in boxes:
            counts[b['label']] = counts.get(b['label'], 0) + 1
        rec = dict(region=region, image_size=list(im.size), entities_rendered=n_ent, counts=counts,
                   seconds_render=round(t_render, 1), seconds_detect=round(t_det, 1))
        if a.tarch_check:
            rec['tarch_check'] = tarch_check(tarch_truth(ents, region), boxes, raw, region, ppu, im.size)
        summary['regions'][name] = rec
        (out / f'{name}_detections.json').write_text(json.dumps(dict(rec, boxes=boxes), indent=1, ensure_ascii=False),
                                                     encoding='utf-8')
        print(f'{name}: {len(boxes)} boxes {counts} render {t_render:.0f}s detect {t_det:.0f}s', flush=True)
        if a.tarch_check:
            print(json.dumps(rec['tarch_check'], ensure_ascii=False), flush=True)
    text_h = a.text_height or 0.0025 * max(max(r[2] - r[0], r[3] - r[1]) for _, r in regions)
    ann_dxf = out / f'{stem}_raqeeb.dxf'
    write_annotated(dxf, ann_dxf, all_dets, text_h)
    summary['annotated_dxf'] = str(ann_dxf)
    if not a.no_dwg:
        ann_dwg = out / f'{stem}_raqeeb.dwg'
        dxf_to_dwg(accore, ann_dxf, ann_dwg, out)
        summary['annotated_dwg'] = str(ann_dwg)
        if a.plot:
            for name, region in regions:
                pdf = out / f'{name}_autocad_plot.pdf'
                ok = plot_pdf(accore, ann_dwg, region, pdf, out, f'plot_{name}')
                if ok:
                    pdf_to_png(pdf, out / f'{name}_autocad_plot.png')
                summary['regions'][name]['autocad_plot'] = str(pdf) if ok else 'failed (see log)'
                print(f'{name}: AutoCAD plot', 'ok' if ok else 'FAILED', flush=True)
    (out / 'summary.json').write_text(json.dumps(summary, indent=2, ensure_ascii=False), encoding='utf-8')
    print('done ->', out)


if __name__ == '__main__':
    main()