Files changed (1) hide show
  1. app.py +29 -47
app.py CHANGED
@@ -3,67 +3,49 @@ 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)
18
- ksize = ksize + 1 if ksize % 2 == 0 else ksize
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)
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 BLOCKS UI (with slider throttle)
42
  # -------------------------------------------------
43
- with gr.Blocks(title="πŸͺ„ Saree Fold Remover (Lighting Only, No Distortion)") as demo:
44
- gr.Markdown(
45
- """
46
- ## πŸͺ„ Saree Fold Remover
47
- Upload a flat saree or fabric image.<br>
48
- Adjust the **Fold Intensity** slider β€” higher values remove deeper folds.<br>
49
- Lighting only correction, no geometric distortion.
50
- """
51
- )
52
 
53
  with gr.Row():
54
  inp = gr.Image(label="Upload Saree Image", type="pil")
55
- intensity = gr.Slider(
56
- 0.0, 1.0, value=0.5, step=0.05,
57
- label="Fold Intensity",
58
- info="Higher = stronger flattening (lighting only)"
59
- )
60
 
61
  out = gr.Image(label="Flat, Fold-Free Output")
62
-
63
- # Button to trigger with throttle behavior
64
- run_btn = gr.Button("✨ Remove Folds")
65
-
66
- # Run when button is pressed (instead of live refresh)
67
- run_btn.click(fn=remove_folds, inputs=[inp, intensity], outputs=out)
68
 
69
  demo.launch()
 
3
  from PIL import Image
4
 
5
  # -------------------------------------------------
6
+ # Core Fold Removal Function
7
  # -------------------------------------------------
8
+ def remove_folds_color_preserve(image, intensity=0.5, enhance=False):
9
  if image is None:
10
  return None
11
 
12
+ img = np.array(image.convert("RGB"), dtype=np.float32) / 255.0
 
 
13
 
14
+ # --- (1) Estimate illumination via large guided blur ---
15
+ radius = int(80 + 120 * intensity)
16
+ illum = cv2.GaussianBlur(img, (radius|1, radius|1), 0)
 
 
17
 
18
+ # --- (2) Divide reflectance by illumination (Retinex-like) ---
19
+ norm = np.clip(img / (illum + 1e-5), 0, 4)
20
+ norm = norm / norm.max()
 
 
 
 
 
 
 
 
 
21
 
22
+ # --- (3) Edge-aware enhancement to restore motifs ---
23
+ out = cv2.detailEnhance((norm * 255).astype(np.uint8), sigma_s=20, sigma_r=0.2)
24
+
25
+ # --- (4) Optional weave enhancement ---
26
+ if enhance:
27
+ blur = cv2.GaussianBlur(out, (0, 0), 3)
28
+ out = cv2.addWeighted(out, 1.5, blur, -0.5, 0)
29
+
30
+ return Image.fromarray(out)
31
 
32
  # -------------------------------------------------
33
+ # Gradio Blocks UI
34
  # -------------------------------------------------
35
+ with gr.Blocks(title="πŸͺ„ Saree Fold Remover β€” Color-Preserving Edition") as demo:
36
+ gr.Markdown("""
37
+ ## πŸͺ„ Saree Fold Remover (Color-Preserving)
38
+ Removes lighting folds & shadows **without color shifts or distortion**.<br>
39
+ Use the slider to control strength, and enable *Enhance Texture* for fine weave sharpening.
40
+ """)
 
 
 
41
 
42
  with gr.Row():
43
  inp = gr.Image(label="Upload Saree Image", type="pil")
44
+ intensity = gr.Slider(0.0, 1.0, value=0.5, step=0.05, label="Fold Intensity")
45
+ enhance = gr.Checkbox(label="✨ Enhance Texture", value=False)
 
 
 
46
 
47
  out = gr.Image(label="Flat, Fold-Free Output")
48
+ run = gr.Button("πŸš€ Remove Folds")
49
+ run.click(fn=remove_folds_color_preserve, inputs=[inp, intensity, enhance], outputs=out)
 
 
 
 
50
 
51
  demo.launch()