Spaces:
Sleeping
Sleeping
DEWARP
Browse files
app.py
CHANGED
|
@@ -1,148 +1,76 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
from
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
#
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
if len(regions) < 3:
|
| 78 |
-
raise ValueError("Insufficient distinct regions")
|
| 79 |
-
|
| 80 |
-
features = np.array([r[1] for r in regions])
|
| 81 |
-
kmeans = KMeans(n_clusters=3, random_state=42).fit(features)
|
| 82 |
-
labels = kmeans.labels_
|
| 83 |
-
|
| 84 |
-
colors = [(0, 0, 255), (255, 0, 0), (0, 255, 0)]
|
| 85 |
-
names = ["Body", "Border", "Pallu"]
|
| 86 |
-
seg_out = np.zeros_like(image)
|
| 87 |
-
layers = [np.zeros_like(image, dtype=np.uint8) for _ in range(3)]
|
| 88 |
-
|
| 89 |
-
for i, (mask, _) in enumerate(regions):
|
| 90 |
-
seg_out[mask] = colors[labels[i]]
|
| 91 |
-
layers[labels[i]][mask] = image[mask]
|
| 92 |
-
|
| 93 |
-
seg_img = Image.fromarray(seg_out)
|
| 94 |
-
draw = ImageDraw.Draw(seg_img)
|
| 95 |
-
for (mask, _), lbl in zip(regions, labels):
|
| 96 |
-
x, y = get_centroid(mask)
|
| 97 |
-
draw.text((x, y), names[lbl], fill=(255, 255, 255))
|
| 98 |
-
|
| 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))
|
| 116 |
-
return blank, blank, blank, blank, None
|
| 117 |
-
|
| 118 |
-
# -----------------------------------------------------
|
| 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
|
| 126 |
-
- 🪞 Provide transparent PNGs
|
| 127 |
-
- 📦 Download all masks as a single ZIP
|
| 128 |
-
|
| 129 |
-
Built for saree recoloring, catalog automation, and AI draping pipelines.
|
| 130 |
-
"""
|
| 131 |
-
|
| 132 |
-
demo = gr.Interface(
|
| 133 |
-
fn=segment_saree,
|
| 134 |
-
inputs=gr.Image(type="pil", label="Upload Saree Image"),
|
| 135 |
-
outputs=[
|
| 136 |
-
gr.Image(type="pil", label="Overlay Mask with Labels"),
|
| 137 |
-
gr.Image(type="pil", label="Body (Transparent)"),
|
| 138 |
-
gr.Image(type="pil", label="Border (Transparent)"),
|
| 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__":
|
| 148 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
def flatten_image(img, points):
|
| 7 |
+
"""
|
| 8 |
+
img: PIL.Image
|
| 9 |
+
points: list of (x, y) tuples in order [TL, TR, BR, BL]
|
| 10 |
+
"""
|
| 11 |
+
if img is None or not points or len(points) != 4:
|
| 12 |
+
return None, "Please click exactly 4 points (TL, TR, BR, BL)."
|
| 13 |
+
|
| 14 |
+
# Convert to numpy array
|
| 15 |
+
image_np = np.array(img)
|
| 16 |
+
h, w = image_np.shape[:2]
|
| 17 |
+
|
| 18 |
+
# Convert input points to float32 numpy array
|
| 19 |
+
src_pts = np.array(points, dtype=np.float32)
|
| 20 |
+
|
| 21 |
+
# Compute output rectangle size using distances
|
| 22 |
+
width_top = np.linalg.norm(src_pts[0] - src_pts[1])
|
| 23 |
+
width_bottom = np.linalg.norm(src_pts[3] - src_pts[2])
|
| 24 |
+
height_left = np.linalg.norm(src_pts[0] - src_pts[3])
|
| 25 |
+
height_right = np.linalg.norm(src_pts[1] - src_pts[2])
|
| 26 |
+
|
| 27 |
+
max_width = int(max(width_top, width_bottom))
|
| 28 |
+
max_height = int(max(height_left, height_right))
|
| 29 |
+
|
| 30 |
+
dst_pts = np.array([
|
| 31 |
+
[0, 0],
|
| 32 |
+
[max_width - 1, 0],
|
| 33 |
+
[max_width - 1, max_height - 1],
|
| 34 |
+
[0, max_height - 1]
|
| 35 |
+
], dtype=np.float32)
|
| 36 |
+
|
| 37 |
+
# Compute homography
|
| 38 |
+
M = cv2.getPerspectiveTransform(src_pts, dst_pts)
|
| 39 |
+
|
| 40 |
+
# Apply perspective warp
|
| 41 |
+
warped = cv2.warpPerspective(image_np, M, (max_width, max_height), flags=cv2.INTER_CUBIC)
|
| 42 |
+
|
| 43 |
+
warped_pil = Image.fromarray(warped)
|
| 44 |
+
return warped_pil, None
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
with gr.Blocks() as demo:
|
| 48 |
+
gr.Markdown("## 📸 Perspective Flatten Tool\nUpload an image, click 4 corners (Top-Left → Top-Right → Bottom-Right → Bottom-Left), then flatten!")
|
| 49 |
+
|
| 50 |
+
with gr.Row():
|
| 51 |
+
input_image = gr.Image(label="Upload Image", tool="select", type="pil")
|
| 52 |
+
output_image = gr.Image(label="Flattened Output")
|
| 53 |
+
|
| 54 |
+
coords = gr.State([])
|
| 55 |
+
|
| 56 |
+
def collect_points(evt: gr.SelectData, points):
|
| 57 |
+
if points is None:
|
| 58 |
+
points = []
|
| 59 |
+
points.append(evt.index) # evt.index returns (x, y)
|
| 60 |
+
if len(points) > 4:
|
| 61 |
+
points = points[-4:] # keep only last 4
|
| 62 |
+
return points, f"Selected {len(points)}/4 points: {points}"
|
| 63 |
+
|
| 64 |
+
points_output = gr.Textbox(label="Selected Points", interactive=False)
|
| 65 |
+
|
| 66 |
+
input_image.select(fn=collect_points, inputs=coords, outputs=[coords, points_output])
|
| 67 |
+
|
| 68 |
+
flatten_btn = gr.Button("🔄 Flatten Image")
|
| 69 |
+
|
| 70 |
+
error_box = gr.Textbox(label="Messages", interactive=False)
|
| 71 |
+
|
| 72 |
+
flatten_btn.click(fn=flatten_image, inputs=[input_image, coords], outputs=[output_image, error_box])
|
| 73 |
+
|
| 74 |
+
gr.Markdown("Tip: Re-upload image to reset point selection.")
|
| 75 |
+
|
| 76 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|