ethanrom commited on
Commit
e13d38c
·
1 Parent(s): ec99c40

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +74 -8
  2. bestslab-seg.onnx +3 -0
  3. markup.py +7 -0
app.py CHANGED
@@ -2,13 +2,15 @@ import streamlit as st
2
  from rembg import remove
3
  from PIL import ImageOps, ImageEnhance, Image
4
  from streamlit_option_menu import option_menu
5
- from markup import real_estate_app, real_estate_app_hf, sliders_intro, perspective_intro, manual_bg_intro
6
  from perspective_correction import perspective_correction, perspective_correction2
7
  from streamlit_drawable_canvas import st_canvas
8
  import tempfile
9
-
10
  import numpy as np
11
  import cv2
 
 
12
 
13
 
14
  def tab1():
@@ -120,7 +122,66 @@ def remove_background(image, points):
120
 
121
  return result
122
 
 
123
  def tab3():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  st.header("Manual Background Removal")
125
  st.markdown(manual_bg_intro(), unsafe_allow_html=True)
126
  uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
@@ -139,6 +200,7 @@ def tab3():
139
  with col2:
140
  drawing_mode = "point"
141
  stroke_width = st.slider("Stroke width: ", 1, 25, 3)
 
142
  realtime_update = st.checkbox("Update in realtime", True)
143
 
144
  with col1:
@@ -151,10 +213,13 @@ def tab3():
151
  height=image_height,
152
  width=image_width,
153
  drawing_mode=drawing_mode,
 
154
  key="canvas",
155
  )
156
 
157
  if st.button("Remove Background"):
 
 
158
  if canvas_result.json_data is not None:
159
  points = []
160
  for obj in canvas_result.json_data["objects"]:
@@ -171,10 +236,10 @@ def tab3():
171
  transparent_bg_result = result_image.convert("RGBA")
172
  file_path = "background_removed.png"
173
  transparent_bg_result.save(file_path, format="PNG")
174
- st.image(transparent_bg_result, caption="Background Removed Image")
175
 
176
 
177
- def tab4():
178
  st.header("Image Perspective Correction")
179
  st.write("Upload a transparent PNG image which you have removed the background using the previous tab.")
180
  st.markdown(perspective_intro(),unsafe_allow_html=True)
@@ -220,7 +285,7 @@ def tab4():
220
 
221
  def main():
222
  st.set_page_config(page_title="Background Removal Demo", page_icon=":memo:", layout="wide")
223
- tabs = ["Intro", "AI Background Removal", "Manual Background Removal", "Perspective Correction"]
224
 
225
  with st.sidebar:
226
 
@@ -229,12 +294,13 @@ def main():
229
  tab_functions = {
230
  "Intro": tab1,
231
  "AI Background Removal": tab2,
232
- "Manual Background Removal": tab3,
233
- "Perspective Correction": tab4,
 
234
  }
235
 
236
  if current_tab in tab_functions:
237
  tab_functions[current_tab]()
238
 
239
  if __name__ == "__main__":
240
- main()
 
2
  from rembg import remove
3
  from PIL import ImageOps, ImageEnhance, Image
4
  from streamlit_option_menu import option_menu
5
+ from markup import real_estate_app, real_estate_app_hf, sliders_intro, perspective_intro, manual_bg_intro, segement_intro
6
  from perspective_correction import perspective_correction, perspective_correction2
7
  from streamlit_drawable_canvas import st_canvas
8
  import tempfile
9
+ from ultralytics import YOLO
10
  import numpy as np
11
  import cv2
12
+ import gdown
13
+ import os
14
 
15
 
16
  def tab1():
 
122
 
123
  return result
124
 
125
+
126
  def tab3():
127
+ model_file = 'bestslab-seg.onnx'
128
+ model_url = 'https://drive.google.com/uc?id=1---iqs2llLrgDbzr_S1nzkKUr3sJ_ru3'
129
+
130
+ if not os.path.exists(model_file):
131
+ gdown.download(model_url, model_file, quiet=False)
132
+
133
+ model = YOLO(model_file)
134
+
135
+ st.header("Background Removal with instance Segmentaion")
136
+ st.markdown(segement_intro(), unsafe_allow_html=True)
137
+
138
+ uploaded_file = st.file_uploader('Choose an image', type=['jpg', 'jpeg', 'png'])
139
+ if uploaded_file is not None:
140
+ image = Image.open(uploaded_file)
141
+ image_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
142
+
143
+ col1, col2 = st.columns([2,1])
144
+ with col2:
145
+
146
+ iou_threshold = st.slider('IoU Threshold', min_value=0.0, max_value=1.0, value=0.7)
147
+ conf_threshold = st.slider('Confidence Threshold', min_value=0.0, max_value=1.0, value=0.65)
148
+ show_labels = st.checkbox('Show Labels', value=False)
149
+ show_conf = st.checkbox('Show Confidence Scores', value=False)
150
+ boxes = st.checkbox('Show Boxes', value=True)
151
+
152
+ with col1:
153
+ st.image(image, caption='Input Image', use_column_width=True)
154
+
155
+ if st.button('Apply and Predict'):
156
+
157
+ results = model(
158
+ image_cv,
159
+ iou=iou_threshold,
160
+ conf=conf_threshold,
161
+ show_labels=show_labels,
162
+ show_conf=show_conf,
163
+ boxes=boxes,
164
+ )
165
+
166
+ masks = results[0].masks
167
+ mask_image = np.zeros((image_cv.shape[0], image_cv.shape[1], 4), dtype=np.uint8)
168
+
169
+ annotated_frame = results[0].plot()
170
+ annotated_image = Image.fromarray(cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB))
171
+
172
+ for segment in masks.xy:
173
+ segment = np.array(segment, dtype=np.int32)
174
+ segment = segment.reshape((-1, 1, 2))
175
+ cv2.fillPoly(mask_image, [segment], (255, 255, 255, 255))
176
+
177
+ alpha_channel = mask_image[:, :, 3]
178
+ image_rgba = np.concatenate((image_cv, np.expand_dims(alpha_channel, axis=2)), axis=2)
179
+ masked_image = image_rgba * (mask_image / 255)
180
+ masked_pil = Image.fromarray(masked_image.astype(np.uint8), 'RGBA')
181
+
182
+ st.image([annotated_image, masked_pil], caption=['Detections', 'Masked Image'], use_column_width=True)
183
+
184
+ def tab4():
185
  st.header("Manual Background Removal")
186
  st.markdown(manual_bg_intro(), unsafe_allow_html=True)
187
  uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
 
200
  with col2:
201
  drawing_mode = "point"
202
  stroke_width = st.slider("Stroke width: ", 1, 25, 3)
203
+ #point_display_radius = st.slider("Point display radius: ", 1, 25, 3)
204
  realtime_update = st.checkbox("Update in realtime", True)
205
 
206
  with col1:
 
213
  height=image_height,
214
  width=image_width,
215
  drawing_mode=drawing_mode,
216
+ #point_display_radius=point_display_radius if drawing_mode == 'point' else 0,
217
  key="canvas",
218
  )
219
 
220
  if st.button("Remove Background"):
221
+ st.subheader("This Feature is broken in this hosting service, please DM for Private link")
222
+ """
223
  if canvas_result.json_data is not None:
224
  points = []
225
  for obj in canvas_result.json_data["objects"]:
 
236
  transparent_bg_result = result_image.convert("RGBA")
237
  file_path = "background_removed.png"
238
  transparent_bg_result.save(file_path, format="PNG")
239
+ st.image(transparent_bg_result, caption="Background Removed Image") """
240
 
241
 
242
+ def tab5():
243
  st.header("Image Perspective Correction")
244
  st.write("Upload a transparent PNG image which you have removed the background using the previous tab.")
245
  st.markdown(perspective_intro(),unsafe_allow_html=True)
 
285
 
286
  def main():
287
  st.set_page_config(page_title="Background Removal Demo", page_icon=":memo:", layout="wide")
288
+ tabs = ["Intro", "AI Background Removal", "Background Removal with Segmentaion", "Manual Background Removal", "Perspective Correction"]
289
 
290
  with st.sidebar:
291
 
 
294
  tab_functions = {
295
  "Intro": tab1,
296
  "AI Background Removal": tab2,
297
+ "Background Removal with Segmentaion": tab3,
298
+ "Manual Background Removal": tab4,
299
+ "Perspective Correction": tab5,
300
  }
301
 
302
  if current_tab in tab_functions:
303
  tab_functions[current_tab]()
304
 
305
  if __name__ == "__main__":
306
+ main()
bestslab-seg.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c61cc93ff7fc7477e12072c8ec4d1c96a7e302a33fb4a7cc8366b516c449d77a
3
+ size 183900291
markup.py CHANGED
@@ -68,4 +68,11 @@ def perspective_intro():
68
  def manual_bg_intro():
69
  return """
70
  Click on the canvas and select four or more points, starting from any one corner of the slab and proceeding in order in any direction. The background will be removed using the selected points.
 
 
 
 
 
 
 
71
  """
 
68
  def manual_bg_intro():
69
  return """
70
  Click on the canvas and select four or more points, starting from any one corner of the slab and proceeding in order in any direction. The background will be removed using the selected points.
71
+ """
72
+
73
+ def segement_intro():
74
+ return """
75
+ Uses a custom trained model on multiple variations of the provided images. Accucary will increase if more images are provided and model is fine tuned.
76
+ fine tuning the model for new images is a straight forward process, necessary documentaion and guidlines will be provided.
77
+ Adjust the confidence score and IoU sliders to refine the selected area.
78
  """