nishanth-saka commited on
Commit
df17c15
·
verified ·
1 Parent(s): 030909f
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2, numpy as np
3
+ from PIL import Image
4
+
5
+ # -------------------------------------------------
6
+ # 🔧 CORE FUNCTION
7
+ # -------------------------------------------------
8
+ def remove_folds(image, intensity=0.5):
9
+ if image is None:
10
+ return None
11
+
12
+ img = np.array(image.convert("RGB"))
13
+ img_lab = cv2.cvtColor(img, cv2.COLOR_RGB2LAB)
14
+ l, a, b = cv2.split(img_lab)
15
+
16
+ # --- (1) Smooth illumination correction ---
17
+ ksize = int(49 + intensity * 100) # kernel size changes with intensity
18
+ ksize = ksize + 1 if ksize % 2 == 0 else ksize # must be odd
19
+ l_blur = cv2.GaussianBlur(l, (ksize, ksize), 0)
20
+ l_equal = cv2.divide(l, l_blur, scale=128)
21
+
22
+ # --- (2) Frequency-domain smoothing ---
23
+ freq = np.fft.fft2(l_equal)
24
+ freqshift = np.fft.fftshift(freq)
25
+ rows, cols = l_equal.shape
26
+ crow, ccol = rows // 2, cols // 2
27
+ mask = np.ones((rows, cols), np.uint8)
28
+ r = int(10 + intensity * 30) # notch size grows with intensity
29
+ mask[crow - r:crow + r, ccol - r:ccol + r] = 0
30
+ freqshift = freqshift * mask
31
+ ishift = np.fft.ifftshift(freqshift)
32
+ l_flat = np.abs(np.fft.ifft2(ishift))
33
+ l_flat = cv2.normalize(l_flat, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8)
34
+
35
+ # --- (3) Merge back and convert ---
36
+ img_lab = cv2.merge([l_flat, a, b])
37
+ result = cv2.cvtColor(img_lab, cv2.COLOR_LAB2RGB)
38
+ return Image.fromarray(result)
39
+
40
+ # -------------------------------------------------
41
+ # 🎛️ GRADIO UI
42
+ # -------------------------------------------------
43
+ title = "🪄 Saree Fold Remover (Lighting Only, No Distortion)"
44
+ description = """
45
+ Upload a flat saree or fabric image.<br>
46
+ Adjust the **Fold Intensity** slider — higher values remove deeper folds.<br>
47
+ The process equalizes lighting only, keeping weave and motifs intact.
48
+ """
49
+
50
+ iface = gr.Interface(
51
+ fn=remove_folds,
52
+ inputs=[
53
+ gr.Image(label="Upload Saree Image", type="pil"),
54
+ gr.Slider(
55
+ 0.0,
56
+ 1.0,
57
+ value=0.5,
58
+ step=0.05,
59
+ label="Fold Intensity",
60
+ info="Higher = stronger flattening (lighting only)",
61
+ interactive=True
62
+ ),
63
+ ],
64
+ outputs=gr.Image(label="Flat, Fold-Free Output"),
65
+ title=title,
66
+ description=description,
67
+ live=False, # prevents rerun while moving
68
+ throttle=0.8 # waits 0.8 s before triggering
69
+ )
70
+
71
+ if __name__ == "__main__":
72
+ iface.launch()