Image Segmentation
ultralytics
yolo
yolo11
instance-segmentation
object-detection
floor-plan
architecture
cad
autocad
building-elements
cctv
Eval Results (legacy)
Instructions to use swedishtech/RAQEEB-CCTV with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- ultralytics
How to use swedishtech/RAQEEB-CCTV with ultralytics:
from huggingface_hub import hf_hub_download from ultralytics import YOLO # pick the weights file from this repo's "Files and versions" tab weights = hf_hub_download("swedishtech/RAQEEB-CCTV", "<weights>.pt") model = YOLO(weights) source = 'http://images.cocodataset.org/val2017/000000039769.jpg' model.predict(source=source, save=True) - Notebooks
- Google Colab
- Kaggle
Download code/annotate_raqeeb.py from swedishtech/RAQEEB-CCTV: direct link, hf CLI and curl.
- Browser
- Download file 6.62 kB
-
https://huggingface.co/swedishtech/RAQEEB-CCTV/resolve/main/code/annotate_raqeeb.py
- Command line
-
hf download hf://swedishtech/RAQEEB-CCTV/code/annotate_raqeeb.py
-
curl -L -o annotate_raqeeb.py https://huggingface.co/swedishtech/RAQEEB-CCTV/resolve/main/code/annotate_raqeeb.py
6.62 kB
| """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() | |