Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,6 @@ from scipy.ndimage import distance_transform_edt
|
|
| 11 |
# ---------------------------------------------------------------------------
|
| 12 |
st.set_page_config(layout="wide", page_title="Garment Extractor Pro", page_icon="✂️", initial_sidebar_state="expanded")
|
| 13 |
|
| 14 |
-
# Initialize state at the very top to prevent runtime routing crashes
|
| 15 |
if "fringe_val" not in st.session_state:
|
| 16 |
st.session_state.fringe_val = 0
|
| 17 |
|
|
@@ -132,7 +131,6 @@ AI_MODELS = {
|
|
| 132 |
|
| 133 |
MAX_MASK_EDGE = 1024
|
| 134 |
|
| 135 |
-
# MEMORY FIX: max_entries=1 drops the old neural network before loading a new one.
|
| 136 |
@st.cache_resource(show_spinner=False, max_entries=1)
|
| 137 |
def load_ai_session(model_name: str):
|
| 138 |
import onnxruntime as ort
|
|
@@ -160,8 +158,6 @@ def ai_cutout(image_bytes: bytes, model_name: str, grow_px: int) -> bytes:
|
|
| 160 |
if model_name == "u2net_cloth_seg":
|
| 161 |
rgb_mask = raw_mask.convert("RGB")
|
| 162 |
r, g, b = rgb_mask.split()
|
| 163 |
-
|
| 164 |
-
# Red = Upper Clothes, Green = Lower Clothes. Drops the Blue skin channel.
|
| 165 |
clothes_only = np.maximum(np.array(r), np.array(g))
|
| 166 |
mask = Image.fromarray(clothes_only).convert("L")
|
| 167 |
else:
|
|
@@ -280,11 +276,11 @@ with st.sidebar:
|
|
| 280 |
st.markdown("<p style='font-size: 0.9rem; font-weight: 500; margin-bottom: 0;'>Fringe Eraser (px)</p>", unsafe_allow_html=True)
|
| 281 |
col_min, col_slide, col_plus = st.columns([1, 4, 1])
|
| 282 |
with col_min:
|
| 283 |
-
st.button("−", on_click=step_fringe, args=(-1,),
|
| 284 |
with col_slide:
|
| 285 |
grow_px = st.slider("Fringe", 0, 5, key="fringe_val", label_visibility="collapsed")
|
| 286 |
with col_plus:
|
| 287 |
-
st.button("+", on_click=step_fringe, args=(1,),
|
| 288 |
|
| 289 |
|
| 290 |
# 3. Process & Render Result
|
|
@@ -292,7 +288,7 @@ col1, col2 = st.columns(2, gap="large")
|
|
| 292 |
|
| 293 |
with col1:
|
| 294 |
st.markdown('<div class="image-card-title">Source Image</div>', unsafe_allow_html=True)
|
| 295 |
-
st.image(original_image,
|
| 296 |
|
| 297 |
with col2:
|
| 298 |
st.markdown('<div class="image-card-title">Extraction Result</div>', unsafe_allow_html=True)
|
|
@@ -319,7 +315,7 @@ with col2:
|
|
| 319 |
extracted_image = chroma_img.copy()
|
| 320 |
extracted_image.putalpha(Image.fromarray(combined_alpha))
|
| 321 |
|
| 322 |
-
st.image(extracted_image,
|
| 323 |
|
| 324 |
buf = io.BytesIO()
|
| 325 |
extracted_image.save(buf, format="PNG")
|
|
@@ -328,7 +324,7 @@ with col2:
|
|
| 328 |
data=buf.getvalue(),
|
| 329 |
file_name="product_asset.png",
|
| 330 |
mime="image/png",
|
| 331 |
-
|
| 332 |
)
|
| 333 |
|
| 334 |
except ModuleNotFoundError as exc:
|
|
|
|
| 11 |
# ---------------------------------------------------------------------------
|
| 12 |
st.set_page_config(layout="wide", page_title="Garment Extractor Pro", page_icon="✂️", initial_sidebar_state="expanded")
|
| 13 |
|
|
|
|
| 14 |
if "fringe_val" not in st.session_state:
|
| 15 |
st.session_state.fringe_val = 0
|
| 16 |
|
|
|
|
| 131 |
|
| 132 |
MAX_MASK_EDGE = 1024
|
| 133 |
|
|
|
|
| 134 |
@st.cache_resource(show_spinner=False, max_entries=1)
|
| 135 |
def load_ai_session(model_name: str):
|
| 136 |
import onnxruntime as ort
|
|
|
|
| 158 |
if model_name == "u2net_cloth_seg":
|
| 159 |
rgb_mask = raw_mask.convert("RGB")
|
| 160 |
r, g, b = rgb_mask.split()
|
|
|
|
|
|
|
| 161 |
clothes_only = np.maximum(np.array(r), np.array(g))
|
| 162 |
mask = Image.fromarray(clothes_only).convert("L")
|
| 163 |
else:
|
|
|
|
| 276 |
st.markdown("<p style='font-size: 0.9rem; font-weight: 500; margin-bottom: 0;'>Fringe Eraser (px)</p>", unsafe_allow_html=True)
|
| 277 |
col_min, col_slide, col_plus = st.columns([1, 4, 1])
|
| 278 |
with col_min:
|
| 279 |
+
st.button("−", on_click=step_fringe, args=(-1,), width="stretch", key="btn_minus")
|
| 280 |
with col_slide:
|
| 281 |
grow_px = st.slider("Fringe", 0, 5, key="fringe_val", label_visibility="collapsed")
|
| 282 |
with col_plus:
|
| 283 |
+
st.button("+", on_click=step_fringe, args=(1,), width="stretch", key="btn_plus")
|
| 284 |
|
| 285 |
|
| 286 |
# 3. Process & Render Result
|
|
|
|
| 288 |
|
| 289 |
with col1:
|
| 290 |
st.markdown('<div class="image-card-title">Source Image</div>', unsafe_allow_html=True)
|
| 291 |
+
st.image(original_image, width="stretch")
|
| 292 |
|
| 293 |
with col2:
|
| 294 |
st.markdown('<div class="image-card-title">Extraction Result</div>', unsafe_allow_html=True)
|
|
|
|
| 315 |
extracted_image = chroma_img.copy()
|
| 316 |
extracted_image.putalpha(Image.fromarray(combined_alpha))
|
| 317 |
|
| 318 |
+
st.image(extracted_image, width="stretch")
|
| 319 |
|
| 320 |
buf = io.BytesIO()
|
| 321 |
extracted_image.save(buf, format="PNG")
|
|
|
|
| 324 |
data=buf.getvalue(),
|
| 325 |
file_name="product_asset.png",
|
| 326 |
mime="image/png",
|
| 327 |
+
width="stretch"
|
| 328 |
)
|
| 329 |
|
| 330 |
except ModuleNotFoundError as exc:
|