File size: 12,384 Bytes
04382d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1d7e72
04382d7
 
 
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
"""
Floor Plan Segmentation API β€” Hugging Face Space
Segments rooms, walls, doors, windows from floor plan images.

Uses Mask2Former for instance segmentation with fallback to
color-based contour detection for robustness.

API endpoint: POST /api/predict
"""

import gradio as gr
import numpy as np
import cv2
import json
from PIL import Image
import io
import base64

# ─── Model loading ───────────────────────────────────────────

MODEL = None
PROCESSOR = None
USE_MASK2FORMER = False

def load_model():
    """Try loading Mask2Former; fall back to OpenCV contour detection."""
    global MODEL, PROCESSOR, USE_MASK2FORMER
    try:
        from transformers import AutoImageProcessor, Mask2FormerForInstanceSegmentation
        PROCESSOR = AutoImageProcessor.from_pretrained(
            "Hyunwoo1605/mask2former-floorplan-instance-segmentation"
        )
        MODEL = Mask2FormerForInstanceSegmentation.from_pretrained(
            "Hyunwoo1605/mask2former-floorplan-instance-segmentation"
        )
        MODEL.eval()
        USE_MASK2FORMER = True
        print("[INFO] Mask2Former model loaded successfully")
    except Exception as e:
        print(f"[WARN] Could not load Mask2Former: {e}")
        print("[INFO] Using OpenCV contour-based fallback")
        USE_MASK2FORMER = False

load_model()


# ─── Mask2Former inference ───────────────────────────────────

def segment_mask2former(image: np.ndarray) -> dict:
    """Run Mask2Former instance segmentation on floor plan image."""
    import torch

    pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
    inputs = PROCESSOR(images=pil_image, return_tensors="pt")

    with torch.no_grad():
        outputs = MODEL(**inputs)

    # Post-process: get instance masks and labels
    result = PROCESSOR.post_process_instance_segmentation(
        outputs, target_sizes=[pil_image.size[::-1]]
    )[0]

    rooms = []
    walls = []
    h, w = image.shape[:2]

    for seg_info in result["segments_info"]:
        mask = (result["segmentation"] == seg_info["id"]).numpy().astype(np.uint8)
        label_id = seg_info["label_id"]
        score = float(seg_info["score"])

        # Extract contour from mask
        contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        if not contours:
            continue

        # Largest contour = room boundary
        contour = max(contours, key=cv2.contourArea)
        area_px = cv2.contourArea(contour)
        if area_px < 100:  # skip tiny segments
            continue

        # Simplify contour to polygon
        epsilon = 0.02 * cv2.arcLength(contour, True)
        approx = cv2.approxPolyDP(contour, epsilon, True)
        boundary = [{"x": float(p[0][0]) / w, "y": float(p[0][1]) / h} for p in approx]

        # Get label name from model config
        label_name = MODEL.config.id2label.get(label_id, f"class_{label_id}")

        # Map to room type
        room_type = classify_label(label_name)

        if room_type == "wall":
            # Extract wall segments from contour
            for i in range(len(approx)):
                j = (i + 1) % len(approx)
                walls.append({
                    "start": {"x": float(approx[i][0][0]) / w, "y": float(approx[i][0][1]) / h},
                    "end": {"x": float(approx[j][0][0]) / w, "y": float(approx[j][0][1]) / h},
                    "is_exterior": False,
                })
        else:
            rooms.append({
                "name": label_name,
                "type": room_type,
                "boundary": boundary,
                "area_estimate_m2": 0,  # needs scale info
                "has_door": False,
                "has_window": False,
                "confidence": score,
                "floor_type": "parkett",
            })

    return {"rooms": rooms, "walls": walls, "doors": [], "windows": [], "method": "mask2former"}


# ─── OpenCV contour-based fallback ──────────────────────────

def segment_opencv(image: np.ndarray) -> dict:
    """
    OpenCV-based floor plan segmentation using adaptive thresholding
    and contour detection. Works without GPU or ML models.
    """
    h, w = image.shape[:2]
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Adaptive threshold to detect walls (dark lines on light/white background)
    thresh = cv2.adaptiveThreshold(
        gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 5
    )

    # Morphological operations to clean up wall detection
    kernel_close = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
    walls_mask = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel_close, iterations=2)

    # Dilate walls slightly to close small gaps
    kernel_dilate = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
    walls_dilated = cv2.dilate(walls_mask, kernel_dilate, iterations=1)

    # Invert to get room regions (white = room interior)
    rooms_mask = cv2.bitwise_not(walls_dilated)

    # Find room contours
    contours, _ = cv2.findContours(rooms_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    rooms = []
    min_room_area = (w * h) * 0.005  # min 0.5% of image area
    max_room_area = (w * h) * 0.5    # max 50% of image area

    for i, contour in enumerate(contours):
        area = cv2.contourArea(contour)
        if area < min_room_area or area > max_room_area:
            continue

        # Simplify contour
        epsilon = 0.015 * cv2.arcLength(contour, True)
        approx = cv2.approxPolyDP(contour, epsilon, True)

        if len(approx) < 3:
            continue

        boundary = [{"x": float(p[0][0]) / w, "y": float(p[0][1]) / h} for p in approx]

        # Compute bounding rect for aspect ratio
        x_r, y_r, w_r, h_r = cv2.boundingRect(approx)
        aspect = max(w_r, h_r) / max(min(w_r, h_r), 1)

        # Simple room classification by size and shape
        relative_area = area / (w * h)
        if aspect > 5:
            room_type = "hallway"
            name = f"Flur {i+1}"
        elif relative_area > 0.08:
            room_type = "living_room"
            name = f"Raum {i+1}"
        elif relative_area < 0.02:
            room_type = "wc"
            name = f"WC/Bad {i+1}"
        else:
            room_type = "custom"
            name = f"Raum {i+1}"

        rooms.append({
            "name": name,
            "type": room_type,
            "boundary": boundary,
            "area_estimate_m2": 0,
            "has_door": False,
            "has_window": False,
            "confidence": 0.6,
            "floor_type": "parkett",
        })

    # Extract wall segments using Hough Line Transform
    walls = []
    lines = cv2.HoughLinesP(walls_mask, 1, np.pi / 180, 80, minLineLength=30, maxLineGap=10)
    if lines is not None:
        for line in lines[:200]:  # cap at 200 wall segments
            x1, y1, x2, y2 = line[0]
            walls.append({
                "start": {"x": float(x1) / w, "y": float(y1) / h},
                "end": {"x": float(x2) / w, "y": float(y2) / h},
                "is_exterior": False,
            })

    # Detect doors (arcs / small circular segments)
    doors = []
    circles = cv2.HoughCircles(
        gray, cv2.HOUGH_GRADIENT, 1, 50,
        param1=100, param2=30, minRadius=15, maxRadius=80
    )
    if circles is not None:
        for circle in circles[0][:20]:
            cx, cy, r = circle
            doors.append({
                "position": {"x": float(cx) / w, "y": float(cy) / h},
                "width_mm": int(r * 2 * 10),  # rough estimate
                "type": "standard",
            })

    return {"rooms": rooms, "walls": walls, "doors": doors, "windows": [], "method": "opencv"}


# ─── Label mapping ──────────────────────────────────────────

LABEL_MAP = {
    "wall": "wall",
    "room": "custom",
    "living": "living_room",
    "living_room": "living_room",
    "bedroom": "bedroom",
    "bathroom": "bathroom",
    "kitchen": "kitchen",
    "hallway": "hallway",
    "corridor": "hallway",
    "closet": "storage",
    "storage": "storage",
    "balcony": "terrace",
    "door": "door",
    "window": "window",
    "dining": "dining_room",
    "office": "study",
    "garage": "garage",
    "stairs": "staircase",
    "toilet": "wc",
    "wc": "wc",
    "utility": "utility_room",
    "laundry": "utility_room",
    "entrance": "entrance",
}

def classify_label(label: str) -> str:
    """Map model output label to standard room type."""
    label_lower = label.lower().strip()
    for key, value in LABEL_MAP.items():
        if key in label_lower:
            return value
    return "custom"


# ─── Main API function ──────────────────────────────────────

def analyze_floor_plan(image: np.ndarray) -> dict:
    """Analyze a floor plan image and return segmentation results."""
    if image is None:
        return {"error": "No image provided", "rooms": [], "walls": [], "doors": [], "windows": []}

    # Ensure BGR format
    if len(image.shape) == 2:
        image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
    elif image.shape[2] == 4:
        image = cv2.cvtColor(image, cv2.COLOR_RGBA2BGR)

    # Run segmentation
    if USE_MASK2FORMER:
        result = segment_mask2former(image)
    else:
        result = segment_opencv(image)

    result["image_width"] = image.shape[1]
    result["image_height"] = image.shape[0]
    result["_version"] = "hf-space-v1"
    result["_coordSystem"] = "normalized"  # all coords 0..1
    result["notes"] = f"Analyzed using {result.get('method', 'unknown')} method. {len(result.get('rooms', []))} rooms detected."

    return result


# ─── Gradio Interface ────────────────────────────────────────

def gradio_predict(image):
    """Gradio wrapper that returns JSON string + annotated image."""
    result = analyze_floor_plan(image)

    # Draw annotations on image for visualization
    annotated = image.copy()
    h, w = annotated.shape[:2]

    colors = [
        (66, 133, 244), (234, 67, 53), (251, 188, 4), (52, 168, 83),
        (171, 71, 188), (255, 112, 67), (0, 172, 193), (124, 179, 66),
    ]

    for i, room in enumerate(result.get("rooms", [])):
        color = colors[i % len(colors)]
        boundary = room.get("boundary", [])
        if len(boundary) < 3:
            continue

        pts = np.array([[int(p["x"] * w), int(p["y"] * h)] for p in boundary], dtype=np.int32)

        # Semi-transparent fill
        overlay = annotated.copy()
        cv2.fillPoly(overlay, [pts], color)
        cv2.addWeighted(overlay, 0.3, annotated, 0.7, 0, annotated)

        # Boundary outline
        cv2.polylines(annotated, [pts], True, color, 2)

        # Label
        M = cv2.moments(pts)
        if M["m00"] > 0:
            cx = int(M["m10"] / M["m00"])
            cy = int(M["m01"] / M["m00"])
            label = room.get("name", f"Room {i+1}")
            cv2.putText(annotated, label, (cx - 30, cy), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
            cv2.putText(annotated, label, (cx - 30, cy), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1)

    return annotated, json.dumps(result, indent=2, ensure_ascii=False)


with gr.Blocks(title="Floor Plan Segmentation API") as demo:
    gr.Markdown("""
    # Floor Plan Segmentation
    Upload a floor plan image to detect rooms, walls, doors, and windows.

    **API Usage:** `POST /api/predict` with `{"data": [<base64_image>]}`
    """)

    with gr.Row():
        with gr.Column():
            input_image = gr.Image(label="Floor Plan", type="numpy")
            analyze_btn = gr.Button("Analyze", variant="primary")
        with gr.Column():
            output_image = gr.Image(label="Segmentation Result")
            output_json = gr.Textbox(label="JSON Result", lines=15, max_lines=30)

    analyze_btn.click(
        fn=gradio_predict,
        inputs=[input_image],
        outputs=[output_image, output_json],
        api_name="predict",
    )

demo.launch(show_api=True)