Refactor image upload and sidebar logic
Browse filesRefactor UI logic to improve image upload handling and sidebar rendering. Update background hex detection and adjust edge cleanup settings.
219 unmodified lines2202212222232242252262272282295 unmodified lines2352362372382392402412422432442452462 unmodified lines24925025125225325425525625725825926026126226326426526626731 unmodified lines299300301302303304305219 unmodified lines# ---------------------------------------------------------------------------# UI - App Shell & Sidebar# ---------------------------------------------------------------------------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)with st.sidebar:st.markdown('<div class="sidebar-header">Engine Settings</div>', unsafe_allow_html=True)method = st.radio("Processing Engine", ["AI Engine (Auto)", "Chroma Key (Studio Green)"], label_visibility="collapsed")5 unmodified linesmodel_label = st.selectbox("Model Tier", list(AI_MODELS), index=0)model_name = AI_MODELS[model_label]with st.expander("Advanced Output Settings"):grow_px = st.slider("Fringe Eraser (px)", 0, 5, 0, help="Shrinks the cutout mask to remove edge halos.")else:st.markdown('<div class="sidebar-header">Keying Parameters</div>', unsafe_allow_html=True)detected_hex = "#3A6047"key_color_hex = st.color_picker("Key Color", detected_hex)tola = st.slider("Tolerance A (Shadows)", 1, 50, 10, help="Pixels darker/closer to this color are deleted.")tolb = st.slider("Tolerance B (Highlights)", tola + 1, 120, 60, help="Pixels beyond this distance are kept 100%.")2 unmodified linesgrow_px = st.slider("Fringe Eraser (px)", 0, 5, 2)# ---------------------------------------------------------------------------# UI - Main Workspace# ---------------------------------------------------------------------------uploaded_file = st.file_uploader("Upload product photo to begin", type=["png", "jpg", "jpeg"], label_visibility="collapsed")if uploaded_file is None:st.info("👋 Upload a garment photo above to launch the extraction studio.")st.stop()image_bytes = uploaded_file.getvalue()original_image = Image.open(io.BytesIO(image_bytes)).convert("RGB")img_array = np.array(original_image)col1, col2 = st.columns(2, gap="large")with col1:31 unmodified lineslabel="↓ Export Transparent Asset (PNG)",data=buf.getvalue(),file_name="product_asset.png",mime="image/png")except ModuleNotFoundError as exc:219 unmodified lines2202212222232242252262272282292302312322332342352362372382392402415 unmodified lines2472482492502512522532542552562572582 unmodified lines26126226326426526626731 unmodified lines299300301302303304305306219 unmodified lines# ---------------------------------------------------------------------------# UI - Main App Logic# ---------------------------------------------------------------------------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)# 1. Image upload must happen first so the sidebar can dynamically read the background hexuploaded_file = st.file_uploader("Upload product photo to begin", type=["png", "jpg", "jpeg"], label_visibility="collapsed")if uploaded_file is None:st.info("👋 Upload a garment photo above to launch the extraction studio.")st.stop()image_bytes = uploaded_file.getvalue()original_image = Image.open(io.BytesIO(image_bytes)).convert("RGB")img_array = np.array(original_image)# 2. Sidebar renders after image is loadedwith st.sidebar:st.markdown('<div class="sidebar-header">Engine Settings</div>', unsafe_allow_html=True)method = st.radio("Processing Engine", ["AI Engine (Auto)", "Chroma Key (Studio Green)"], label_visibility="collapsed")5 unmodified linesmodel_label = st.selectbox("Model Tier", list(AI_MODELS), index=0)model_name = AI_MODELS[model_label]with st.expander("Edge Cleanup"):grow_px = st.slider("Fringe Eraser (px)", 0, 5, 0, help="Shrinks the cutout mask to remove edge halos.")else:st.markdown('<div class="sidebar-header">Keying Parameters</div>', unsafe_allow_html=True)detected_hex = detect_background_hex(img_array)key_color_hex = st.color_picker("Key Color", detected_hex)tola = st.slider("Tolerance A (Shadows)", 1, 50, 10, help="Pixels darker/closer to this color are deleted.")tolb = st.slider("Tolerance B (Highlights)", tola + 1, 120, 60, help="Pixels beyond this distance are kept 100%.")2 unmodified linesgrow_px = st.slider("Fringe Eraser (px)", 0, 5, 2)# 3. Process & Render Resultcol1, col2 = st.columns(2, gap="large")with col1:31 unmodified lineslabel="↓ Export Transparent Asset (PNG)",data=buf.getvalue(),file_name="product_asset.png",mime="image/png",use_container_width=True)except ModuleNotFoundError as exc:
app.py+18-17