nishanth-saka commited on
Commit
3577c9b
·
verified ·
1 Parent(s): 5316a76
Files changed (1) hide show
  1. app.py +13 -12
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 all outputs
103
- zip_buffer = io.BytesIO()
104
- with zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED) as zf:
 
105
  for n, t in zip(names, transparent_imgs):
106
- bio = io.BytesIO()
107
- t.save(bio, format="PNG")
108
- zf.writestr(f"{n}.png", bio.getvalue())
109
- zip_buffer.seek(0)
 
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
- ### 🧵 Saree AI — Intelligent Segmentation & Layer Export
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="🧶 Saree AI — SAM + DINOv2 Smart Segmentation",
142
  description=description,
143
- allow_flagging="never",
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__":