Spaces:
Sleeping
Sleeping
UPDATE
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ from segment_anything import SamAutomaticMaskGenerator, sam_model_registry
|
|
| 3 |
from transformers import AutoImageProcessor, AutoModel
|
| 4 |
from huggingface_hub import snapshot_download
|
| 5 |
from PIL import Image, ImageDraw
|
| 6 |
-
import torch, numpy as np, cv2, zipfile, io, os
|
| 7 |
from sklearn.cluster import KMeans
|
| 8 |
|
| 9 |
# -----------------------------------------------------
|
|
@@ -99,16 +99,17 @@ def segment_saree(image):
|
|
| 99 |
# Transparent layers
|
| 100 |
transparent_imgs = [Image.fromarray(make_transparent(l, l.any(axis=2))) for l in layers]
|
| 101 |
|
| 102 |
-
# ZIP
|
| 103 |
-
|
| 104 |
-
|
|
|
|
| 105 |
for n, t in zip(names, transparent_imgs):
|
| 106 |
-
|
| 107 |
-
t.save(
|
| 108 |
-
zf.
|
| 109 |
-
|
|
|
|
| 110 |
|
| 111 |
-
return seg_img, transparent_imgs[0], transparent_imgs[1], transparent_imgs[2], zip_buffer
|
| 112 |
except Exception as e:
|
| 113 |
print("Error:", e)
|
| 114 |
blank = Image.new("RGB", (512, 512), color=(30, 30, 30))
|
|
@@ -118,7 +119,7 @@ def segment_saree(image):
|
|
| 118 |
# 4️⃣ Gradio UI
|
| 119 |
# -----------------------------------------------------
|
| 120 |
description = """
|
| 121 |
-
###
|
| 122 |
Upload a **flat or draped saree image**, and this tool will:
|
| 123 |
- ✂️ Remove background
|
| 124 |
- 🧠 Segment into **Body**, **Border**, **Pallu** using SAM + DINOv2
|
|
@@ -138,9 +139,9 @@ demo = gr.Interface(
|
|
| 138 |
gr.Image(type="pil", label="Pallu (Transparent)"),
|
| 139 |
gr.File(label="📦 Download All (ZIP)"),
|
| 140 |
],
|
| 141 |
-
title="
|
| 142 |
description=description,
|
| 143 |
-
|
| 144 |
)
|
| 145 |
|
| 146 |
if __name__ == "__main__":
|
|
|
|
| 3 |
from transformers import AutoImageProcessor, AutoModel
|
| 4 |
from huggingface_hub import snapshot_download
|
| 5 |
from PIL import Image, ImageDraw
|
| 6 |
+
import torch, numpy as np, cv2, zipfile, io, os, tempfile
|
| 7 |
from sklearn.cluster import KMeans
|
| 8 |
|
| 9 |
# -----------------------------------------------------
|
|
|
|
| 99 |
# Transparent layers
|
| 100 |
transparent_imgs = [Image.fromarray(make_transparent(l, l.any(axis=2))) for l in layers]
|
| 101 |
|
| 102 |
+
# Write ZIP to a temp file (Gradio expects a real path)
|
| 103 |
+
tmpdir = tempfile.mkdtemp()
|
| 104 |
+
zip_path = os.path.join(tmpdir, "saree_layers.zip")
|
| 105 |
+
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
|
| 106 |
for n, t in zip(names, transparent_imgs):
|
| 107 |
+
tmp_img = os.path.join(tmpdir, f"{n}.png")
|
| 108 |
+
t.save(tmp_img)
|
| 109 |
+
zf.write(tmp_img, arcname=f"{n}.png")
|
| 110 |
+
|
| 111 |
+
return seg_img, transparent_imgs[0], transparent_imgs[1], transparent_imgs[2], zip_path
|
| 112 |
|
|
|
|
| 113 |
except Exception as e:
|
| 114 |
print("Error:", e)
|
| 115 |
blank = Image.new("RGB", (512, 512), color=(30, 30, 30))
|
|
|
|
| 119 |
# 4️⃣ Gradio UI
|
| 120 |
# -----------------------------------------------------
|
| 121 |
description = """
|
| 122 |
+
### 🧶 Saree AI — Intelligent Segmentation & Layer Export
|
| 123 |
Upload a **flat or draped saree image**, and this tool will:
|
| 124 |
- ✂️ Remove background
|
| 125 |
- 🧠 Segment into **Body**, **Border**, **Pallu** using SAM + DINOv2
|
|
|
|
| 139 |
gr.Image(type="pil", label="Pallu (Transparent)"),
|
| 140 |
gr.File(label="📦 Download All (ZIP)"),
|
| 141 |
],
|
| 142 |
+
title="🧵 Saree AI — SAM + DINOv2 Smart Segmentation",
|
| 143 |
description=description,
|
| 144 |
+
flagging_mode="never",
|
| 145 |
)
|
| 146 |
|
| 147 |
if __name__ == "__main__":
|