Shreyansh1409 commited on
Commit
33ab91b
·
unverified ·
1 Parent(s): 7108ac5

Refactor image upload and sidebar logic

Browse files

Refactor UI logic to improve image upload handling and sidebar rendering. Update background hex detection and adjust edge cleanup settings.

Files changed (1) hide show
  1. app.py +18 -17
app.py CHANGED
@@ -220,10 +220,22 @@ def chroma_cutout(img_array: np.ndarray, key_hex: str, tola: int, tolb: int, gro
220
 
221
 
222
  # ---------------------------------------------------------------------------
223
- # UI - App Shell & Sidebar
224
  # ---------------------------------------------------------------------------
225
  st.markdown('<div class="app-header"><div class="app-title">Garment Extractor Pro</div><div class="app-subtitle">Create clean, transparent product assets instantly.</div></div>', unsafe_allow_html=True)
226
 
 
 
 
 
 
 
 
 
 
 
 
 
227
  with st.sidebar:
228
  st.markdown('<div class="sidebar-header">Engine Settings</div>', unsafe_allow_html=True)
229
  method = st.radio("Processing Engine", ["AI Engine (Auto)", "Chroma Key (Studio Green)"], label_visibility="collapsed")
@@ -235,12 +247,12 @@ with st.sidebar:
235
  model_label = st.selectbox("Model Tier", list(AI_MODELS), index=0)
236
  model_name = AI_MODELS[model_label]
237
 
238
- with st.expander("Advanced Output Settings"):
239
  grow_px = st.slider("Fringe Eraser (px)", 0, 5, 0, help="Shrinks the cutout mask to remove edge halos.")
240
 
241
  else:
242
  st.markdown('<div class="sidebar-header">Keying Parameters</div>', unsafe_allow_html=True)
243
- detected_hex = "#3A6047"
244
  key_color_hex = st.color_picker("Key Color", detected_hex)
245
  tola = st.slider("Tolerance A (Shadows)", 1, 50, 10, help="Pixels darker/closer to this color are deleted.")
246
  tolb = st.slider("Tolerance B (Highlights)", tola + 1, 120, 60, help="Pixels beyond this distance are kept 100%.")
@@ -249,19 +261,7 @@ with st.sidebar:
249
  grow_px = st.slider("Fringe Eraser (px)", 0, 5, 2)
250
 
251
 
252
- # ---------------------------------------------------------------------------
253
- # UI - Main Workspace
254
- # ---------------------------------------------------------------------------
255
- uploaded_file = st.file_uploader("Upload product photo to begin", type=["png", "jpg", "jpeg"], label_visibility="collapsed")
256
-
257
- if uploaded_file is None:
258
- st.info("👋 Upload a garment photo above to launch the extraction studio.")
259
- st.stop()
260
-
261
- image_bytes = uploaded_file.getvalue()
262
- original_image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
263
- img_array = np.array(original_image)
264
-
265
  col1, col2 = st.columns(2, gap="large")
266
 
267
  with col1:
@@ -299,7 +299,8 @@ with col2:
299
  label="↓ Export Transparent Asset (PNG)",
300
  data=buf.getvalue(),
301
  file_name="product_asset.png",
302
- mime="image/png"
 
303
  )
304
 
305
  except ModuleNotFoundError as exc:
 
220
 
221
 
222
  # ---------------------------------------------------------------------------
223
+ # UI - Main App Logic
224
  # ---------------------------------------------------------------------------
225
  st.markdown('<div class="app-header"><div class="app-title">Garment Extractor Pro</div><div class="app-subtitle">Create clean, transparent product assets instantly.</div></div>', unsafe_allow_html=True)
226
 
227
+ # 1. Image upload must happen first so the sidebar can dynamically read the background hex
228
+ uploaded_file = st.file_uploader("Upload product photo to begin", type=["png", "jpg", "jpeg"], label_visibility="collapsed")
229
+
230
+ if uploaded_file is None:
231
+ st.info("👋 Upload a garment photo above to launch the extraction studio.")
232
+ st.stop()
233
+
234
+ image_bytes = uploaded_file.getvalue()
235
+ original_image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
236
+ img_array = np.array(original_image)
237
+
238
+ # 2. Sidebar renders after image is loaded
239
  with st.sidebar:
240
  st.markdown('<div class="sidebar-header">Engine Settings</div>', unsafe_allow_html=True)
241
  method = st.radio("Processing Engine", ["AI Engine (Auto)", "Chroma Key (Studio Green)"], label_visibility="collapsed")
 
247
  model_label = st.selectbox("Model Tier", list(AI_MODELS), index=0)
248
  model_name = AI_MODELS[model_label]
249
 
250
+ with st.expander("Edge Cleanup"):
251
  grow_px = st.slider("Fringe Eraser (px)", 0, 5, 0, help="Shrinks the cutout mask to remove edge halos.")
252
 
253
  else:
254
  st.markdown('<div class="sidebar-header">Keying Parameters</div>', unsafe_allow_html=True)
255
+ detected_hex = detect_background_hex(img_array)
256
  key_color_hex = st.color_picker("Key Color", detected_hex)
257
  tola = st.slider("Tolerance A (Shadows)", 1, 50, 10, help="Pixels darker/closer to this color are deleted.")
258
  tolb = st.slider("Tolerance B (Highlights)", tola + 1, 120, 60, help="Pixels beyond this distance are kept 100%.")
 
261
  grow_px = st.slider("Fringe Eraser (px)", 0, 5, 2)
262
 
263
 
264
+ # 3. Process & Render Result
 
 
 
 
 
 
 
 
 
 
 
 
265
  col1, col2 = st.columns(2, gap="large")
266
 
267
  with col1:
 
299
  label="↓ Export Transparent Asset (PNG)",
300
  data=buf.getvalue(),
301
  file_name="product_asset.png",
302
+ mime="image/png",
303
+ use_container_width=True
304
  )
305
 
306
  except ModuleNotFoundError as exc: