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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# π Floor Plan Parser
|
| 2 |
+
|
| 3 |
+
Parse floor plan images into structured data β walls with thickness, doors, windows, and automatically detected rooms.
|
| 4 |
+
|
| 5 |
+
## Architecture
|
| 6 |
+
|
| 7 |
+
```
|
| 8 |
+
Floor Plan Image (raster or vector)
|
| 9 |
+
β
|
| 10 |
+
βΌ
|
| 11 |
+
ββββββββββββββββββββββββββββββββ
|
| 12 |
+
β 1. INITIAL PARSE β VLM extracts walls/doors/windows
|
| 13 |
+
β (VLM or vector parser) β into structured JSON schema
|
| 14 |
+
ββββββββββββββββββββββββββββββββ
|
| 15 |
+
β
|
| 16 |
+
βΌ
|
| 17 |
+
ββββββββββββββββββββββββββββββββ
|
| 18 |
+
β 2. COMPUTE TOPOLOGY β Derive rooms from wall faces
|
| 19 |
+
β (computational geometry) β via planar subdivision (Shapely)
|
| 20 |
+
ββββββββββββββββββββββββββββββββ
|
| 21 |
+
β
|
| 22 |
+
βΌ
|
| 23 |
+
ββββββββββββββββββββββββββββββββ
|
| 24 |
+
β 3. RENDER OVERLAY β Schema β SVG/PNG β alpha-composite
|
| 25 |
+
β (PIL / SVG) β on original image for verification
|
| 26 |
+
ββββββββββββββββββββββββββββββββ
|
| 27 |
+
β
|
| 28 |
+
βΌ
|
| 29 |
+
ββββββββββββββββββββββββββββββββ
|
| 30 |
+
β 4. VERIFY & CORRECT β VLM sees overlay vs original,
|
| 31 |
+
β (VLM or human) β outputs field-level corrections
|
| 32 |
+
ββββββββββββββββββββββββββββββββ
|
| 33 |
+
β
|
| 34 |
+
βΌ converged? ββnoβββ back to step 1
|
| 35 |
+
yes
|
| 36 |
+
βΌ
|
| 37 |
+
Final structured schema (JSON)
|
| 38 |
+
```
|
| 39 |
+
|
| 40 |
+
## The Schema
|
| 41 |
+
|
| 42 |
+
Walls are first-class. Everything else references them.
|
| 43 |
+
|
| 44 |
+
```json
|
| 45 |
+
{
|
| 46 |
+
"walls": [
|
| 47 |
+
{
|
| 48 |
+
"id": "w1",
|
| 49 |
+
"centerline": [{"x": 0, "y": 0}, {"x": 6, "y": 0}],
|
| 50 |
+
"thickness": 0.24,
|
| 51 |
+
"openings": [
|
| 52 |
+
{
|
| 53 |
+
"id": "win1",
|
| 54 |
+
"type": "window",
|
| 55 |
+
"start": 1.5,
|
| 56 |
+
"length": 1.5
|
| 57 |
+
}
|
| 58 |
+
]
|
| 59 |
+
}
|
| 60 |
+
],
|
| 61 |
+
"rooms": [
|
| 62 |
+
{
|
| 63 |
+
"id": "r1",
|
| 64 |
+
"label": "bedroom",
|
| 65 |
+
"boundary": [
|
| 66 |
+
{"wall_id": "w1", "side": "right"},
|
| 67 |
+
{"wall_id": "w2", "side": "left"}
|
| 68 |
+
],
|
| 69 |
+
"area": 16.0
|
| 70 |
+
}
|
| 71 |
+
]
|
| 72 |
+
}
|
| 73 |
+
```
|
| 74 |
+
|
| 75 |
+
**Key design decisions:**
|
| 76 |
+
- **Walls have thickness** β exterior (0.20-0.30m) vs interior (0.10-0.15m)
|
| 77 |
+
- **Openings live on walls** β `start` + `length` along the centerline
|
| 78 |
+
- **Rooms are topological** β defined by ordered wall-face references (left/right side), not duplicate coordinates
|
| 79 |
+
- **Curved walls** = polyline centerlines with many sample points
|
| 80 |
+
- **Non-rectangular rooms** fully supported (any angles)
|
| 81 |
+
|
| 82 |
+
## Modules
|
| 83 |
+
|
| 84 |
+
| File | Purpose |
|
| 85 |
+
|------|---------|
|
| 86 |
+
| `floorplan/schema.py` | Pydantic data models β `Wall`, `Opening`, `Room`, `FloorPlan`, `Correction` |
|
| 87 |
+
| `floorplan/geometry.py` | Computational geometry β wall polygons, room detection, centerline ops |
|
| 88 |
+
| `floorplan/renderer.py` | SVG + PIL rendering for visual verification and overlay |
|
| 89 |
+
| `floorplan/parser.py` | VLM-based parsing with iterative correction loop |
|
| 90 |
+
|
| 91 |
+
## Quick Start
|
| 92 |
+
|
| 93 |
+
### Demo (no API key needed)
|
| 94 |
+
|
| 95 |
+
```bash
|
| 96 |
+
pip install pydantic shapely numpy pillow
|
| 97 |
+
python example.py --demo
|
| 98 |
+
```
|
| 99 |
+
|
| 100 |
+
Renders a 2-bedroom apartment with angled walls β `output/demo_floorplan.png` + `.svg` + `.json`
|
| 101 |
+
|
| 102 |
+
### Parse a real floor plan
|
| 103 |
+
|
| 104 |
+
```bash
|
| 105 |
+
pip install pydantic shapely numpy pillow openai
|
| 106 |
+
python example.py --image myfloorplan.png --api-key sk-...
|
| 107 |
+
```
|
| 108 |
+
|
| 109 |
+
Or with a local model (vLLM, Ollama, etc.):
|
| 110 |
+
|
| 111 |
+
```bash
|
| 112 |
+
python example.py --image myfloorplan.png --base-url http://localhost:8000/v1 --model qwen2.5-vl-72b
|
| 113 |
+
```
|
| 114 |
+
|
| 115 |
+
### Python API
|
| 116 |
+
|
| 117 |
+
```python
|
| 118 |
+
from floorplan import FloorPlan, Wall, Opening, OpeningType, Point2D
|
| 119 |
+
from floorplan import build_rooms, render_to_image, render_floorplan_svg
|
| 120 |
+
|
| 121 |
+
# Build a floor plan programmatically
|
| 122 |
+
walls = [
|
| 123 |
+
Wall(id="w1", centerline=[Point2D(x=0, y=0), Point2D(x=5, y=0)], thickness=0.24,
|
| 124 |
+
openings=[Opening(id="d1", type=OpeningType.DOOR, start=1.0, length=0.9)]),
|
| 125 |
+
Wall(id="w2", centerline=[Point2D(x=5, y=0), Point2D(x=5, y=4)], thickness=0.24),
|
| 126 |
+
Wall(id="w3", centerline=[Point2D(x=5, y=4), Point2D(x=0, y=4)], thickness=0.24),
|
| 127 |
+
Wall(id="w4", centerline=[Point2D(x=0, y=4), Point2D(x=0, y=0)], thickness=0.24),
|
| 128 |
+
]
|
| 129 |
+
|
| 130 |
+
rooms, room_polygons = build_rooms(walls)
|
| 131 |
+
fp = FloorPlan(walls=walls, rooms=rooms)
|
| 132 |
+
|
| 133 |
+
# Render
|
| 134 |
+
img = render_to_image(fp, room_polygons=room_polygons)
|
| 135 |
+
img.save("output.png")
|
| 136 |
+
|
| 137 |
+
# Export SVG
|
| 138 |
+
svg = render_floorplan_svg(fp, room_polygons=room_polygons)
|
| 139 |
+
|
| 140 |
+
# Serialize to JSON
|
| 141 |
+
print(fp.model_dump_json(indent=2))
|
| 142 |
+
```
|
| 143 |
+
|
| 144 |
+
### VLM parsing with correction loop
|
| 145 |
+
|
| 146 |
+
```python
|
| 147 |
+
from floorplan import parse_floorplan
|
| 148 |
+
|
| 149 |
+
fp, room_polygons, overlays = parse_floorplan(
|
| 150 |
+
image="floorplan.png",
|
| 151 |
+
api_key="sk-...",
|
| 152 |
+
model="gpt-4o",
|
| 153 |
+
max_iterations=4,
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
# overlays contains the render-on-original images for each iteration
|
| 157 |
+
for i, overlay in enumerate(overlays):
|
| 158 |
+
overlay.save(f"overlay_{i}.png")
|
| 159 |
+
```
|
| 160 |
+
|
| 161 |
+
## Features
|
| 162 |
+
|
| 163 |
+
- β
**Wall thickness** β not just centerlines
|
| 164 |
+
- β
**Non-rectangular rooms** β any angles
|
| 165 |
+
- β
**Curved walls** β polyline approximation
|
| 166 |
+
- β
**Automatic room detection** β from wall topology via Shapely
|
| 167 |
+
- β
**Doors & windows** β parametric on walls
|
| 168 |
+
- β
**SVG + PNG rendering** β dual output
|
| 169 |
+
- β
**Overlay verification** β rendered schema composited on original
|
| 170 |
+
- β
**Iterative VLM correction** β parse β render β compare β correct β repeat
|
| 171 |
+
- β
**JSON serialization** β full Pydantic roundtrip
|
| 172 |
+
- β
**Field-level corrections** β modify, add, delete walls/openings
|
| 173 |
+
|
| 174 |
+
## Dependencies
|
| 175 |
+
|
| 176 |
+
**Core** (always needed):
|
| 177 |
+
- `pydantic` β schema validation
|
| 178 |
+
- `shapely` β computational geometry
|
| 179 |
+
- `numpy` β array ops
|
| 180 |
+
- `pillow` β image rendering
|
| 181 |
+
|
| 182 |
+
**VLM parsing** (optional):
|
| 183 |
+
- `openai` β for VLM API calls (works with any OpenAI-compatible endpoint)
|
| 184 |
+
|
| 185 |
+
## License
|
| 186 |
+
|
| 187 |
+
MIT
|