Replace Hugging Face API with local Segformer model
Browse filesRefactor Segformer API logic to use local PyTorch model and update styling.
12345625 unmodified lines32333435363738394041428 unmodified lines5152535455565758596019 unmodified lines808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015125 unmodified lines17717817918018118218318418518624 unmodified lines21121221321421521621711 unmodified lines22923023123223323423523623723844 unmodified lines28328428528628728828929029129229329439 unmodified lines334335336337338339340341342343344import ioimport base64import requestsimport cv2import numpy as np25 unmodified linesheader { visibility: hidden; }footer { visibility: hidden; }.app-header {margin-bottom: 2rem;padding-bottom: 1rem;border-bottom: 1px solid rgba(128, 128, 128, 0.2);}.app-title { font-weight: 700; font-size: 2.25rem; margin-bottom: 0.25rem; }.app-subtitle { font-size: 1rem; opacity: 0.7; }.sidebar-header { font-weight: 600; font-size: 1.1rem; margin-top: 1rem; margin-bottom: 0.5rem; }8 unmodified linestransition: all 0.2s ease !important;width: 100%;}.stButton > button:hover {background-color: #4338CA !important;box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1) !important;}.stDownloadButton > button {background-color: #10B981 !important;19 unmodified linesoverflow: hidden;border: 1px solid rgba(128, 128, 128, 0.2);}.image-card-title {font-weight: 600;font-size: 0.9rem;margin-bottom: 0.75rem;text-transform: uppercase;letter-spacing: 0.05em;opacity: 0.8;}[data-testid="stFileUploaderDropzone"] {border-radius: 12px;border: 2px dashed rgba(128, 128, 128, 0.4);}</style>""",unsafe_allow_html=True,)# ---------------------------------------------------------------------------# Segformer API Logic# ---------------------------------------------------------------------------def hf_segformer_cutout(image_bytes: bytes, hf_token: str, grow_px: int) -> bytes:from huggingface_hub import InferenceClientif not hf_token:raise ValueError("Hugging Face API Token is missing.")# Initialize official client routed to the Segformer clothing modelclient = InferenceClient(model="mattmdjaga/segformer_b2_clothes", token=hf_token)# Run serverless image segmentation via the official SDKresults = client.image_segmentation(image=image_bytes)source_img = Image.open(io.BytesIO(image_bytes)).convert("RGB")target_labels = ["Upper-clothes", "Pants", "Skirt", "Dress", "Coat"]combined_mask = Nonefor item in results:# Check standard label structures returned by InferenceClientlabel = item.get("label", "")if label in target_labels:mask_img = item.get("mask").convert("L")mask_img = mask_img.resize(source_img.size, Image.LANCZOS)if combined_mask is None:combined_mask = np.array(mask_img)else:combined_mask = np.maximum(combined_mask, np.array(mask_img))if combined_mask is None:raise ValueError("No garments detected by Segformer.")if grow_px > 0:kernel = np.ones((3, 3), np.uint8)combined_mask = cv2.erode(combined_mask, kernel, iterations=grow_px)mask_final = Image.fromarray(combined_mask)result = source_img.convert("RGBA")result.putalpha(mask_final)buf = io.BytesIO()result.save(buf, format="PNG")return buf.getvalue()# ---------------------------------------------------------------------------# Local AI Cutout Logic# ---------------------------------------------------------------------------AI_MODELS = {"Clothes ONLY (Deletes Skin & Hair)": "u2net_cloth_seg",25 unmodified linesif model_name == "u2net_cloth_seg":masks = remove(small, session=load_ai_session(model_name), only_mask=True, return_multiple=True)if isinstance(masks, list) and len(masks) >= 3:# Corrected Mapping based on visual artifacts:# 0 = Face/Hair, 1 = Lower Body, 2 = Upper Bodylower = np.array(masks[1].convert("L"))upper = np.array(masks[2].convert("L"))clothes_only = np.maximum(upper, lower)24 unmodified lines# ---------------------------------------------------------------------------# Chroma Key Logic# ---------------------------------------------------------------------------def detect_background_hex(img_array: np.ndarray, border: int = 10) -> str:h, w, _ = img_array.shape11 unmodified lineshex_str = key_hex.lstrip('#')target_color = np.array([int(hex_str[i:i+2], 16) for i in (0, 2, 4)], dtype=np.float32)distances = np.linalg.norm(img_array.astype(np.float32) - target_color, axis=-1)mask = 1.0 - (distances - tola) / max(tolb - tola, 1)mask = np.clip(mask, 0.0, 1.0)alpha_f = 1.0 - maskfinal_rgb = img_array.copy()44 unmodified linesst.markdown("---")if method == "Segformer (Pro Garment AI)":st.info("🚀 **State-of-the-Art Clothing AI.** Uses an advanced transformer API to map 18 garment classes while ignoring skin and backgrounds.")st.markdown('<div class="sidebar-header">API Configuration</div>', unsafe_allow_html=True)hf_token = st.text_input("Hugging Face API Token", type="password", help="Get a free API token from your huggingface.co account settings.")elif method == "Local AI (Basic & Full Subject)":st.info("💡 **Local Processing.** Extracts the general silhouette without an internet connection. Struggles with specific garments.")st.markdown('<div class="sidebar-header">AI Parameters</div>', unsafe_allow_html=True)model_label = st.selectbox("Model Tier", list(AI_MODELS), index=0, key="ai_model_sel")model_name = AI_MODELS[model_label]39 unmodified linestry:if method == "Segformer (Pro Garment AI)":if not hf_token:st.warning("A Hugging Face API Token is required for this engine.")st.stop()with st.spinner("🧠 Querying Segformer Transformer..."):extracted_image = Image.open(io.BytesIO(hf_segformer_cutout(image_bytes, hf_token, grow_px)))elif method == "Local AI (Basic & Full Subject)":with st.spinner(f"🤖 Running {model_label.split(' ')[0]} model..."):123425 unmodified lines303132333435368 unmodified lines4546474849505119 unmodified lines717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413525 unmodified lines16116216316416516616724 unmodified lines19219319419519619719811 unmodified lines21021121221321421521621744 unmodified lines26226326426526626726826927027139 unmodified lines311312313314315316317318import ioimport cv2import numpy as np25 unmodified linesheader { visibility: hidden; }footer { visibility: hidden; }.app-header { margin-bottom: 2rem; padding-bottom: 1rem; border-bottom: 1px solid rgba(128, 128, 128, 0.2); }.app-title { font-weight: 700; font-size: 2.25rem; margin-bottom: 0.25rem; }.app-subtitle { font-size: 1rem; opacity: 0.7; }.sidebar-header { font-weight: 600; font-size: 1.1rem; margin-top: 1rem; margin-bottom: 0.5rem; }8 unmodified linestransition: all 0.2s ease !important;width: 100%;}.stButton > button:hover { background-color: #4338CA !important; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1) !important; }.stDownloadButton > button {background-color: #10B981 !important;19 unmodified linesoverflow: hidden;border: 1px solid rgba(128, 128, 128, 0.2);}.image-card-title { font-weight: 600; font-size: 0.9rem; margin-bottom: 0.75rem; text-transform: uppercase; opacity: 0.8; }[data-testid="stFileUploaderDropzone"] { border-radius: 12px; border: 2px dashed rgba(128, 128, 128, 0.4); }</style>""",unsafe_allow_html=True,)# ---------------------------------------------------------------------------# Segformer Local PyTorch Engine# ---------------------------------------------------------------------------@st.cache_resource(show_spinner=False, max_entries=1)def load_segformer():from transformers import SegformerImageProcessor, AutoModelForSemanticSegmentationprocessor = SegformerImageProcessor.from_pretrained("mattmdjaga/segformer_b2_clothes")model = AutoModelForSemanticSegmentation.from_pretrained("mattmdjaga/segformer_b2_clothes")return processor, model@st.cache_data(show_spinner=False, max_entries=2)def local_segformer_cutout(image_bytes: bytes, grow_px: int) -> bytes:import torchimport torch.nn as nnprocessor, model = load_segformer()source_img = Image.open(io.BytesIO(image_bytes)).convert("RGB")# Process image natively via PyTorch tensorsinputs = processor(images=source_img, return_tensors="pt")with torch.no_grad():outputs = model(**inputs)logits = outputs.logits.cpu()upsampled_logits = nn.functional.interpolate(logits,size=source_img.size[::-1], # (height, width)mode="bilinear",align_corners=False,)# Convert logits to categorical integer mapspred_seg = upsampled_logits.argmax(dim=1)[0].numpy()# Model Classes: 4: Upper-clothes, 5: Skirt, 6: Pants, 7: Dresstarget_labels = [4, 5, 6, 7]mask_arr = np.isin(pred_seg, target_labels).astype(np.uint8) * 255if grow_px > 0:kernel = np.ones((3, 3), np.uint8)mask_arr = cv2.erode(mask_arr, kernel, iterations=grow_px)mask = Image.fromarray(mask_arr, mode="L")result = source_img.convert("RGBA")result.putalpha(mask)buf = io.BytesIO()result.save(buf, format="PNG")return buf.getvalue()# ---------------------------------------------------------------------------# Local AI (rembg) Engine# ---------------------------------------------------------------------------AI_MODELS = {"Clothes ONLY (Deletes Skin & Hair)": "u2net_cloth_seg",25 unmodified linesif model_name == "u2net_cloth_seg":masks = remove(small, session=load_ai_session(model_name), only_mask=True, return_multiple=True)if isinstance(masks, list) and len(masks) >= 3:lower = np.array(masks[1].convert("L"))upper = np.array(masks[2].convert("L"))clothes_only = np.maximum(upper, lower)24 unmodified lines# ---------------------------------------------------------------------------# Chroma Key Engine# ---------------------------------------------------------------------------def detect_background_hex(img_array: np.ndarray, border: int = 10) -> str:h, w, _ = img_array.shape11 unmodified lineshex_str = key_hex.lstrip('#')target_color = np.array([int(hex_str[i:i+2], 16) for i in (0, 2, 4)], dtype=np.float32)distances = np.linalg.norm(img_array.astype(np.float32) - target_color, axis=-1)mask = 1.0 - (distances - tola) / max(tolb - tola, 1)mask = np.clip(mask, 0.0, 1.0)alpha_f = 1.0 - maskfinal_rgb = img_array.copy()44 unmodified linesst.markdown("---")if method == "Segformer (Pro Garment AI)":st.info("🚀 **State-of-the-Art Clothing AI.** Uses an advanced PyTorch transformer to accurately map 18 garment classes locally while strictly ignoring skin and backgrounds.")elif method == "Local AI (Basic & Full Subject)":st.info("💡 **Basic Model.** Extracts the general silhouette perfectly. Struggles with internal specific garments.")st.markdown('<div class="sidebar-header">AI Parameters</div>', unsafe_allow_html=True)model_label = st.selectbox("Model Tier", list(AI_MODELS), index=0, key="ai_model_sel")model_name = AI_MODELS[model_label]39 unmodified linestry:if method == "Segformer (Pro Garment AI)":with st.spinner("🧠 Downloading & running Segformer (Local PyTorch)..."):extracted_image = Image.open(io.BytesIO(local_segformer_cutout(image_bytes, grow_px)))elif method == "Local AI (Basic & Full Subject)":with st.spinner(f"🤖 Running {model_label.split(' ')[0]} model..."):
app.py+45-71