Enhance image processing options and UI elements
Browse filesAdded new processing method 'Hybrid (AI + Chroma)' and updated UI components for better user interaction.
15 unmodified lines161718192021222 unmodified lines2526272829303116 unmodified lines4849505152535425 unmodified lines8081828384858617 unmodified lines104105106107108109110118 unmodified lines2292302312322332342352362372382392402412422432442452462472 unmodified lines2502512522532542552562572582591 unmodified line2612622632642652662672682692702712722732742752762772782792802812822839 unmodified lines29329429529629729829930030115 unmodified lines<style>@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');/* Base Typography */html, body, [class*="css"] {font-family: 'Inter', sans-serif;}2 unmodified linesheader { visibility: hidden; }footer { visibility: hidden; }/* Layout & Headers */.app-header {margin-bottom: 2rem;padding-bottom: 1rem;16 unmodified linesmargin-bottom: 0.5rem;}/* Buttons */.stButton > button {background-color: #4F46E5 !important;color: white !important;25 unmodified linesbox-shadow: 0 4px 6px -1px rgba(16, 185, 129, 0.2) !important;}/* Image Containers & Checkerboard Background for Transparency */[data-testid="stImage"] {background-color: #ffffff;background-image:17 unmodified linesopacity: 0.8;}/* File Uploader */[data-testid="stFileUploaderDropzone"] {border-radius: 12px;border: 2px dashed rgba(128, 128, 128, 0.4);118 unmodified linesoriginal_image = Image.open(io.BytesIO(image_bytes)).convert("RGB")img_array = np.array(original_image)# 2. Sidebar renders after image is loadedwith st.sidebar:# --- ADD THIS STATE INITIALIZATION ---if "fringe_val" not in st.session_state:st.session_state.fringe_val = 0def step_fringe(delta):# Keeps the value strictly between 0 and 5st.session_state.fringe_val = max(0, min(5, st.session_state.fringe_val + delta))# -------------------------------------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")st.markdown("---")2 unmodified linesmodel_label = st.selectbox("Model Tier", list(AI_MODELS), index=0)model_name = AI_MODELS[model_label]with st.expander("Edge Cleanup"):st.markdown("<p style='font-size: 0.9rem; font-weight: 500; margin-bottom: 0;'>Fringe Eraser (px)</p>", unsafe_allow_html=True)col_min, col_slide, col_plus = st.columns([1, 4, 1])with col_min:st.button("−", on_click=step_fringe, args=(-1,), use_container_width=True, key="ai_minus")with col_slide:1 unmodified linewith col_plus:st.button("+", on_click=step_fringe, args=(1,), use_container_width=True, key="ai_plus")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%.")with st.expander("Edge Cleanup"):st.markdown("<p style='font-size: 0.9rem; font-weight: 500; margin-bottom: 0;'>Fringe Eraser (px)</p>", unsafe_allow_html=True)col_min, col_slide, col_plus = st.columns([1, 4, 1])with col_min:st.button("−", on_click=step_fringe, args=(-1,), use_container_width=True, key="chroma_minus")with col_slide:grow_px = st.slider("Fringe", 0, 5, key="fringe_val", label_visibility="collapsed")with col_plus:st.button("+", on_click=step_fringe, args=(1,), use_container_width=True, key="chroma_plus")# 3. Process & Render Resultcol1, col2 = st.columns(2, gap="large")9 unmodified linesif method == "AI Engine (Auto)":with st.spinner(f"🤖 Running {model_label.split(' ')[0]} model..."):extracted_image = Image.open(io.BytesIO(ai_cutout(image_bytes, model_name, grow_px)))else:with st.spinner("🟩 Applying color math..."):extracted_image = chroma_cutout(img_array, key_color_hex, tola, tolb, grow_px)st.image(extracted_image, use_container_width=True)15 unmodified lines1617181920212 unmodified lines24252627282916 unmodified lines46474849505125 unmodified lines77787980818217 unmodified lines100101102103104105118 unmodified lines2242252262272282292302312322332342352362372382392402 unmodified lines2432442452462472482492502511 unmodified line2532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942959 unmodified lines30530630730830931031131231331431531631731831932032132232332432532632732832933033115 unmodified lines<style>@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');html, body, [class*="css"] {font-family: 'Inter', sans-serif;}2 unmodified linesheader { visibility: hidden; }footer { visibility: hidden; }.app-header {margin-bottom: 2rem;padding-bottom: 1rem;16 unmodified linesmargin-bottom: 0.5rem;}.stButton > button {background-color: #4F46E5 !important;color: white !important;25 unmodified linesbox-shadow: 0 4px 6px -1px rgba(16, 185, 129, 0.2) !important;}[data-testid="stImage"] {background-color: #ffffff;background-image:17 unmodified linesopacity: 0.8;}[data-testid="stFileUploaderDropzone"] {border-radius: 12px;border: 2px dashed rgba(128, 128, 128, 0.4);118 unmodified linesoriginal_image = Image.open(io.BytesIO(image_bytes)).convert("RGB")img_array = np.array(original_image)with st.sidebar:# --- Session State for + / - Steppers ---if "fringe_val" not in st.session_state:st.session_state.fringe_val = 0def step_fringe(delta):st.session_state.fringe_val = max(0, min(5, st.session_state.fringe_val + delta))# ----------------------------------------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)", "Hybrid (AI + Chroma)"], label_visibility="collapsed")st.markdown("---")2 unmodified linesmodel_label = st.selectbox("Model Tier", list(AI_MODELS), index=0)model_name = AI_MODELS[model_label]with st.expander("Edge Cleanup", expanded=True):st.markdown("<p style='font-size: 0.9rem; font-weight: 500; margin-bottom: 0;'>Fringe Eraser (px)</p>", unsafe_allow_html=True)col_min, col_slide, col_plus = st.columns([1, 4, 1])with col_min:st.button("−", on_click=step_fringe, args=(-1,), use_container_width=True, key="ai_minus")with col_slide:1 unmodified linewith col_plus:st.button("+", on_click=step_fringe, args=(1,), use_container_width=True, key="ai_plus")elif method == "Chroma Key (Studio Green)":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)tolb = st.slider("Tolerance B (Highlights)", tola + 1, 120, 60)with st.expander("Edge Cleanup", expanded=True):st.markdown("<p style='font-size: 0.9rem; font-weight: 500; margin-bottom: 0;'>Fringe Eraser (px)</p>", unsafe_allow_html=True)col_min, col_slide, col_plus = st.columns([1, 4, 1])with col_min:st.button("−", on_click=step_fringe, args=(-1,), use_container_width=True, key="chroma_minus")with col_slide:grow_px = st.slider("Fringe", 0, 5, key="fringe_val", label_visibility="collapsed")with col_plus:st.button("+", on_click=step_fringe, args=(1,), use_container_width=True, key="chroma_plus")elif method == "Hybrid (AI + Chroma)":st.markdown('<div class="sidebar-header">Hybrid Parameters</div>', unsafe_allow_html=True)model_label = st.selectbox("AI Model Tier", list(AI_MODELS), index=0)model_name = AI_MODELS[model_label]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)tolb = st.slider("Tolerance B (Highlights)", tola + 1, 120, 60)with st.expander("Edge Cleanup", expanded=True):st.markdown("<p style='font-size: 0.9rem; font-weight: 500; margin-bottom: 0;'>Fringe Eraser (px)</p>", unsafe_allow_html=True)col_min, col_slide, col_plus = st.columns([1, 4, 1])with col_min:st.button("−", on_click=step_fringe, args=(-1,), use_container_width=True, key="hyb_minus")with col_slide:grow_px = st.slider("Fringe", 0, 5, key="fringe_val", label_visibility="collapsed")with col_plus:st.button("+", on_click=step_fringe, args=(1,), use_container_width=True, key="hyb_plus")# 3. Process & Render Resultcol1, col2 = st.columns(2, gap="large")9 unmodified linesif method == "AI Engine (Auto)":with st.spinner(f"🤖 Running {model_label.split(' ')[0]} model..."):extracted_image = Image.open(io.BytesIO(ai_cutout(image_bytes, model_name, grow_px)))elif method == "Chroma Key (Studio Green)":with st.spinner("🟩 Applying color math..."):extracted_image = chroma_cutout(img_array, key_color_hex, tola, tolb, grow_px)elif method == "Hybrid (AI + Chroma)":with st.spinner("⚔️ Fusing AI Shape & Color Math..."):# Run AI (without edge erosion yet)ai_bytes = ai_cutout(image_bytes, model_name, 0)ai_alpha = np.array(Image.open(io.BytesIO(ai_bytes)).split()[-1])# Run Chroma (with spill suppression and edge erosion)chroma_img = chroma_cutout(img_array, key_color_hex, tola, tolb, grow_px)chroma_alpha = np.array(chroma_img.split()[-1])# Intersect Alphas: If EITHER engine says it's background, delete it.combined_alpha = np.minimum(ai_alpha, chroma_alpha)# Apply combined alpha to the spill-suppressed Chroma RGBextracted_image = chroma_img.copy()extracted_image.putalpha(Image.fromarray(combined_alpha))st.image(extracted_image, use_container_width=True)
app.py+48-18