Build a ai powered virtual furniture placement and interior decor app
To upload files, please first save the app
import streamlit as st
import cv2
import numpy as np
from PIL import Image
from skimage.transform import resize
import io
def overlay_furniture(room_image, furniture_image, position, scale=1.0):
"""Overlay furniture image on room image at specified position"""
# Convert PIL images to numpy arrays if needed
if isinstance(room_image, Image.Image):
room_image = np.array(room_image)
if isinstance(furniture_image, Image.Image):
furniture_image = np.array(furniture_image)
# Resize furniture
h, w = furniture_image.shape[:2]
new_h, new_w = int(h * scale), int(w * scale)
furniture_resized = cv2.resize(furniture_image, (new_w, new_h))
# Create mask for transparency
if furniture_resized.shape[2] == 4: # If RGBA
mask = furniture_resized[:, :, 3] / 255.0
furniture_resized = furniture_resized[:, :, :3]
else:
mask = np.ones((new_h, new_w))
x, y = position
# Ensure coordinates are within bounds
if x >= 0 and y >= 0 and x + new_w <= room_image.shape[1] and y + new_h <= room_image.shape[0]:
for c in range(3): # RGB channels
room_image[y:y+new_h, x:x+new_w, c] = (
room_image[y:y+new_h, x:x+new_w, c] * (1 - mask) +
furniture_resized[:, :, c] * mask
)
return room_image
st.title("AI Virtual Interior Designer")
st.sidebar.header("Upload Images")
room_file = st.sidebar.file_uploader("Upload Room Image", type=['jpg', 'jpeg', 'png'])
furniture_file = st.sidebar.file_uploader("Upload Furniture Image", type=['jpg', 'jpeg', 'png'])
if room_file and furniture_file:
# Load images
room_image = Image.open(room_file)
furniture_image = Image.open(furniture_file)
# Convert to RGB if needed
if room_image.mode != 'RGB':
room_image = room_image.convert('RGB')
if furniture_image.mode not in ['RGB', 'RGBA']:
furniture_image = furniture_image.convert('RGBA')
# Display original room
st.subheader("Original Room")
st.image(room_image, use_column_width=True)
# Controls
st.sidebar.subheader("Furniture Controls")
scale = st.sidebar.slider("Furniture Size", 0.1, 2.0, 1.0, 0.1)
col1, col2 = st.sidebar.columns(2)
with col1:
x_pos = st.number_input("X Position", 0, room_image.width, room_image.width // 2)
with col2:
y_pos = st.number_input("Y Position", 0, room_image.height, room_image.height // 2)
# Create composite image
result = overlay_furniture(
np.array(room_image),
np.array(furniture_image),
(int(x_pos), int(y_pos)),
scale
)
# Display result
st.subheader("Virtual Room Design")
st.image(result, use_column_width=True)
# Tips
st.sidebar.markdown("---")
st.sidebar.markdown("""
### Tips:
1. Upload a room photo and furniture item
2. Adjust the furniture size using the slider
3. Position the furniture using X and Y coordinates
4. The furniture will be overlaid on the room image
""")
else:
st.info("Please upload both a room image and a furniture image to begin designing.")
# Example images and instructions
st.markdown("""
### How to use this app:
1. Upload a photo of your room using the sidebar
2. Upload a furniture item with a transparent background (PNG recommended)
3. Use the controls to position and size the furniture
4. Experiment with different positions to find the perfect layout
### Features:
- Real-time furniture placement
- Size adjustment
- Precise positioning controls
- Support for transparent furniture images
### Tips for best results:
- Use well-lit room photos
- Use furniture images with transparent backgrounds
- Take room photos from a straight-on angle
- Ensure good image quality for both room and furniture
""")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?