hand-drawn floor plan to cad converter
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_floor_plan(image):
# Convert PIL Image to OpenCV format
img = np.array(image)
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
# Apply threshold to get binary image
_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)
# Find contours
contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Create blank image for CAD-like output
cad_output = np.zeros_like(img)
# Draw contours in white
cv2.drawContours(cad_output, contours, -1, (255, 255, 255), 2)
return cad_output
st.title("Hand-drawn Floor Plan to CAD Converter")
st.write("""
Upload a hand-drawn floor plan image and this app will convert it to a CAD-like line drawing.
For best results:
- Use clear, dark lines in your drawing
- Ensure good lighting and contrast in the photo
- Keep the image clean and free of unnecessary marks
""")
uploaded_file = st.file_uploader("Choose a floor plan image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# Open and display original image
image = Image.open(uploaded_file)
st.subheader("Original Floor Plan")
st.image(image, use_column_width=True)
# Process the image and display result
st.subheader("Converted CAD-like Drawing")
try:
processed_image = process_floor_plan(image)
st.image(processed_image, use_column_width=True)
# Add download button for processed image
processed_pil = Image.fromarray(processed_image)
buf = io.BytesIO()
processed_pil.save(buf, format="PNG")
st.download_button(
label="Download CAD Drawing",
data=buf.getvalue(),
file_name="cad_drawing.png",
mime="image/png"
)
except Exception as e:
st.error(f"Error processing image: {str(e)}")
st.write("Please try uploading a different image with clearer lines and better contrast.")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?