2d to 3d floor plan converter with 3d visualization that only takes 2d floor plans no other images
To upload files, please first save the app
import streamlit as st
import numpy as np
import plotly.graph_objects as go
from PIL import Image
import io
import numpy as np
st.title("2D to 3D Floor Plan Converter")
# File uploader for 2D floor plan
uploaded_file = st.file_uploader("Upload your 2D floor plan (PNG, JPG)", type=['png', 'jpg', 'jpeg'])
def process_floor_plan(image):
# Convert image to grayscale numpy array
img_array = np.array(image.convert('L'))
# Threshold the image to create a binary mask
threshold = 128
binary_mask = (img_array > threshold).astype(np.float32)
# Create height data (walls will be higher than floor)
height_data = binary_mask * 3.0 # Walls will be 3 units high
return height_data
def create_3d_visualization(height_data):
# Create coordinates for the surface
y, x = np.mgrid[0:height_data.shape[0], 0:height_data.shape[1]]
# Create 3D surface plot
fig = go.Figure(data=[
go.Surface(
x=x,
y=y,
z=height_data,
colorscale='Viridis',
showscale=False
)
])
# Update layout for better visualization
fig.update_layout(
scene=dict(
xaxis_title='Width',
yaxis_title='Length',
zaxis_title='Height',
camera=dict(
eye=dict(x=1.5, y=1.5, z=1.5)
)
),
width=800,
height=800,
margin=dict(l=0, r=0, b=0, t=0)
)
return fig
if uploaded_file is not None:
# Open and display original image
image = Image.open(uploaded_file)
st.subheader("Original 2D Floor Plan")
st.image(image, use_column_width=True)
# Process the floor plan
height_data = process_floor_plan(image)
# Create and display 3D visualization
st.subheader("3D Visualization")
fig = create_3d_visualization(height_data)
st.plotly_chart(fig)
# Add some controls for the 3D view
st.sidebar.subheader("Visualization Controls")
st.sidebar.write("""
Use your mouse to interact with the 3D view:
- Left click and drag to rotate
- Right click and drag to pan
- Mouse wheel to zoom
""")
# Add information about the visualization
st.sidebar.subheader("About")
st.sidebar.write("""
This app converts 2D floor plans into simple 3D visualizations.
- White/light areas are interpreted as floor
- Black/dark areas are interpreted as walls
- Walls are shown at 3 units height
""")
else:
st.info("Please upload a 2D floor plan to begin.")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?