2d to 3d floor plan converter with 3d visualization with labels of rooms
To upload files, please first save the app
import streamlit as st
import numpy as np
import plotly.graph_objects as go
import base64
from PIL import Image
import io
st.title("2D to 3D Floor Plan Converter")
# File uploader for the 2D floor plan
uploaded_file = st.file_uploader("Upload your 2D floor plan (PNG, JPG)", type=['png', 'jpg', 'jpeg'])
# Default room dimensions and properties
default_rooms = {
"Living Room": {"width": 6, "length": 8, "height": 3, "color": "lightblue"},
"Kitchen": {"width": 4, "length": 5, "height": 3, "color": "lightgreen"},
"Bedroom": {"width": 4, "length": 4, "height": 3, "color": "pink"},
"Bathroom": {"width": 2.5, "length": 3, "height": 3, "color": "lavender"}
}
# Room configuration section
st.sidebar.header("Room Configuration")
rooms = {}
# Allow users to add/configure rooms
num_rooms = st.sidebar.number_input("Number of rooms", min_value=1, max_value=10, value=4)
for i in range(num_rooms):
room_name = st.sidebar.text_input(f"Room {i+1} name", value=list(default_rooms.keys())[i] if i < len(default_rooms) else f"Room {i+1}")
col1, col2, col3 = st.sidebar.columns(3)
with col1:
width = st.number_input(f"{room_name} width (m)", value=float(default_rooms[list(default_rooms.keys())[i]]["width"]) if i < len(default_rooms) else 4.0, key=f"width_{i}")
with col2:
length = st.number_input(f"{room_name} length (m)", value=float(default_rooms[list(default_rooms.keys())[i]]["length"]) if i < len(default_rooms) else 4.0, key=f"length_{i}")
with col3:
height = st.number_input(f"{room_name} height (m)", value=float(default_rooms[list(default_rooms.keys())[i]]["height"]) if i < len(default_rooms) else 3.0, key=f"height_{i}")
color = st.sidebar.color_picker(f"{room_name} color", value=default_rooms[list(default_rooms.keys())[i]]["color"] if i < len(default_rooms) else "#ffffff")
rooms[room_name] = {
"width": width,
"length": length,
"height": height,
"color": color
}
def create_3d_visualization(rooms):
fig = go.Figure()
# Starting position for placing rooms
x_offset = 0
max_length = 0
for room_name, dimensions in rooms.items():
width = dimensions["width"]
length = dimensions["length"]
height = dimensions["height"]
color = dimensions["color"]
# Create room walls using a 3D mesh
x = [x_offset, x_offset + width, x_offset + width, x_offset, x_offset]
y = [0, 0, length, length, 0]
z = [0, 0, 0, 0, 0]
# Floor
fig.add_trace(go.Mesh3d(
x=[x_offset, x_offset + width, x_offset + width, x_offset],
y=[0, 0, length, length],
z=[0, 0, 0, 0],
color=color,
opacity=0.7,
name=room_name
))
# Walls
for i in range(4):
fig.add_trace(go.Mesh3d(
x=[x[i], x[i+1], x[i+1], x[i]],
y=[y[i], y[i+1], y[i+1], y[i]],
z=[0, 0, height, height],
color=color,
opacity=0.5,
showlegend=False
))
# Add room label
fig.add_trace(go.Scatter3d(
x=[x_offset + width/2],
y=[length/2],
z=[height/2],
mode='text',
text=[room_name],
textposition='middle center',
showlegend=False
))
x_offset += width + 1 # Add spacing between rooms
max_length = max(max_length, length)
fig.update_layout(
scene=dict(
xaxis_title='Width (m)',
yaxis_title='Length (m)',
zaxis_title='Height (m)',
aspectmode='data'
),
title="3D Floor Plan Visualization",
showlegend=True
)
return fig
if uploaded_file:
# Display the uploaded 2D floor plan
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded 2D Floor Plan", use_column_width=True)
# Create and display 3D visualization
st.header("3D Visualization")
fig = create_3d_visualization(rooms)
st.plotly_chart(fig, use_container_width=True)
# Add some helpful instructions
st.markdown("""
### How to use:
1. Upload your 2D floor plan image (optional)
2. Configure rooms in the sidebar:
- Set the number of rooms
- Enter room names
- Adjust dimensions (width, length, height)
- Pick colors for each room
3. The 3D visualization will update automatically
4. Use mouse to rotate, zoom, and pan the 3D view:
- Left click + drag to rotate
- Right click + drag to pan
- Scroll to zoom
""")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?