Build a ai powered virtual furniture placement and interior decor app that helps in designing and furnishing home before buying and trying out
To upload files, please first save the app
import streamlit as st
import cv2
import numpy as np
from PIL import Image
import io
# Set page config
st.set_page_config(page_title="Virtual Interior Designer", layout="wide")
# Initialize session state variables
if 'room_image' not in st.session_state:
st.session_state.room_image = None
if 'furniture_items' not in st.session_state:
st.session_state.furniture_items = []
def overlay_furniture(room_img, furniture_img, position):
"""Overlay furniture image on room image at specified position"""
if room_img is None or furniture_img is None:
return None
# Convert images to numpy arrays
room = np.array(room_img)
furniture = np.array(furniture_img)
# Get dimensions
h, w = furniture.shape[:2]
H, W = room.shape[:2]
# Ensure position is within bounds
x, y = position
x = max(0, min(x, W-w))
y = max(0, min(y, H-h))
# Create mask for furniture
mask = furniture[:, :, 3] if furniture.shape[2] == 4 else np.ones((h, w)) * 255
mask = mask / 255.0
# Blend images
for c in range(3):
room[y:y+h, x:x+w, c] = (1-mask) * room[y:y+h, x:x+w, c] + mask * furniture[:, :, c]
return Image.fromarray(room)
# App title and description
st.title("🏠 Virtual Interior Designer")
st.markdown("""
Design your perfect room by uploading a photo and adding furniture items.
This AI-powered tool helps you visualize how different furniture pieces will look in your space.
""")
# Sidebar for controls
with st.sidebar:
st.header("Room Setup")
uploaded_room = st.file_uploader("Upload Room Photo", type=['png', 'jpg', 'jpeg'])
if uploaded_room:
room_image = Image.open(uploaded_room).convert('RGB')
st.session_state.room_image = room_image
st.header("Furniture Library")
furniture_type = st.selectbox(
"Select Furniture Type",
["Sofa", "Chair", "Table", "Bed", "Lamp", "Bookshelf"]
)
# Simulated furniture library (in a real app, this would be connected to a database)
uploaded_furniture = st.file_uploader("Upload Furniture Image", type=['png', 'jpg', 'jpeg'])
if uploaded_furniture:
furniture_img = Image.open(uploaded_furniture).convert('RGBA')
# Resize furniture image to reasonable size
furniture_img.thumbnail((200, 200))
if st.button("Add Furniture"):
st.session_state.furniture_items.append({
'type': furniture_type,
'image': furniture_img,
'position': [0, 0]
})
# Main content area
col1, col2 = st.columns([2, 1])
with col1:
st.header("Room Preview")
if st.session_state.room_image is not None:
# Create a composite image with all furniture
composite = st.session_state.room_image.copy()
for item in st.session_state.furniture_items:
composite = overlay_furniture(composite, item['image'], item['position'])
st.image(composite, use_column_width=True)
else:
st.info("Please upload a room photo to get started")
with col2:
st.header("Furniture Items")
# Display and control furniture items
for idx, item in enumerate(st.session_state.furniture_items):
st.subheader(f"{item['type']} {idx+1}")
col_pos_x, col_pos_y = st.columns(2)
with col_pos_x:
new_x = st.slider(
"X Position",
0,
st.session_state.room_image.width if st.session_state.room_image else 500,
item['position'][0],
key=f"x_{idx}"
)
with col_pos_y:
new_y = st.slider(
"Y Position",
0,
st.session_state.room_image.height if st.session_state.room_image else 500,
item['position'][1],
key=f"y_{idx}"
)
# Update position
st.session_state.furniture_items[idx]['position'] = [new_x, new_y]
if st.button("Remove", key=f"remove_{idx}"):
st.session_state.furniture_items.pop(idx)
st.rerun()
# Add helpful tips
st.markdown("""
### Tips:
- Upload a well-lit photo of your room
- Add furniture items one at a time
- Use the sliders to position furniture
- Remove items you don't want to keep
- Experiment with different furniture pieces to find the perfect match
""")
# Add a reset button
if st.button("Reset Design"):
st.session_state.furniture_items = []
st.rerun()
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?