File size: 6,621 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
"""Draw RAQEEB detections as labelled boxes (class name + confidence) and make before/after images.

  python annotate_raqeeb.py plan.png other.pdf --weights raqeeb-cctv-v1.0.0.pt --thresholds thresholds.json --out out_dir

For every page it writes <name>[_p<page>]_annotated.jpg (boxes and labels), <name>[_p<page>]_before_after.jpg
(original | annotated) and <name>[_p<page>]_boxes.json (box corners in image pixels, class, confidence).
Detection is identical to infer_raqeeb.py (same thresholds, whole page by default, optional --tile).
"""
import argparse
import json
from pathlib import Path
from types import SimpleNamespace

from PIL import Image, ImageDraw, ImageFont

NAMES = ['wall', 'door', 'window', 'open_passage', 'column', 'stair', 'elevator', 'escalator', 'exit_marker', 'gate',
         'space_region']
COLORS = [(235, 60, 60), (20, 170, 60), (40, 100, 240), (255, 140, 30), (130, 60, 220), (0, 160, 170),
          (190, 60, 140), (170, 140, 0), (240, 30, 190), (80, 120, 40), (110, 110, 210)]


def _font(size):
    for name in ('arial.ttf', 'DejaVuSans.ttf'):
        try:
            return ImageFont.truetype(name, size)
        except OSError:
            pass
    try:
        from matplotlib import font_manager
        return ImageFont.truetype(font_manager.findfont('DejaVu Sans'), size)
    except Exception:
        return ImageFont.load_default()


def boxes_from_detections(detections):
    """infer_raqeeb detections (shapely geometry in raster px) -> list of dicts with xyxy boxes."""
    out = []
    for d in detections:
        x0, y0, x1, y1 = d['geometry'].bounds
        out.append(dict(class_id=int(d['class_id']), label=NAMES[int(d['class_id'])],
                        confidence=round(float(d['confidence']), 4),
                        box_xyxy=[round(x0, 1), round(y0, 1), round(x1, 1), round(y1, 1)]))
    return out


def draw_boxes(im, boxes, line=None, font_px=None, fill_alpha=0):
    """Return a copy of `im` with one rectangle per box and a 'class 0.87' tag in the class colour.
    Big regions (space_region) are drawn first so smaller elements stay on top."""
    im = im.convert('RGB')
    w, h = im.size
    line = line or max(2, round(max(w, h) / 900))
    font_px = font_px or max(11, round(max(w, h) / 110))
    font = _font(font_px)
    layer = Image.new('RGBA', im.size, (0, 0, 0, 0))
    d = ImageDraw.Draw(layer)
    order = sorted(boxes, key=lambda b: -(b['box_xyxy'][2] - b['box_xyxy'][0]) * (b['box_xyxy'][3] - b['box_xyxy'][1]))
    for b in order:
        c = COLORS[b['class_id']]
        x0, y0, x1, y1 = b['box_xyxy']
        if fill_alpha:
            d.rectangle([x0, y0, x1, y1], fill=(*c, fill_alpha))
        d.rectangle([x0, y0, x1, y1], outline=(*c, 255), width=line)
    for b in order:  # labels after all boxes so no rectangle covers a label
        c = COLORS[b['class_id']]
        x0, y0, x1, y1 = b['box_xyxy']
        text = f"{b['label']} {b['confidence']:.2f}"
        tx0, ty0, tx1, ty1 = d.textbbox((0, 0), text, font=font)
        tw, th = tx1 - tx0 + 6, ty1 - ty0 + 4
        lx, ly = x0, y0 - th if y0 - th >= 0 else y0  # above the box, or inside it at the top edge
        lx = min(max(0, lx), w - tw)
        d.rectangle([lx, ly, lx + tw, ly + th], fill=(*c, 235))
        d.text((lx + 3 - tx0, ly + 2 - ty0), text, font=font, fill=(255, 255, 255, 255))
    return Image.alpha_composite(im.convert('RGBA'), layer).convert('RGB')


def before_after(before, after, gap=None, title_left='Input', title_right='RAQEEB-CCTV detections'):
    w, h = before.size
    gap = gap or max(8, w // 100)
    bar = max(28, round(h / 22))
    font = _font(round(bar * 0.6))
    canvas = Image.new('RGB', (2 * w + gap, h + bar), 'white')
    canvas.paste(before.convert('RGB'), (0, bar))
    canvas.paste(after, (w + gap, bar))
    d = ImageDraw.Draw(canvas)
    for x, t in ((0, title_left), (w + gap, title_right)):
        d.text((x + 6, round(bar * 0.15)), t, font=font, fill=(20, 20, 20))
    return canvas


def legend(classes, width=900):
    font = _font(20)
    rows = (len(classes) + 3) // 4
    im = Image.new('RGB', (width, 34 * rows + 10), 'white')
    d = ImageDraw.Draw(im)
    for k, c in enumerate(classes):
        x, y = 10 + (k % 4) * (width // 4), 8 + (k // 4) * 34
        d.rectangle([x, y + 3, x + 22, y + 25], outline=COLORS[c], width=4)
        d.text((x + 30, y + 2), NAMES[c], font=font, fill=(20, 20, 20))
    return im


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument('inputs', nargs='+', 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('--device', default='0')
    ap.add_argument('--imgsz', type=int, default=1024)
    ap.add_argument('--tile', type=int, default=0, help='0 = whole page (default)')
    ap.add_argument('--overlap', type=int, default=256)
    ap.add_argument('--dpi', type=float, default=200)
    ap.add_argument('--max-side', type=int, default=2400, help='downscale the saved images to this long side')
    a = ap.parse_args()
    from ultralytics import YOLO
    from infer_raqeeb import image_pages, infer_page
    a.out.mkdir(parents=True, exist_ok=True)
    config = json.loads(a.thresholds.read_text())
    model = YOLO(str(a.weights))
    args = SimpleNamespace(tile=a.tile, overlap=a.overlap, imgsz=a.imgsz, device=a.device)
    for path in a.inputs:
        for im, meta in image_pages(path, a.dpi, None):
            dets, _ = infer_page(model, im, config, args)
            boxes = boxes_from_detections(dets)
            stem = path.stem + (f"_p{meta['page_number']}" if path.suffix.lower() == '.pdf' else '')
            s = min(1.0, a.max_side / max(im.size))
            small = im.resize((round(im.width * s), round(im.height * s)), Image.LANCZOS) if s < 1 else im
            sboxes = [dict(b, box_xyxy=[v * s for v in b['box_xyxy']]) for b in boxes]
            after = draw_boxes(small, sboxes)
            after.save(a.out / f'{stem}_annotated.jpg', quality=92)
            before_after(small, after).save(a.out / f'{stem}_before_after.jpg', quality=90)
            counts = {}
            for b in boxes:
                counts[b['label']] = counts.get(b['label'], 0) + 1
            (a.out / f'{stem}_boxes.json').write_text(json.dumps(dict(
                input=str(path), page=meta['page_number'], image_size=list(im.size), counts=counts, boxes=boxes),
                indent=1))
            print(f'{stem}: {len(boxes)} boxes {counts}', flush=True)


if __name__ == '__main__':
    main()