hand-drawn floor plan sketch and convert it to a CAD-style floor plan using AI
To upload files, please first save the app
import streamlit as st
import cv2
import numpy as np
from PIL import Image
import io
def process_sketch(image):
# Convert PIL Image to OpenCV format
img_array = np.array(image)
# Convert to grayscale
gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
# Apply adaptive thresholding
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY_INV, 11, 2)
# Find contours
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Create clean white background
result = np.ones_like(img_array) * 255
# Draw contours in black
cv2.drawContours(result, contours, -1, (0, 0, 0), 2)
# Convert back to PIL Image
result_pil = Image.fromarray(result)
return result_pil
st.title("Floor Plan Sketch to CAD-Style Converter")
st.write("""
Upload a hand-drawn floor plan sketch and this app will convert it to a cleaner,
CAD-style drawing. For best results:
- Use clear, dark lines in your sketch
- Ensure good lighting and contrast in your photo
- Keep the sketch as flat and straight as possible
""")
uploaded_file = st.file_uploader("Upload your floor plan sketch", type=['png', 'jpg', 'jpeg'])
if uploaded_file is not None:
# Display original image
image = Image.open(uploaded_file)
st.subheader("Original Sketch")
st.image(image, use_column_width=True)
# Process and display result
try:
processed_image = process_sketch(image)
st.subheader("Processed CAD-Style Drawing")
st.image(processed_image, use_column_width=True)
# Add download button for processed image
buf = io.BytesIO()
processed_image.save(buf, format='PNG')
btn = st.download_button(
label="Download Processed Drawing",
data=buf.getvalue(),
file_name="processed_floor_plan.png",
mime="image/png"
)
except Exception as e:
st.error(f"An error occurred while processing the image. Please try a different image. Error: {str(e)}")
st.markdown("""
### Tips for Best Results
1. Draw with clear, dark lines
2. Avoid smudges and unnecessary details
3. Keep the paper flat when taking the photo
4. Ensure good lighting
5. Take the photo directly above the sketch
""")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?