Dharini Baskaran commited on
Commit
873ac58
Β·
1 Parent(s): 30bfac2

trying gradio

Browse files
Files changed (4) hide show
  1. README.md +4 -4
  2. app.py +61 -151
  3. app_streamlit.py +200 -0
  4. requirements.txt +2 -1
README.md CHANGED
@@ -1,14 +1,14 @@
1
  ---
2
  title: 2D Floorplan Vectorizer
3
  emoji: 🏠
4
- colorFrom: blue
5
- colorTo: green
6
- sdk: docker
 
7
  app_file: app.py
8
  pinned: false
9
  ---
10
 
11
-
12
  # 2D Floorplan Vectorizer
13
 
14
  A Streamlit web app that allows you to upload 2D floorplan images and automatically vectorize them into COCO-style annotations using a trained Mask R-CNN model.
 
1
  ---
2
  title: 2D Floorplan Vectorizer
3
  emoji: 🏠
4
+ colorFrom: green
5
+ colorTo: blue
6
+ sdk: gradio
7
+ sdk_version: "4.15.0"
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
 
12
  # 2D Floorplan Vectorizer
13
 
14
  A Streamlit web app that allows you to upload 2D floorplan images and automatically vectorize them into COCO-style annotations using a trained Mask R-CNN model.
app.py CHANGED
@@ -1,200 +1,110 @@
1
- import streamlit as st
2
- import json
3
- import time
4
- from PIL import Image
5
  import os
6
  import sys
 
7
  import shutil
8
  import gdown
 
 
9
  from io import BytesIO
10
 
11
  # ==================================
12
  # SETUP
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 = "/tmp/uploads/"
21
- MODEL_DIR = os.path.join(BASE_DIR, "rcnn_model", "scripts")
22
  JSON_DIR = "/tmp/results/"
23
  OUTPUT_DIR = "/tmp/output/"
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")
27
 
28
- # Google Drive file download link
29
  GOOGLE_DRIVE_FILE_ID = "1yr64AOgaYZPTcQzG6cxG6lWBENHR9qjW"
30
  GDRIVE_URL = f"https://drive.google.com/uc?id={GOOGLE_DRIVE_FILE_ID}"
31
 
32
- # Create necessary folders
33
  os.makedirs(UPLOAD_DIR, exist_ok=True)
34
  os.makedirs(JSON_DIR, exist_ok=True)
35
  os.makedirs(OUTPUT_DIR, exist_ok=True)
36
 
37
- # ==================================
38
- # DOWNLOAD MODEL IF MISSING
39
- # ==================================
40
-
41
  if not os.path.exists(model_path):
42
- print("πŸš€ Model file not found! Downloading from Google Drive...")
43
  try:
44
  gdown.download(GDRIVE_URL, model_path, quiet=False)
45
  print("βœ… Model downloaded successfully.")
46
  except Exception as e:
47
  print(f"❌ Failed to download model: {e}")
48
 
49
- # ==================================
50
- # IMPORT MODEL RUNNER
51
- # ==================================
52
-
53
  sys.path.append(MODEL_DIR)
54
- from rcnn_model.scripts.rcnn_run import main, write_config
 
 
55
 
56
  # ==================================
57
- # PAGE CONFIG
58
  # ==================================
59
 
60
- st.set_page_config(
61
- page_title="2D Floorplan Vectorizer",
62
- layout="wide",
63
- initial_sidebar_state="collapsed"
64
- )
65
 
66
- # ==================================
67
- # HEADER
68
- # ==================================
 
 
 
 
69
 
70
- st.image(logo_path, width=250)
71
- st.markdown("<div class='header-title'>2D Floorplan Vectorizer</div>", unsafe_allow_html=True)
 
72
 
73
- # ==================================
74
- # FILE UPLOAD SECTION
75
- # ==================================
76
 
77
- st.subheader("Upload your Floorplan Image")
78
- uploaded_file = st.file_uploader("Choose an image", type=["png", "jpg", "jpeg"])
79
 
80
- # Initialize session state
81
- if "processing_complete" not in st.session_state:
82
- st.session_state.processing_complete = False
83
- if "json_output" not in st.session_state:
84
- st.session_state.json_output = None
 
 
 
85
 
86
  # ==================================
87
- # IMAGE + JSON Layout
88
  # ==================================
89
 
90
- col1, col2 = st.columns([1, 2])
 
91
 
92
- # ==================================
93
- # MAIN LOGIC
94
- # ==================================
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
- if uploaded_file is not None:
97
- print("πŸ“€ File Uploaded:", uploaded_file.name)
98
-
99
- image_bytes = uploaded_file.read()
100
- img = Image.open(BytesIO(image_bytes)).convert("RGB")
101
-
102
- uploaded_path = os.path.join(UPLOAD_DIR, uploaded_file.name)
103
- with open(uploaded_path, "wb") as f:
104
- f.write(uploaded_file.getbuffer())
105
- print("βœ… Uploaded file saved at:", uploaded_path)
106
-
107
- with col1:
108
- st.markdown("<div class='upload-container'>", unsafe_allow_html=True)
109
- st.image(Image.open(uploaded_path), caption="Uploaded Image", use_container_width=True)
110
- st.markdown("</div>", unsafe_allow_html=True)
111
-
112
- with col2:
113
- if not st.session_state.processing_complete:
114
- status_placeholder = st.empty()
115
- status_placeholder.info("⏳ Model is processing the uploaded image...")
116
- progress_bar = st.progress(0)
117
- status_text = st.empty()
118
-
119
- # === πŸ”₯ Model Run Here ===
120
- input_image = uploaded_path
121
- output_json_name = uploaded_file.name.replace(".png", "_result.json").replace(".jpg", "_result.json").replace(".jpeg", "_result.json")
122
- output_image_name = uploaded_file.name.replace(".png", "_result.png").replace(".jpg", "_result.png").replace(".jpeg", "_result.png")
123
-
124
- output_json_path = os.path.join(JSON_DIR, output_json_name)
125
- output_image_path = os.path.join(JSON_DIR, output_image_name)
126
-
127
- cfg = write_config()
128
- print("βš™οΈ Model config created. Running model...")
129
-
130
- # Simulate progress
131
- for i in range(1, 30):
132
- time.sleep(0.01)
133
- progress_bar.progress(i)
134
- status_text.text(f"Preprocessing: {i}%")
135
-
136
- # Run model
137
- main(cfg, input_image, output_json_path, output_image_path)
138
- print("βœ… Model run complete.")
139
-
140
- while not os.path.exists(output_json_path):
141
- print("Waiting for JSON output...")
142
- time.sleep(0.5)
143
-
144
- for i in range(30, 100):
145
- time.sleep(0.01)
146
- progress_bar.progress(i)
147
- status_text.text(f"Postprocessing: {i}%")
148
-
149
- progress_bar.empty()
150
- status_text.text("βœ… Processing Complete!")
151
- status_placeholder.success("βœ… Model finished and JSON is ready!")
152
-
153
- # Read generated JSON
154
- if os.path.exists(output_json_path):
155
- with open(output_json_path, "r") as jf:
156
- st.session_state.json_output = json.load(jf)
157
- print("πŸ“„ JSON Output Loaded Successfully.")
158
- else:
159
- st.session_state.json_output = {"error": "JSON output not generated."}
160
- print("❌ JSON output missing.")
161
-
162
- st.session_state.processing_complete = True
163
-
164
- # ==================================
165
- # DISPLAY OUTPUTS
166
- # ==================================
167
-
168
- out_col1, out_col2 = st.columns(2)
169
-
170
- with out_col1:
171
- if os.path.exists(output_image_path):
172
- with open(output_image_path, "rb") as img_file:
173
- image = Image.open(img_file)
174
- st.image(image, caption="πŸ–Ό Output Vectorized Image", use_container_width=True)
175
-
176
- img_file.seek(0)
177
- st.download_button(
178
- label="Download Output Image",
179
- data=img_file,
180
- file_name="floorplan_output.png",
181
- mime="image/png"
182
- )
183
-
184
- if os.path.exists(output_json_path):
185
- json_str = json.dumps(st.session_state.json_output, indent=4)
186
- st.download_button(
187
- label="Download JSON",
188
- data=json_str,
189
- file_name="floorplan_output.json",
190
- mime="application/json"
191
- )
192
-
193
- with out_col2:
194
- st.markdown("<div class='json-container'>", unsafe_allow_html=True)
195
- st.json(st.session_state.json_output)
196
- st.markdown("</div>", unsafe_allow_html=True)
197
-
198
- else:
199
- st.warning("⚠️ No image uploaded yet.")
200
- st.session_state.processing_complete = False
 
1
+ import gradio as gr
 
 
 
2
  import os
3
  import sys
4
+ import json
5
  import shutil
6
  import gdown
7
+ import time
8
+ from PIL import Image
9
  from io import BytesIO
10
 
11
  # ==================================
12
  # SETUP
13
  # ==================================
14
 
15
+ print("πŸš€ Gradio App Starting...")
16
 
17
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
18
 
19
+ # Paths
20
  UPLOAD_DIR = "/tmp/uploads/"
 
21
  JSON_DIR = "/tmp/results/"
22
  OUTPUT_DIR = "/tmp/output/"
23
+ MODEL_DIR = os.path.join(BASE_DIR, "rcnn_model", "scripts")
24
  logo_path = os.path.join(BASE_DIR, "public", "logo.png")
25
  model_path = os.path.join(OUTPUT_DIR, "model_final.pth")
26
 
27
+ # Google Drive model
28
  GOOGLE_DRIVE_FILE_ID = "1yr64AOgaYZPTcQzG6cxG6lWBENHR9qjW"
29
  GDRIVE_URL = f"https://drive.google.com/uc?id={GOOGLE_DRIVE_FILE_ID}"
30
 
31
+ # Create folders
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
  if not os.path.exists(model_path):
38
+ print("πŸš€ Model file not found! Downloading...")
39
  try:
40
  gdown.download(GDRIVE_URL, model_path, quiet=False)
41
  print("βœ… Model downloaded successfully.")
42
  except Exception as e:
43
  print(f"❌ Failed to download model: {e}")
44
 
45
+ # Import model
 
 
 
46
  sys.path.append(MODEL_DIR)
47
+ from rcnn_run import main, write_config
48
+
49
+ cfg = write_config()
50
 
51
  # ==================================
52
+ # MAIN PREDICTION FUNCTION
53
  # ==================================
54
 
55
+ def predict(uploaded_file):
56
+ if uploaded_file is None:
57
+ return None, None, "No file uploaded."
 
 
58
 
59
+ # Save uploaded image
60
+ input_bytes = uploaded_file.read()
61
+ img = Image.open(BytesIO(input_bytes)).convert("RGB")
62
+ input_filename = uploaded_file.name
63
+ uploaded_path = os.path.join(UPLOAD_DIR, input_filename)
64
+ img.save(uploaded_path)
65
+ print(f"βœ… Image saved to {uploaded_path}")
66
 
67
+ # Prepare output paths
68
+ output_json_name = input_filename.replace(".png", "_result.json").replace(".jpg", "_result.json").replace(".jpeg", "_result.json")
69
+ output_image_name = input_filename.replace(".png", "_result.png").replace(".jpg", "_result.png").replace(".jpeg", "_result.png")
70
 
71
+ output_json_path = os.path.join(JSON_DIR, output_json_name)
72
+ output_image_path = os.path.join(JSON_DIR, output_image_name)
 
73
 
74
+ # Run model
75
+ main(cfg, uploaded_path, output_json_path, output_image_path)
76
 
77
+ # Read outputs
78
+ result_img = Image.open(output_image_path) if os.path.exists(output_image_path) else None
79
+ result_json = {}
80
+ if os.path.exists(output_json_path):
81
+ with open(output_json_path, "r") as jf:
82
+ result_json = json.load(jf)
83
+
84
+ return result_img, json.dumps(result_json, indent=2), None
85
 
86
  # ==================================
87
+ # GRADIO UI
88
  # ==================================
89
 
90
+ with gr.Blocks() as demo:
91
+ gr.Markdown("<h1 style='text-align: center;'>🏠 Inovonics 2D Floorplan Vectorizer</h1>")
92
 
93
+ with gr.Row():
94
+ with gr.Column():
95
+ uploaded_file = gr.File(label="Upload your Floorplan Image", type="file")
96
+ run_button = gr.Button("Run Vectorizer πŸ”₯")
97
+
98
+ with gr.Column():
99
+ output_image = gr.Image(label="πŸ–Ό Output Vectorized Image")
100
+ output_json = gr.JSON(label="🧾 Output JSON")
101
+
102
+ error_output = gr.Textbox(label="Error Message", visible=False)
103
+
104
+ run_button.click(
105
+ predict,
106
+ inputs=[uploaded_file],
107
+ outputs=[output_image, output_json, error_output]
108
+ )
109
 
110
+ demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app_streamlit.py ADDED
@@ -0,0 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import json
3
+ import time
4
+ from PIL import Image
5
+ import os
6
+ import sys
7
+ import shutil
8
+ import gdown
9
+ from io import BytesIO
10
+
11
+ # ==================================
12
+ # SETUP
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 = "/tmp/uploads/"
21
+ MODEL_DIR = os.path.join(BASE_DIR, "rcnn_model", "scripts")
22
+ JSON_DIR = "/tmp/results/"
23
+ OUTPUT_DIR = "/tmp/output/"
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")
27
+
28
+ # Google Drive file download link
29
+ GOOGLE_DRIVE_FILE_ID = "1yr64AOgaYZPTcQzG6cxG6lWBENHR9qjW"
30
+ GDRIVE_URL = f"https://drive.google.com/uc?id={GOOGLE_DRIVE_FILE_ID}"
31
+
32
+ # Create necessary folders
33
+ os.makedirs(UPLOAD_DIR, exist_ok=True)
34
+ os.makedirs(JSON_DIR, exist_ok=True)
35
+ os.makedirs(OUTPUT_DIR, exist_ok=True)
36
+
37
+ # ==================================
38
+ # DOWNLOAD MODEL IF MISSING
39
+ # ==================================
40
+
41
+ if not os.path.exists(model_path):
42
+ print("πŸš€ Model file not found! Downloading from Google Drive...")
43
+ try:
44
+ gdown.download(GDRIVE_URL, model_path, quiet=False)
45
+ print("βœ… Model downloaded successfully.")
46
+ except Exception as e:
47
+ print(f"❌ Failed to download model: {e}")
48
+
49
+ # ==================================
50
+ # IMPORT MODEL RUNNER
51
+ # ==================================
52
+
53
+ sys.path.append(MODEL_DIR)
54
+ from rcnn_model.scripts.rcnn_run import main, write_config
55
+
56
+ # ==================================
57
+ # PAGE CONFIG
58
+ # ==================================
59
+
60
+ st.set_page_config(
61
+ page_title="2D Floorplan Vectorizer",
62
+ layout="wide",
63
+ initial_sidebar_state="collapsed"
64
+ )
65
+
66
+ # ==================================
67
+ # HEADER
68
+ # ==================================
69
+
70
+ st.image(logo_path, width=250)
71
+ st.markdown("<div class='header-title'>2D Floorplan Vectorizer</div>", unsafe_allow_html=True)
72
+
73
+ # ==================================
74
+ # FILE UPLOAD SECTION
75
+ # ==================================
76
+
77
+ st.subheader("Upload your Floorplan Image")
78
+ uploaded_file = st.file_uploader("Choose an image", type=["png", "jpg", "jpeg"])
79
+
80
+ # Initialize session state
81
+ if "processing_complete" not in st.session_state:
82
+ st.session_state.processing_complete = False
83
+ if "json_output" not in st.session_state:
84
+ st.session_state.json_output = None
85
+
86
+ # ==================================
87
+ # IMAGE + JSON Layout
88
+ # ==================================
89
+
90
+ col1, col2 = st.columns([1, 2])
91
+
92
+ # ==================================
93
+ # MAIN LOGIC
94
+ # ==================================
95
+
96
+ if uploaded_file is not None:
97
+ print("πŸ“€ File Uploaded:", uploaded_file.name)
98
+
99
+ image_bytes = uploaded_file.read()
100
+ img = Image.open(BytesIO(image_bytes)).convert("RGB")
101
+
102
+ uploaded_path = os.path.join(UPLOAD_DIR, uploaded_file.name)
103
+ with open(uploaded_path, "wb") as f:
104
+ f.write(uploaded_file.getbuffer())
105
+ print("βœ… Uploaded file saved at:", uploaded_path)
106
+
107
+ with col1:
108
+ st.markdown("<div class='upload-container'>", unsafe_allow_html=True)
109
+ st.image(Image.open(uploaded_path), caption="Uploaded Image", use_container_width=True)
110
+ st.markdown("</div>", unsafe_allow_html=True)
111
+
112
+ with col2:
113
+ if not st.session_state.processing_complete:
114
+ status_placeholder = st.empty()
115
+ status_placeholder.info("⏳ Model is processing the uploaded image...")
116
+ progress_bar = st.progress(0)
117
+ status_text = st.empty()
118
+
119
+ # === πŸ”₯ Model Run Here ===
120
+ input_image = uploaded_path
121
+ output_json_name = uploaded_file.name.replace(".png", "_result.json").replace(".jpg", "_result.json").replace(".jpeg", "_result.json")
122
+ output_image_name = uploaded_file.name.replace(".png", "_result.png").replace(".jpg", "_result.png").replace(".jpeg", "_result.png")
123
+
124
+ output_json_path = os.path.join(JSON_DIR, output_json_name)
125
+ output_image_path = os.path.join(JSON_DIR, output_image_name)
126
+
127
+ cfg = write_config()
128
+ print("βš™οΈ Model config created. Running model...")
129
+
130
+ # Simulate progress
131
+ for i in range(1, 30):
132
+ time.sleep(0.01)
133
+ progress_bar.progress(i)
134
+ status_text.text(f"Preprocessing: {i}%")
135
+
136
+ # Run model
137
+ main(cfg, input_image, output_json_path, output_image_path)
138
+ print("βœ… Model run complete.")
139
+
140
+ while not os.path.exists(output_json_path):
141
+ print("Waiting for JSON output...")
142
+ time.sleep(0.5)
143
+
144
+ for i in range(30, 100):
145
+ time.sleep(0.01)
146
+ progress_bar.progress(i)
147
+ status_text.text(f"Postprocessing: {i}%")
148
+
149
+ progress_bar.empty()
150
+ status_text.text("βœ… Processing Complete!")
151
+ status_placeholder.success("βœ… Model finished and JSON is ready!")
152
+
153
+ # Read generated JSON
154
+ if os.path.exists(output_json_path):
155
+ with open(output_json_path, "r") as jf:
156
+ st.session_state.json_output = json.load(jf)
157
+ print("πŸ“„ JSON Output Loaded Successfully.")
158
+ else:
159
+ st.session_state.json_output = {"error": "JSON output not generated."}
160
+ print("❌ JSON output missing.")
161
+
162
+ st.session_state.processing_complete = True
163
+
164
+ # ==================================
165
+ # DISPLAY OUTPUTS
166
+ # ==================================
167
+
168
+ out_col1, out_col2 = st.columns(2)
169
+
170
+ with out_col1:
171
+ if os.path.exists(output_image_path):
172
+ with open(output_image_path, "rb") as img_file:
173
+ image = Image.open(img_file)
174
+ st.image(image, caption="πŸ–Ό Output Vectorized Image", use_container_width=True)
175
+
176
+ img_file.seek(0)
177
+ st.download_button(
178
+ label="Download Output Image",
179
+ data=img_file,
180
+ file_name="floorplan_output.png",
181
+ mime="image/png"
182
+ )
183
+
184
+ if os.path.exists(output_json_path):
185
+ json_str = json.dumps(st.session_state.json_output, indent=4)
186
+ st.download_button(
187
+ label="Download JSON",
188
+ data=json_str,
189
+ file_name="floorplan_output.json",
190
+ mime="application/json"
191
+ )
192
+
193
+ with out_col2:
194
+ st.markdown("<div class='json-container'>", unsafe_allow_html=True)
195
+ st.json(st.session_state.json_output)
196
+ st.markdown("</div>", unsafe_allow_html=True)
197
+
198
+ else:
199
+ st.warning("⚠️ No image uploaded yet.")
200
+ st.session_state.processing_complete = False
requirements.txt CHANGED
@@ -8,4 +8,5 @@ shapely
8
  matplotlib
9
  labelme2coco
10
  numpy
11
- from_root
 
 
8
  matplotlib
9
  labelme2coco
10
  numpy
11
+ from_root
12
+ gradio