Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from utils.preprocessing import ImageProcessor
|
3 |
+
|
4 |
+
# Initialize processor (model path relative to repo)
|
5 |
+
processor = ImageProcessor("models/best.pt")
|
6 |
+
|
7 |
+
def process_image(input_image):
|
8 |
+
if input_image is None:
|
9 |
+
raise gr.Error("Please upload an image first!")
|
10 |
+
|
11 |
+
# Convert Gradio Image to bytes
|
12 |
+
_, img_bytes = cv2.imencode(".png", input_image)
|
13 |
+
|
14 |
+
# Process image
|
15 |
+
results = processor.process_image(img_bytes.tobytes())
|
16 |
+
|
17 |
+
# Format outputs
|
18 |
+
return {
|
19 |
+
class_name: (mask * 255).astype(np.uint8)
|
20 |
+
for class_name, mask in results.items()
|
21 |
+
}
|
22 |
+
|
23 |
+
# Gradio interface with enhanced UX
|
24 |
+
with gr.Blocks(title="Fashion Segmenter") as demo:
|
25 |
+
gr.Markdown("# 🧥 Fashion Item Segmenter")
|
26 |
+
|
27 |
+
with gr.Row():
|
28 |
+
input_image = gr.Image(label="Upload Clothing Image", type="numpy")
|
29 |
+
output_gallery = gr.Gallery(label="Segmented Items", columns=2)
|
30 |
+
|
31 |
+
with gr.Row():
|
32 |
+
run_btn = gr.Button("Process Image", variant="primary")
|
33 |
+
examples = gr.Examples(
|
34 |
+
examples=["sample1.jpg", "sample2.jpg"],
|
35 |
+
inputs=[input_image],
|
36 |
+
label="Example Images"
|
37 |
+
)
|
38 |
+
|
39 |
+
run_btn.click(
|
40 |
+
fn=process_image,
|
41 |
+
inputs=[input_image],
|
42 |
+
outputs=[output_gallery],
|
43 |
+
show_progress=True
|
44 |
+
)
|
45 |
+
|
46 |
+
if __name__ == "__main__":
|
47 |
+
demo.launch()
|