Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from PIL import Image
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
# Load U-2-Net segmentation pipeline
|
7 |
+
pipe = pipeline("image-segmentation", model="BritishWerewolf/U-2-Net")
|
8 |
+
|
9 |
+
# Segmentation function for Gradio
|
10 |
+
def segment_dress(image: Image.Image):
|
11 |
+
# Run U-2-Net pipeline
|
12 |
+
segments = pipe(image)
|
13 |
+
|
14 |
+
# We'll assume the first segment is the foreground (person+clothes)
|
15 |
+
if not segments:
|
16 |
+
return image
|
17 |
+
|
18 |
+
# Load and apply mask
|
19 |
+
mask = Image.open(segments[0]["mask"]).convert("L").resize(image.size)
|
20 |
+
mask_np = np.array(mask) / 255.0
|
21 |
+
image_np = np.array(image).astype(np.uint8)
|
22 |
+
|
23 |
+
# Apply mask to image (keep only masked region)
|
24 |
+
segmented = (image_np * mask_np[..., None]).astype(np.uint8)
|
25 |
+
segmented_img = Image.fromarray(segmented)
|
26 |
+
|
27 |
+
return segmented_img
|
28 |
+
|
29 |
+
# Gradio Interface
|
30 |
+
gr.Interface(
|
31 |
+
fn=segment_dress,
|
32 |
+
inputs=gr.Image(type="pil", label="Upload Image"),
|
33 |
+
outputs=gr.Image(type="pil", label="Segmented Dress Region"),
|
34 |
+
title="Dress Segmentation using U-2-Net",
|
35 |
+
description="Upload an image. The U-2-Net model will segment the main foreground (usually a person wearing a dress)."
|
36 |
+
).launch()
|