Spaces:
Sleeping
Sleeping
PATTERN
Browse files
app.py
CHANGED
|
@@ -1,66 +1,95 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
| 3 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 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 |
-
# ποΈ Gradio UI
|
| 44 |
-
# -------------------------------------------------
|
| 45 |
-
with gr.Blocks(title="πͺ Saree Fold Remover β Color-Safe LAB Edition") as demo:
|
| 46 |
-
gr.Markdown("""
|
| 47 |
-
## πͺ Saree Fold Remover β *Color-Safe Edition*
|
| 48 |
-
Removes folds & lighting shadows **without changing original colors**.<br>
|
| 49 |
-
Works beautifully on silk, cotton and zari fabrics.<br>
|
| 50 |
-
Adjust *Fold Intensity* for stronger corrections; enable *Enhance Texture* for sharper weave.
|
| 51 |
-
""")
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
info="Higher = stronger flattening"
|
| 59 |
-
)
|
| 60 |
-
enhance = gr.Checkbox(label="β¨ Enhance Texture", value=False)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
|
|
|
|
|
| 1 |
+
# ==========================================================
|
| 2 |
+
# πͺ Saree Pattern Extractor (DINOv2 / ViT Patch Clustering)
|
| 3 |
+
# Optimized for HF Free CPU Tier
|
| 4 |
+
# ==========================================================
|
| 5 |
+
import os, zipfile, io, cv2, numpy as np, torch
|
| 6 |
from PIL import Image
|
| 7 |
+
from sklearn.cluster import KMeans
|
| 8 |
+
import gradio as gr
|
| 9 |
+
from transformers import AutoImageProcessor, AutoModel
|
| 10 |
+
import matplotlib.pyplot as plt
|
| 11 |
|
| 12 |
+
# -----------------------------
|
| 13 |
+
# 1οΈβ£ Load DINOv2-small model
|
| 14 |
+
# -----------------------------
|
| 15 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 16 |
+
model_id = "facebook/dinov2-small"
|
| 17 |
+
processor = AutoImageProcessor.from_pretrained(model_id)
|
| 18 |
+
model = AutoModel.from_pretrained(model_id).to(device).eval()
|
| 19 |
|
| 20 |
+
# -----------------------------
|
| 21 |
+
# 2οΈβ£ Helper functions
|
| 22 |
+
# -----------------------------
|
| 23 |
+
def upscale_label_map(lbl, target_size):
|
| 24 |
+
h, w = lbl.shape
|
| 25 |
+
return cv2.resize(lbl.astype(np.uint8), target_size, interpolation=cv2.INTER_NEAREST)
|
| 26 |
|
| 27 |
+
def extract_patterns(image: Image.Image, K: int = 8):
|
| 28 |
+
# Resize to keep under memory limits
|
| 29 |
+
image = image.convert("RGB")
|
| 30 |
+
img_small = image.copy()
|
| 31 |
+
img_small.thumbnail((480,480))
|
| 32 |
|
| 33 |
+
inputs = processor(images=img_small, return_tensors="pt").to(device)
|
| 34 |
+
with torch.no_grad():
|
| 35 |
+
outputs = model(**inputs, output_hidden_states=True)
|
| 36 |
+
feats = outputs.last_hidden_state.squeeze(0)[1:].cpu().numpy()
|
| 37 |
|
| 38 |
+
grid = int(np.sqrt(feats.shape[0]))
|
| 39 |
+
km = KMeans(n_clusters=K, random_state=0, n_init="auto").fit(feats)
|
| 40 |
+
labels = km.labels_.reshape(grid, grid)
|
| 41 |
|
| 42 |
+
# Rebuild overlay
|
| 43 |
+
lbl_map = upscale_label_map(labels, img_small.size)
|
| 44 |
+
colors = plt.cm.tab10(np.linspace(0,1,K))[:,:3]
|
| 45 |
+
overlay = np.zeros((*lbl_map.shape,3))
|
| 46 |
+
for k in range(K):
|
| 47 |
+
overlay[lbl_map==k] = colors[k]
|
| 48 |
+
overlay = (overlay*255).astype(np.uint8)
|
| 49 |
+
blend = cv2.addWeighted(np.array(img_small), 0.6, overlay, 0.4, 0)
|
| 50 |
|
| 51 |
+
# Patch export
|
| 52 |
+
outdir = "patterns"
|
| 53 |
+
os.makedirs(outdir, exist_ok=True)
|
| 54 |
+
ph, pw = np.array(img_small.size)//grid
|
| 55 |
+
for k in range(K):
|
| 56 |
+
mask = (labels==k)
|
| 57 |
+
coords = np.argwhere(mask)
|
| 58 |
+
for i,(y,x) in enumerate(coords):
|
| 59 |
+
y0,y1 = int(y*ph), int((y+1)*ph)
|
| 60 |
+
x0,x1 = int(x*pw), int((x+1)*pw)
|
| 61 |
+
patch = img_small.crop((x0,y0,x1,y1))
|
| 62 |
+
patch.save(f"{outdir}/cluster{k}_patch{i}.png")
|
| 63 |
|
| 64 |
+
# Zip patches
|
| 65 |
+
zip_path = "patterns.zip"
|
| 66 |
+
with zipfile.ZipFile(zip_path,"w",zipfile.ZIP_DEFLATED) as zf:
|
| 67 |
+
for fn in os.listdir(outdir):
|
| 68 |
+
zf.write(os.path.join(outdir,fn), fn)
|
| 69 |
|
| 70 |
+
return Image.fromarray(blend), zip_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
# -----------------------------
|
| 73 |
+
# 3οΈβ£ Gradio Interface
|
| 74 |
+
# -----------------------------
|
| 75 |
+
title = "πͺ Saree Pattern Extractor (DINOv2)"
|
| 76 |
+
desc = "Upload a saree image β cluster similar motifs & textures β preview overlay β download all patch crops as ZIP."
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
demo = gr.Interface(
|
| 79 |
+
fn=extract_patterns,
|
| 80 |
+
inputs=[
|
| 81 |
+
gr.Image(label="Upload Saree Image", type="pil"),
|
| 82 |
+
gr.Slider(3,12,value=8,step=1,label="Number of Pattern Clusters")
|
| 83 |
+
],
|
| 84 |
+
outputs=[
|
| 85 |
+
gr.Image(label="Cluster Overlay"),
|
| 86 |
+
gr.File(label="Download ZIP of Crops")
|
| 87 |
+
],
|
| 88 |
+
title=title,
|
| 89 |
+
description=desc,
|
| 90 |
+
allow_flagging="never",
|
| 91 |
+
cache_examples=False
|
| 92 |
+
)
|
| 93 |
|
| 94 |
+
if __name__ == "__main__":
|
| 95 |
+
demo.launch()
|