File size: 3,867 Bytes
d7ebaca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import spaces  # must be imported before torch

import torch
import gradio as gr
from PIL import Image, ImageDraw, ImageFont
from transformers import AutoImageProcessor, AutoModelForObjectDetection

MODEL_ID = "joshlyman/rtdetrv2-floorplancad-doors"

processor = AutoImageProcessor.from_pretrained(MODEL_ID)
model = AutoModelForObjectDetection.from_pretrained(MODEL_ID)
model.eval()

COLORS = {
    "single_door": "#e6194b",
    "double_door": "#3cb44b",
    "sliding_door": "#4363d8",
}


@spaces.GPU(duration=1)
def _noop():  # ZeroGPU requires >=1 decorated function; never called (model runs on CPU)
    pass


def detect_doors(image: Image.Image, confidence_threshold: float = 0.5):
    """Detect single, double, and sliding doors in a floor-plan image and draw the boxes."""
    if image is None:
        return None, "Upload a floor-plan image first."

    image = image.convert("RGB")
    inputs = processor(images=image, return_tensors="pt")
    with torch.no_grad():
        outputs = model(**inputs)

    target_sizes = torch.tensor([image.size[::-1]])
    results = processor.post_process_object_detection(
        outputs, threshold=confidence_threshold, target_sizes=target_sizes
    )[0]

    annotated = image.copy()
    draw = ImageDraw.Draw(annotated)
    try:
        font = ImageFont.truetype("DejaVuSans-Bold.ttf", 16)
    except OSError:
        font = ImageFont.load_default()

    rows = []
    for score, label_id, box in zip(results["scores"], results["labels"], results["boxes"]):
        label = model.config.id2label[label_id.item()]
        conf = score.item()
        x0, y0, x1, y1 = [round(v) for v in box.tolist()]
        color = COLORS.get(label, "#f58231")

        draw.rectangle([x0, y0, x1, y1], outline=color, width=3)
        caption = f"{label} {conf:.2f}"
        text_bbox = draw.textbbox((x0, y0), caption, font=font)
        draw.rectangle(
            [x0, text_bbox[1] - 2, text_bbox[2] + 4, text_bbox[3] + 2], fill=color
        )
        draw.text((x0 + 2, y0), caption, fill="white", font=font)

        rows.append([label, f"{conf:.2%}", f"({x0}, {y0}, {x1}, {y1})"])

    if not rows:
        summary = "No doors detected above this confidence threshold."
    else:
        summary = f"{len(rows)} door(s) detected."

    return annotated, gr.Dataframe(
        headers=["type", "confidence", "box (x0, y0, x1, y1)"], value=rows
    ), summary


with gr.Blocks(title="Floor Plan Door Detector") as demo:
    gr.Markdown(
        "# Floor Plan Door Detector\n"
        f"RT-DETRv2 fine-tuned on floor-plan CAD drawings — "
        f"[{MODEL_ID}](https://huggingface.co/{MODEL_ID}) — "
        "detects **single**, **double**, and **sliding** doors. Upload a floor-plan "
        "image (or an exported CAD/PDF page as an image) to see the detections."
    )
    with gr.Row():
        with gr.Column():
            image_input = gr.Image(type="pil", label="Floor plan")
            threshold = gr.Slider(
                minimum=0.05, maximum=0.95, value=0.5, step=0.05,
                label="Confidence threshold",
            )
            run_btn = gr.Button("Detect doors", variant="primary")
        with gr.Column():
            image_output = gr.Image(type="pil", label="Detections")
            summary_output = gr.Textbox(label="Summary", interactive=False)
            table_output = gr.Dataframe(
                headers=["type", "confidence", "box (x0, y0, x1, y1)"],
                label="Detected doors",
            )

    run_btn.click(
        fn=detect_doors,
        inputs=[image_input, threshold],
        outputs=[image_output, table_output, summary_output],
        api_name="detect_doors",
    )
    image_input.change(
        fn=detect_doors,
        inputs=[image_input, threshold],
        outputs=[image_output, table_output, summary_output],
    )

demo.launch(mcp_server=True)