Spaces:
Sleeping
Sleeping
CLAHE
#3
by
nishanth-saka - opened
app.py
CHANGED
|
@@ -2,42 +2,38 @@ import gradio as gr
|
|
| 2 |
import cv2, numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
|
| 5 |
-
def
|
| 6 |
-
# Convert PIL → OpenCV
|
| 7 |
img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
corrected = np.clip(img.astype(np.float32) * illum_norm, 0, 255).astype(np.uint8)
|
| 22 |
|
| 23 |
-
|
| 24 |
-
corrected_rgb = cv2.cvtColor(corrected, cv2.COLOR_BGR2RGB)
|
| 25 |
-
return Image.fromarray(corrected_rgb)
|
| 26 |
-
|
| 27 |
-
title = "🧵 Saree AI – Uniform Lighting Correction"
|
| 28 |
description = """
|
| 29 |
-
Upload a saree image with uneven
|
| 30 |
-
This
|
| 31 |
-
|
| 32 |
"""
|
| 33 |
|
| 34 |
demo = gr.Interface(
|
| 35 |
-
fn=
|
| 36 |
inputs=gr.Image(label="Upload Saree Image", type="pil"),
|
| 37 |
-
outputs=gr.Image(label="
|
| 38 |
title=title,
|
| 39 |
description=description,
|
| 40 |
-
examples=[],
|
| 41 |
)
|
| 42 |
|
| 43 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import cv2, numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
|
| 5 |
+
def clahe_uniform(image):
|
| 6 |
+
# Convert PIL → OpenCV BGR
|
| 7 |
img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 8 |
|
| 9 |
+
# --- Convert to LAB color space ---
|
| 10 |
+
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
|
| 11 |
+
L, A, B = cv2.split(lab)
|
| 12 |
|
| 13 |
+
# --- Apply CLAHE (Contrast Limited Adaptive Histogram Equalization) ---
|
| 14 |
+
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
|
| 15 |
+
L2 = clahe.apply(L)
|
| 16 |
|
| 17 |
+
# --- Merge back channels and convert to RGB ---
|
| 18 |
+
merged = cv2.merge([L2, A, B])
|
| 19 |
+
result = cv2.cvtColor(merged, cv2.COLOR_LAB2BGR)
|
| 20 |
+
result_rgb = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
|
| 21 |
|
| 22 |
+
return [image, Image.fromarray(result_rgb)]
|
|
|
|
| 23 |
|
| 24 |
+
title = "🧵 Saree AI – Uniform Lighting (CLAHE)"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
description = """
|
| 26 |
+
Upload a saree image with uneven illumination.
|
| 27 |
+
This app applies **CLAHE (adaptive histogram equalization)** to balance lighting locally
|
| 28 |
+
while preserving texture and color richness.
|
| 29 |
"""
|
| 30 |
|
| 31 |
demo = gr.Interface(
|
| 32 |
+
fn=clahe_uniform,
|
| 33 |
inputs=gr.Image(label="Upload Saree Image", type="pil"),
|
| 34 |
+
outputs=[gr.Image(label="Original"), gr.Image(label="CLAHE Corrected")],
|
| 35 |
title=title,
|
| 36 |
description=description,
|
|
|
|
| 37 |
)
|
| 38 |
|
| 39 |
if __name__ == "__main__":
|