Files changed (1) hide show
  1. app.py +19 -23
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 uniform_lighting(image):
6
- # Convert PIL → OpenCV
7
  img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
8
 
9
- # 1️⃣ Convert to grayscale to estimate illumination
10
- gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
11
 
12
- # 2️⃣ Smooth illumination field (large Gaussian kernel)
13
- illumination = cv2.GaussianBlur(gray, (51, 51), 0)
 
14
 
15
- # 3️⃣ Compute normalization map using brightest region
16
- brightest = np.max(illumination)
17
- illum_norm = brightest / (illumination + 1e-5)
18
- illum_norm = np.repeat(illum_norm[:, :, np.newaxis], 3, axis=2)
19
 
20
- # 4️⃣ Apply correction
21
- corrected = np.clip(img.astype(np.float32) * illum_norm, 0, 255).astype(np.uint8)
22
 
23
- # Convert back to PIL for Gradio display
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 lighting.
30
- This tool will automatically detect illumination gradients and normalize brightness
31
- using the brightest region as reference, giving you a flat, uniform look for catalog or dataset use.
32
  """
33
 
34
  demo = gr.Interface(
35
- fn=uniform_lighting,
36
  inputs=gr.Image(label="Upload Saree Image", type="pil"),
37
- outputs=gr.Image(label="Corrected Image"),
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__":