Enhance image processing options and UI elements
Browse filesAdded new processing method 'Hybrid (AI + Chroma)' and updated UI components for better user interaction.
app.py
CHANGED
|
@@ -16,7 +16,6 @@ st.markdown(
|
|
| 16 |
<style>
|
| 17 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
|
| 18 |
|
| 19 |
-
/* Base Typography */
|
| 20 |
html, body, [class*="css"] {
|
| 21 |
font-family: 'Inter', sans-serif;
|
| 22 |
}
|
|
@@ -25,7 +24,6 @@ st.markdown(
|
|
| 25 |
header { visibility: hidden; }
|
| 26 |
footer { visibility: hidden; }
|
| 27 |
|
| 28 |
-
/* Layout & Headers */
|
| 29 |
.app-header {
|
| 30 |
margin-bottom: 2rem;
|
| 31 |
padding-bottom: 1rem;
|
|
@@ -48,7 +46,6 @@ st.markdown(
|
|
| 48 |
margin-bottom: 0.5rem;
|
| 49 |
}
|
| 50 |
|
| 51 |
-
/* Buttons */
|
| 52 |
.stButton > button {
|
| 53 |
background-color: #4F46E5 !important;
|
| 54 |
color: white !important;
|
|
@@ -80,7 +77,6 @@ st.markdown(
|
|
| 80 |
box-shadow: 0 4px 6px -1px rgba(16, 185, 129, 0.2) !important;
|
| 81 |
}
|
| 82 |
|
| 83 |
-
/* Image Containers & Checkerboard Background for Transparency */
|
| 84 |
[data-testid="stImage"] {
|
| 85 |
background-color: #ffffff;
|
| 86 |
background-image:
|
|
@@ -104,7 +100,6 @@ st.markdown(
|
|
| 104 |
opacity: 0.8;
|
| 105 |
}
|
| 106 |
|
| 107 |
-
/* File Uploader */
|
| 108 |
[data-testid="stFileUploaderDropzone"] {
|
| 109 |
border-radius: 12px;
|
| 110 |
border: 2px dashed rgba(128, 128, 128, 0.4);
|
|
@@ -229,19 +224,17 @@ image_bytes = uploaded_file.getvalue()
|
|
| 229 |
original_image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 230 |
img_array = np.array(original_image)
|
| 231 |
|
| 232 |
-
# 2. Sidebar renders after image is loaded
|
| 233 |
with st.sidebar:
|
| 234 |
-
# ---
|
| 235 |
if "fringe_val" not in st.session_state:
|
| 236 |
st.session_state.fringe_val = 0
|
| 237 |
|
| 238 |
def step_fringe(delta):
|
| 239 |
-
# Keeps the value strictly between 0 and 5
|
| 240 |
st.session_state.fringe_val = max(0, min(5, st.session_state.fringe_val + delta))
|
| 241 |
-
# -------------------------------------
|
| 242 |
|
| 243 |
st.markdown('<div class="sidebar-header">Engine Settings</div>', unsafe_allow_html=True)
|
| 244 |
-
method = st.radio("Processing Engine", ["AI Engine (Auto)", "Chroma Key (Studio Green)"], label_visibility="collapsed")
|
| 245 |
|
| 246 |
st.markdown("---")
|
| 247 |
|
|
@@ -250,10 +243,9 @@ with st.sidebar:
|
|
| 250 |
model_label = st.selectbox("Model Tier", list(AI_MODELS), index=0)
|
| 251 |
model_name = AI_MODELS[model_label]
|
| 252 |
|
| 253 |
-
with st.expander("Edge Cleanup"):
|
| 254 |
st.markdown("<p style='font-size: 0.9rem; font-weight: 500; margin-bottom: 0;'>Fringe Eraser (px)</p>", unsafe_allow_html=True)
|
| 255 |
col_min, col_slide, col_plus = st.columns([1, 4, 1])
|
| 256 |
-
|
| 257 |
with col_min:
|
| 258 |
st.button("−", on_click=step_fringe, args=(-1,), use_container_width=True, key="ai_minus")
|
| 259 |
with col_slide:
|
|
@@ -261,23 +253,43 @@ with st.sidebar:
|
|
| 261 |
with col_plus:
|
| 262 |
st.button("+", on_click=step_fringe, args=(1,), use_container_width=True, key="ai_plus")
|
| 263 |
|
| 264 |
-
|
| 265 |
st.markdown('<div class="sidebar-header">Keying Parameters</div>', unsafe_allow_html=True)
|
| 266 |
detected_hex = detect_background_hex(img_array)
|
| 267 |
key_color_hex = st.color_picker("Key Color", detected_hex)
|
| 268 |
-
tola = st.slider("Tolerance A (Shadows)", 1, 50, 10
|
| 269 |
-
tolb = st.slider("Tolerance B (Highlights)", tola + 1, 120, 60
|
| 270 |
|
| 271 |
-
with st.expander("Edge Cleanup"):
|
| 272 |
st.markdown("<p style='font-size: 0.9rem; font-weight: 500; margin-bottom: 0;'>Fringe Eraser (px)</p>", unsafe_allow_html=True)
|
| 273 |
col_min, col_slide, col_plus = st.columns([1, 4, 1])
|
| 274 |
-
|
| 275 |
with col_min:
|
| 276 |
st.button("−", on_click=step_fringe, args=(-1,), use_container_width=True, key="chroma_minus")
|
| 277 |
with col_slide:
|
| 278 |
grow_px = st.slider("Fringe", 0, 5, key="fringe_val", label_visibility="collapsed")
|
| 279 |
with col_plus:
|
| 280 |
st.button("+", on_click=step_fringe, args=(1,), use_container_width=True, key="chroma_plus")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
|
| 282 |
# 3. Process & Render Result
|
| 283 |
col1, col2 = st.columns(2, gap="large")
|
|
@@ -293,9 +305,27 @@ with col2:
|
|
| 293 |
if method == "AI Engine (Auto)":
|
| 294 |
with st.spinner(f"🤖 Running {model_label.split(' ')[0]} model..."):
|
| 295 |
extracted_image = Image.open(io.BytesIO(ai_cutout(image_bytes, model_name, grow_px)))
|
| 296 |
-
|
|
|
|
| 297 |
with st.spinner("🟩 Applying color math..."):
|
| 298 |
extracted_image = chroma_cutout(img_array, key_color_hex, tola, tolb, grow_px)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
|
| 300 |
st.image(extracted_image, use_container_width=True)
|
| 301 |
|
|
|
|
| 16 |
<style>
|
| 17 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
|
| 18 |
|
|
|
|
| 19 |
html, body, [class*="css"] {
|
| 20 |
font-family: 'Inter', sans-serif;
|
| 21 |
}
|
|
|
|
| 24 |
header { visibility: hidden; }
|
| 25 |
footer { visibility: hidden; }
|
| 26 |
|
|
|
|
| 27 |
.app-header {
|
| 28 |
margin-bottom: 2rem;
|
| 29 |
padding-bottom: 1rem;
|
|
|
|
| 46 |
margin-bottom: 0.5rem;
|
| 47 |
}
|
| 48 |
|
|
|
|
| 49 |
.stButton > button {
|
| 50 |
background-color: #4F46E5 !important;
|
| 51 |
color: white !important;
|
|
|
|
| 77 |
box-shadow: 0 4px 6px -1px rgba(16, 185, 129, 0.2) !important;
|
| 78 |
}
|
| 79 |
|
|
|
|
| 80 |
[data-testid="stImage"] {
|
| 81 |
background-color: #ffffff;
|
| 82 |
background-image:
|
|
|
|
| 100 |
opacity: 0.8;
|
| 101 |
}
|
| 102 |
|
|
|
|
| 103 |
[data-testid="stFileUploaderDropzone"] {
|
| 104 |
border-radius: 12px;
|
| 105 |
border: 2px dashed rgba(128, 128, 128, 0.4);
|
|
|
|
| 224 |
original_image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 225 |
img_array = np.array(original_image)
|
| 226 |
|
|
|
|
| 227 |
with st.sidebar:
|
| 228 |
+
# --- Session State for + / - Steppers ---
|
| 229 |
if "fringe_val" not in st.session_state:
|
| 230 |
st.session_state.fringe_val = 0
|
| 231 |
|
| 232 |
def step_fringe(delta):
|
|
|
|
| 233 |
st.session_state.fringe_val = max(0, min(5, st.session_state.fringe_val + delta))
|
| 234 |
+
# ----------------------------------------
|
| 235 |
|
| 236 |
st.markdown('<div class="sidebar-header">Engine Settings</div>', unsafe_allow_html=True)
|
| 237 |
+
method = st.radio("Processing Engine", ["AI Engine (Auto)", "Chroma Key (Studio Green)", "Hybrid (AI + Chroma)"], label_visibility="collapsed")
|
| 238 |
|
| 239 |
st.markdown("---")
|
| 240 |
|
|
|
|
| 243 |
model_label = st.selectbox("Model Tier", list(AI_MODELS), index=0)
|
| 244 |
model_name = AI_MODELS[model_label]
|
| 245 |
|
| 246 |
+
with st.expander("Edge Cleanup", expanded=True):
|
| 247 |
st.markdown("<p style='font-size: 0.9rem; font-weight: 500; margin-bottom: 0;'>Fringe Eraser (px)</p>", unsafe_allow_html=True)
|
| 248 |
col_min, col_slide, col_plus = st.columns([1, 4, 1])
|
|
|
|
| 249 |
with col_min:
|
| 250 |
st.button("−", on_click=step_fringe, args=(-1,), use_container_width=True, key="ai_minus")
|
| 251 |
with col_slide:
|
|
|
|
| 253 |
with col_plus:
|
| 254 |
st.button("+", on_click=step_fringe, args=(1,), use_container_width=True, key="ai_plus")
|
| 255 |
|
| 256 |
+
elif method == "Chroma Key (Studio Green)":
|
| 257 |
st.markdown('<div class="sidebar-header">Keying Parameters</div>', unsafe_allow_html=True)
|
| 258 |
detected_hex = detect_background_hex(img_array)
|
| 259 |
key_color_hex = st.color_picker("Key Color", detected_hex)
|
| 260 |
+
tola = st.slider("Tolerance A (Shadows)", 1, 50, 10)
|
| 261 |
+
tolb = st.slider("Tolerance B (Highlights)", tola + 1, 120, 60)
|
| 262 |
|
| 263 |
+
with st.expander("Edge Cleanup", expanded=True):
|
| 264 |
st.markdown("<p style='font-size: 0.9rem; font-weight: 500; margin-bottom: 0;'>Fringe Eraser (px)</p>", unsafe_allow_html=True)
|
| 265 |
col_min, col_slide, col_plus = st.columns([1, 4, 1])
|
|
|
|
| 266 |
with col_min:
|
| 267 |
st.button("−", on_click=step_fringe, args=(-1,), use_container_width=True, key="chroma_minus")
|
| 268 |
with col_slide:
|
| 269 |
grow_px = st.slider("Fringe", 0, 5, key="fringe_val", label_visibility="collapsed")
|
| 270 |
with col_plus:
|
| 271 |
st.button("+", on_click=step_fringe, args=(1,), use_container_width=True, key="chroma_plus")
|
| 272 |
+
|
| 273 |
+
elif method == "Hybrid (AI + Chroma)":
|
| 274 |
+
st.markdown('<div class="sidebar-header">Hybrid Parameters</div>', unsafe_allow_html=True)
|
| 275 |
+
model_label = st.selectbox("AI Model Tier", list(AI_MODELS), index=0)
|
| 276 |
+
model_name = AI_MODELS[model_label]
|
| 277 |
+
|
| 278 |
+
detected_hex = detect_background_hex(img_array)
|
| 279 |
+
key_color_hex = st.color_picker("Key Color", detected_hex)
|
| 280 |
+
tola = st.slider("Tolerance A (Shadows)", 1, 50, 10)
|
| 281 |
+
tolb = st.slider("Tolerance B (Highlights)", tola + 1, 120, 60)
|
| 282 |
+
|
| 283 |
+
with st.expander("Edge Cleanup", expanded=True):
|
| 284 |
+
st.markdown("<p style='font-size: 0.9rem; font-weight: 500; margin-bottom: 0;'>Fringe Eraser (px)</p>", unsafe_allow_html=True)
|
| 285 |
+
col_min, col_slide, col_plus = st.columns([1, 4, 1])
|
| 286 |
+
with col_min:
|
| 287 |
+
st.button("−", on_click=step_fringe, args=(-1,), use_container_width=True, key="hyb_minus")
|
| 288 |
+
with col_slide:
|
| 289 |
+
grow_px = st.slider("Fringe", 0, 5, key="fringe_val", label_visibility="collapsed")
|
| 290 |
+
with col_plus:
|
| 291 |
+
st.button("+", on_click=step_fringe, args=(1,), use_container_width=True, key="hyb_plus")
|
| 292 |
+
|
| 293 |
|
| 294 |
# 3. Process & Render Result
|
| 295 |
col1, col2 = st.columns(2, gap="large")
|
|
|
|
| 305 |
if method == "AI Engine (Auto)":
|
| 306 |
with st.spinner(f"🤖 Running {model_label.split(' ')[0]} model..."):
|
| 307 |
extracted_image = Image.open(io.BytesIO(ai_cutout(image_bytes, model_name, grow_px)))
|
| 308 |
+
|
| 309 |
+
elif method == "Chroma Key (Studio Green)":
|
| 310 |
with st.spinner("🟩 Applying color math..."):
|
| 311 |
extracted_image = chroma_cutout(img_array, key_color_hex, tola, tolb, grow_px)
|
| 312 |
+
|
| 313 |
+
elif method == "Hybrid (AI + Chroma)":
|
| 314 |
+
with st.spinner("⚔️ Fusing AI Shape & Color Math..."):
|
| 315 |
+
# Run AI (without edge erosion yet)
|
| 316 |
+
ai_bytes = ai_cutout(image_bytes, model_name, 0)
|
| 317 |
+
ai_alpha = np.array(Image.open(io.BytesIO(ai_bytes)).split()[-1])
|
| 318 |
+
|
| 319 |
+
# Run Chroma (with spill suppression and edge erosion)
|
| 320 |
+
chroma_img = chroma_cutout(img_array, key_color_hex, tola, tolb, grow_px)
|
| 321 |
+
chroma_alpha = np.array(chroma_img.split()[-1])
|
| 322 |
+
|
| 323 |
+
# Intersect Alphas: If EITHER engine says it's background, delete it.
|
| 324 |
+
combined_alpha = np.minimum(ai_alpha, chroma_alpha)
|
| 325 |
+
|
| 326 |
+
# Apply combined alpha to the spill-suppressed Chroma RGB
|
| 327 |
+
extracted_image = chroma_img.copy()
|
| 328 |
+
extracted_image.putalpha(Image.fromarray(combined_alpha))
|
| 329 |
|
| 330 |
st.image(extracted_image, use_container_width=True)
|
| 331 |
|