Conversion of uploaded 2d floor plan to 3d and give 3d view of plan layout
To upload files, please first save the app
import streamlit as st
import numpy as np
import matplotlib.pyplot as plt
import open3d as o3d
from PIL import Image
import io
st.title("2D Floor Plan to 3D Visualization")
st.write("""
This app converts a 2D floor plan image into a basic 3D visualization.
Upload a floor plan image (preferably with clear walls and room boundaries).
""")
# File uploader
uploaded_file = st.file_uploader("Upload your floor plan image", type=['png', 'jpg', 'jpeg'])
if uploaded_file is not None:
# Read and process the image
image = Image.open(uploaded_file)
image = image.convert('L') # Convert to grayscale
# Display original image
st.subheader("Original Floor Plan")
st.image(image, use_column_width=True)
# Convert to numpy array and normalize
img_array = np.array(image)
img_array = img_array / 255.0
# Create height map (simple approach - darker pixels will be higher)
height_map = 1 - img_array
# Create 3D points
x, y = np.meshgrid(np.arange(img_array.shape[1]), np.arange(img_array.shape[0]))
z = height_map * 5 # Scale height for better visualization
# Create point cloud
points = np.stack([x.flatten(), y.flatten(), z.flatten()], axis=1)
colors = np.tile([0.8, 0.8, 0.8], (points.shape[0], 1)) # Gray color for points
# Create Open3D point cloud
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(points)
pcd.colors = o3d.utility.Vector3dVector(colors)
# Create visualization
vis = o3d.visualization.Visualizer()
vis.create_window(visible=False)
vis.add_geometry(pcd)
# Set camera view
ctr = vis.get_view_control()
ctr.set_zoom(0.3)
ctr.set_lookat([img_array.shape[1]/2, img_array.shape[0]/2, 0])
ctr.set_up([0, 0, 1])
# Render image
vis.poll_events()
vis.update_renderer()
# Capture image
image_3d = vis.capture_screen_float_buffer(False)
vis.destroy_window()
# Convert to PIL Image and display
image_3d = (np.asarray(image_3d) * 255).astype(np.uint8)
st.subheader("3D Visualization")
st.image(image_3d, use_column_width=True)
st.write("""
Note: This is a basic 3D visualization where:
- Darker areas in the original plan are interpreted as walls/boundaries
- The height is determined by pixel intensity
- The 3D view can be limited but gives a general sense of the layout
For better results, use floor plans with:
- Clear wall boundaries
- Good contrast
- Minimal text or annotations
""")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?