Spaces:
Sleeping
Sleeping
Dharini Baskaran
commited on
Commit
ยท
d1af37b
1
Parent(s):
d8997c5
use the huggingface tmp folder
Browse files- app.py +60 -24
- rcnn_model/scripts/rcnn_run.py +1 -1
app.py
CHANGED
|
@@ -4,23 +4,22 @@ import time
|
|
| 4 |
from PIL import Image
|
| 5 |
import os
|
| 6 |
import sys
|
|
|
|
| 7 |
import gdown
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
initial_sidebar_state="collapsed"
|
| 13 |
-
)
|
| 14 |
|
| 15 |
-
print("Streamlit App Starting...")
|
| 16 |
|
| 17 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 18 |
|
| 19 |
# Setup Paths
|
| 20 |
-
UPLOAD_DIR =
|
| 21 |
MODEL_DIR = os.path.join(BASE_DIR, "rcnn_model", "scripts")
|
| 22 |
-
JSON_DIR =
|
| 23 |
-
OUTPUT_DIR =
|
| 24 |
SAMPLE_DIR = os.path.join(BASE_DIR, "rcnn_model", "sample")
|
| 25 |
logo_path = os.path.join(BASE_DIR, "public", "logo.png")
|
| 26 |
model_path = os.path.join(OUTPUT_DIR, "model_final.pth")
|
|
@@ -29,23 +28,39 @@ model_path = os.path.join(OUTPUT_DIR, "model_final.pth")
|
|
| 29 |
GOOGLE_DRIVE_FILE_ID = "1yr64AOgaYZPTcQzG6cxG6lWBENHR9qjW"
|
| 30 |
GDRIVE_URL = f"https://drive.google.com/uc?id={GOOGLE_DRIVE_FILE_ID}"
|
| 31 |
|
|
|
|
| 32 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 33 |
os.makedirs(JSON_DIR, exist_ok=True)
|
| 34 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 35 |
|
|
|
|
| 36 |
# DOWNLOAD MODEL IF MISSING
|
|
|
|
| 37 |
|
| 38 |
if not os.path.exists(model_path):
|
| 39 |
-
print("Model file not found! Downloading from Google Drive...")
|
| 40 |
try:
|
| 41 |
gdown.download(GDRIVE_URL, model_path, quiet=False)
|
| 42 |
-
print("Model downloaded successfully.")
|
| 43 |
except Exception as e:
|
| 44 |
-
print(f"Failed to download model: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
sys.path.append(MODEL_DIR)
|
| 47 |
-
from
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
st.markdown(
|
| 51 |
"""
|
|
@@ -65,28 +80,44 @@ st.markdown(
|
|
| 65 |
unsafe_allow_html=True
|
| 66 |
)
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
st.image(logo_path, width=250)
|
| 69 |
st.markdown("<div class='header-title'>2D Floorplan Vectorizer</div>", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
st.subheader("Upload your Floorplan Image")
|
| 71 |
uploaded_file = st.file_uploader("Choose an image", type=["png", "jpg", "jpeg"])
|
| 72 |
|
|
|
|
| 73 |
if "processing_complete" not in st.session_state:
|
| 74 |
st.session_state.processing_complete = False
|
| 75 |
if "json_output" not in st.session_state:
|
| 76 |
st.session_state.json_output = None
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
col1, col2 = st.columns([1, 2])
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
if uploaded_file is not None:
|
| 81 |
-
print("File Uploaded:", uploaded_file.name)
|
| 82 |
|
| 83 |
-
# Save uploaded file
|
| 84 |
uploaded_path = os.path.join(UPLOAD_DIR, uploaded_file.name)
|
| 85 |
with open(uploaded_path, "wb") as f:
|
| 86 |
f.write(uploaded_file.getbuffer())
|
| 87 |
-
print("Uploaded file saved at:", uploaded_path)
|
| 88 |
|
| 89 |
-
# Display uploaded image
|
| 90 |
with col1:
|
| 91 |
st.markdown("<div class='upload-container'>", unsafe_allow_html=True)
|
| 92 |
st.image(Image.open(uploaded_path), caption="Uploaded Image", use_container_width=True)
|
|
@@ -99,23 +130,24 @@ if uploaded_file is not None:
|
|
| 99 |
progress_bar = st.progress(0)
|
| 100 |
status_text = st.empty()
|
| 101 |
|
| 102 |
-
# Run Model
|
| 103 |
input_image = uploaded_path
|
| 104 |
output_json_name = uploaded_file.name.replace(".png", "_result.json").replace(".jpg", "_result.json").replace(".jpeg", "_result.json")
|
| 105 |
output_image_name = uploaded_file.name.replace(".png", "_result.png").replace(".jpg", "_result.png").replace(".jpeg", "_result.png")
|
| 106 |
|
| 107 |
cfg = write_config()
|
| 108 |
-
print("Model config created. Running model...")
|
| 109 |
|
| 110 |
-
# Simulate progress
|
| 111 |
for i in range(1, 30):
|
| 112 |
time.sleep(0.01)
|
| 113 |
progress_bar.progress(i)
|
| 114 |
status_text.text(f"Preprocessing: {i}%")
|
| 115 |
|
| 116 |
main(cfg, input_image, output_json_name, output_image_name)
|
| 117 |
-
print("Model run complete.")
|
| 118 |
|
|
|
|
| 119 |
output_json_path = os.path.join(JSON_DIR, output_json_name)
|
| 120 |
output_image_path = os.path.join(JSON_DIR, output_image_name)
|
| 121 |
|
|
@@ -136,13 +168,17 @@ if uploaded_file is not None:
|
|
| 136 |
if os.path.exists(output_json_path):
|
| 137 |
with open(output_json_path, "r") as jf:
|
| 138 |
st.session_state.json_output = json.load(jf)
|
| 139 |
-
print("JSON Output Loaded Successfully.")
|
| 140 |
else:
|
| 141 |
st.session_state.json_output = {"error": "JSON output not generated."}
|
| 142 |
-
print("JSON output missing.")
|
| 143 |
|
| 144 |
st.session_state.processing_complete = True
|
| 145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
out_col1, out_col2 = st.columns(2)
|
| 147 |
|
| 148 |
with out_col1:
|
|
@@ -174,4 +210,4 @@ if uploaded_file is not None:
|
|
| 174 |
|
| 175 |
else:
|
| 176 |
st.warning("โ ๏ธ No image uploaded yet.")
|
| 177 |
-
st.session_state.processing_complete = False
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import os
|
| 6 |
import sys
|
| 7 |
+
import shutil
|
| 8 |
import gdown
|
| 9 |
|
| 10 |
+
# ==================================
|
| 11 |
+
# SETUP
|
| 12 |
+
# ==================================
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
print("๐ Streamlit App Starting...")
|
| 15 |
|
| 16 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 17 |
|
| 18 |
# Setup Paths
|
| 19 |
+
UPLOAD_DIR = "/tmp/uploads/"
|
| 20 |
MODEL_DIR = os.path.join(BASE_DIR, "rcnn_model", "scripts")
|
| 21 |
+
JSON_DIR = "/tmp/results/"
|
| 22 |
+
OUTPUT_DIR = "/tmp/output/"
|
| 23 |
SAMPLE_DIR = os.path.join(BASE_DIR, "rcnn_model", "sample")
|
| 24 |
logo_path = os.path.join(BASE_DIR, "public", "logo.png")
|
| 25 |
model_path = os.path.join(OUTPUT_DIR, "model_final.pth")
|
|
|
|
| 28 |
GOOGLE_DRIVE_FILE_ID = "1yr64AOgaYZPTcQzG6cxG6lWBENHR9qjW"
|
| 29 |
GDRIVE_URL = f"https://drive.google.com/uc?id={GOOGLE_DRIVE_FILE_ID}"
|
| 30 |
|
| 31 |
+
# Make folders if they don't exist
|
| 32 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 33 |
os.makedirs(JSON_DIR, exist_ok=True)
|
| 34 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 35 |
|
| 36 |
+
# ==================================
|
| 37 |
# DOWNLOAD MODEL IF MISSING
|
| 38 |
+
# ==================================
|
| 39 |
|
| 40 |
if not os.path.exists(model_path):
|
| 41 |
+
print("๐ Model file not found! Downloading from Google Drive...")
|
| 42 |
try:
|
| 43 |
gdown.download(GDRIVE_URL, model_path, quiet=False)
|
| 44 |
+
print("โ
Model downloaded successfully.")
|
| 45 |
except Exception as e:
|
| 46 |
+
print(f"โ Failed to download model: {e}")
|
| 47 |
+
|
| 48 |
+
# ==================================
|
| 49 |
+
# IMPORT MODEL RUNNER
|
| 50 |
+
# ==================================
|
| 51 |
|
| 52 |
sys.path.append(MODEL_DIR)
|
| 53 |
+
from rcnn_run import main, write_config
|
| 54 |
|
| 55 |
+
# ==================================
|
| 56 |
+
# CSS Styling
|
| 57 |
+
# ==================================
|
| 58 |
+
|
| 59 |
+
st.set_page_config(
|
| 60 |
+
page_title="2D Floorplan Vectorizer",
|
| 61 |
+
layout="wide",
|
| 62 |
+
initial_sidebar_state="collapsed"
|
| 63 |
+
)
|
| 64 |
|
| 65 |
st.markdown(
|
| 66 |
"""
|
|
|
|
| 80 |
unsafe_allow_html=True
|
| 81 |
)
|
| 82 |
|
| 83 |
+
# ==================================
|
| 84 |
+
# HEADER
|
| 85 |
+
# ==================================
|
| 86 |
+
|
| 87 |
st.image(logo_path, width=250)
|
| 88 |
st.markdown("<div class='header-title'>2D Floorplan Vectorizer</div>", unsafe_allow_html=True)
|
| 89 |
+
|
| 90 |
+
# ==================================
|
| 91 |
+
# FILE UPLOAD SECTION
|
| 92 |
+
# ==================================
|
| 93 |
+
|
| 94 |
st.subheader("Upload your Floorplan Image")
|
| 95 |
uploaded_file = st.file_uploader("Choose an image", type=["png", "jpg", "jpeg"])
|
| 96 |
|
| 97 |
+
# Initialize session state
|
| 98 |
if "processing_complete" not in st.session_state:
|
| 99 |
st.session_state.processing_complete = False
|
| 100 |
if "json_output" not in st.session_state:
|
| 101 |
st.session_state.json_output = None
|
| 102 |
|
| 103 |
+
# ==================================
|
| 104 |
+
# IMAGE + JSON Layout
|
| 105 |
+
# ==================================
|
| 106 |
+
|
| 107 |
col1, col2 = st.columns([1, 2])
|
| 108 |
|
| 109 |
+
# ==================================
|
| 110 |
+
# MAIN LOGIC
|
| 111 |
+
# ==================================
|
| 112 |
+
|
| 113 |
if uploaded_file is not None:
|
| 114 |
+
print("๐ค File Uploaded:", uploaded_file.name)
|
| 115 |
|
|
|
|
| 116 |
uploaded_path = os.path.join(UPLOAD_DIR, uploaded_file.name)
|
| 117 |
with open(uploaded_path, "wb") as f:
|
| 118 |
f.write(uploaded_file.getbuffer())
|
| 119 |
+
print("โ
Uploaded file saved at:", uploaded_path)
|
| 120 |
|
|
|
|
| 121 |
with col1:
|
| 122 |
st.markdown("<div class='upload-container'>", unsafe_allow_html=True)
|
| 123 |
st.image(Image.open(uploaded_path), caption="Uploaded Image", use_container_width=True)
|
|
|
|
| 130 |
progress_bar = st.progress(0)
|
| 131 |
status_text = st.empty()
|
| 132 |
|
| 133 |
+
# === ๐ฅ Run Model Here ===
|
| 134 |
input_image = uploaded_path
|
| 135 |
output_json_name = uploaded_file.name.replace(".png", "_result.json").replace(".jpg", "_result.json").replace(".jpeg", "_result.json")
|
| 136 |
output_image_name = uploaded_file.name.replace(".png", "_result.png").replace(".jpg", "_result.png").replace(".jpeg", "_result.png")
|
| 137 |
|
| 138 |
cfg = write_config()
|
| 139 |
+
print("โ๏ธ Model config created. Running model...")
|
| 140 |
|
| 141 |
+
# Simulate progress
|
| 142 |
for i in range(1, 30):
|
| 143 |
time.sleep(0.01)
|
| 144 |
progress_bar.progress(i)
|
| 145 |
status_text.text(f"Preprocessing: {i}%")
|
| 146 |
|
| 147 |
main(cfg, input_image, output_json_name, output_image_name)
|
| 148 |
+
print("โ
Model run complete.")
|
| 149 |
|
| 150 |
+
# Prepare output paths
|
| 151 |
output_json_path = os.path.join(JSON_DIR, output_json_name)
|
| 152 |
output_image_path = os.path.join(JSON_DIR, output_image_name)
|
| 153 |
|
|
|
|
| 168 |
if os.path.exists(output_json_path):
|
| 169 |
with open(output_json_path, "r") as jf:
|
| 170 |
st.session_state.json_output = json.load(jf)
|
| 171 |
+
print("๐ JSON Output Loaded Successfully.")
|
| 172 |
else:
|
| 173 |
st.session_state.json_output = {"error": "JSON output not generated."}
|
| 174 |
+
print("โ JSON output missing.")
|
| 175 |
|
| 176 |
st.session_state.processing_complete = True
|
| 177 |
|
| 178 |
+
# ==================================
|
| 179 |
+
# DISPLAY Output Image + Download Buttons + JSON side-by-side
|
| 180 |
+
# ==================================
|
| 181 |
+
|
| 182 |
out_col1, out_col2 = st.columns(2)
|
| 183 |
|
| 184 |
with out_col1:
|
|
|
|
| 210 |
|
| 211 |
else:
|
| 212 |
st.warning("โ ๏ธ No image uploaded yet.")
|
| 213 |
+
st.session_state.processing_complete = False
|
rcnn_model/scripts/rcnn_run.py
CHANGED
|
@@ -24,7 +24,7 @@ from rcnn_model.extraction.annotation_builder import AnnotationBuilder as AnnBui
|
|
| 24 |
# results_directory = str(from_root("results"))+"/"
|
| 25 |
# sample_data_directory = str(from_root("models/rcnn/sample_data"))+"/"
|
| 26 |
|
| 27 |
-
results_directory = "
|
| 28 |
sample_data_directory = "rcnn_model/sample/"
|
| 29 |
|
| 30 |
def main(cfg,img_source_path, coco_dest_filename, val_img_dest_filename):
|
|
|
|
| 24 |
# results_directory = str(from_root("results"))+"/"
|
| 25 |
# sample_data_directory = str(from_root("models/rcnn/sample_data"))+"/"
|
| 26 |
|
| 27 |
+
results_directory = "/tmp/results/"
|
| 28 |
sample_data_directory = "rcnn_model/sample/"
|
| 29 |
|
| 30 |
def main(cfg,img_source_path, coco_dest_filename, val_img_dest_filename):
|