Folds / app.py
nishanth-saka's picture
App
df17c15 verified
raw
history blame
2.43 kB
import gradio as gr
import cv2, numpy as np
from PIL import Image
# -------------------------------------------------
# πŸ”§ CORE FUNCTION
# -------------------------------------------------
def remove_folds(image, intensity=0.5):
if image is None:
return None
img = np.array(image.convert("RGB"))
img_lab = cv2.cvtColor(img, cv2.COLOR_RGB2LAB)
l, a, b = cv2.split(img_lab)
# --- (1) Smooth illumination correction ---
ksize = int(49 + intensity * 100) # kernel size changes with intensity
ksize = ksize + 1 if ksize % 2 == 0 else ksize # must be odd
l_blur = cv2.GaussianBlur(l, (ksize, ksize), 0)
l_equal = cv2.divide(l, l_blur, scale=128)
# --- (2) Frequency-domain smoothing ---
freq = np.fft.fft2(l_equal)
freqshift = np.fft.fftshift(freq)
rows, cols = l_equal.shape
crow, ccol = rows // 2, cols // 2
mask = np.ones((rows, cols), np.uint8)
r = int(10 + intensity * 30) # notch size grows with intensity
mask[crow - r:crow + r, ccol - r:ccol + r] = 0
freqshift = freqshift * mask
ishift = np.fft.ifftshift(freqshift)
l_flat = np.abs(np.fft.ifft2(ishift))
l_flat = cv2.normalize(l_flat, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8)
# --- (3) Merge back and convert ---
img_lab = cv2.merge([l_flat, a, b])
result = cv2.cvtColor(img_lab, cv2.COLOR_LAB2RGB)
return Image.fromarray(result)
# -------------------------------------------------
# πŸŽ›οΈ GRADIO UI
# -------------------------------------------------
title = "πŸͺ„ Saree Fold Remover (Lighting Only, No Distortion)"
description = """
Upload a flat saree or fabric image.<br>
Adjust the **Fold Intensity** slider β€” higher values remove deeper folds.<br>
The process equalizes lighting only, keeping weave and motifs intact.
"""
iface = gr.Interface(
fn=remove_folds,
inputs=[
gr.Image(label="Upload Saree Image", type="pil"),
gr.Slider(
0.0,
1.0,
value=0.5,
step=0.05,
label="Fold Intensity",
info="Higher = stronger flattening (lighting only)",
interactive=True
),
],
outputs=gr.Image(label="Flat, Fold-Free Output"),
title=title,
description=description,
live=False, # prevents rerun while moving
throttle=0.8 # waits 0.8 s before triggering
)
if __name__ == "__main__":
iface.launch()