Spaces:
Sleeping
Sleeping
Color FIX
#4
by
nishanth-saka - opened
app.py
CHANGED
|
@@ -3,67 +3,49 @@ import cv2, numpy as np
|
|
| 3 |
from PIL import Image
|
| 4 |
|
| 5 |
# -------------------------------------------------
|
| 6 |
-
#
|
| 7 |
# -------------------------------------------------
|
| 8 |
-
def
|
| 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)
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
l_blur = cv2.GaussianBlur(l, (ksize, ksize), 0)
|
| 20 |
-
l_equal = cv2.divide(l, l_blur, scale=128)
|
| 21 |
|
| 22 |
-
# --- (2)
|
| 23 |
-
|
| 24 |
-
|
| 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)
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
# -------------------------------------------------
|
| 41 |
-
#
|
| 42 |
# -------------------------------------------------
|
| 43 |
-
with gr.Blocks(title="πͺ Saree Fold Remover
|
| 44 |
-
gr.Markdown(
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 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 |
-
|
| 57 |
-
label="Fold Intensity",
|
| 58 |
-
info="Higher = stronger flattening (lighting only)"
|
| 59 |
-
)
|
| 60 |
|
| 61 |
out = gr.Image(label="Flat, Fold-Free Output")
|
| 62 |
-
|
| 63 |
-
|
| 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()
|