A1mal commited on
Commit
533a575
·
verified ·
1 Parent(s): fcb37d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -14
app.py CHANGED
@@ -174,15 +174,10 @@ def main():
174
  # ---------------------------------------------------------
175
  def run_image_enhancement(img_np):
176
 
177
- # call your existing pipeline
178
  result = enhancement_pipeline(img_np, {
179
- "gamma_enable": True,
180
- "gamma": 1.0,
181
  "clahe_enable": True,
182
- "clahe_clip": 2.0,
183
- "denoise_enable": True,
184
- "denoise_h": 10,
185
- "sharpen_enable": True
186
  })
187
 
188
  return {
@@ -192,22 +187,70 @@ def run_image_enhancement(img_np):
192
  "speedup": 1.0
193
  }
194
 
 
 
 
 
 
 
 
 
195
  def enhancement_pipeline(img, config):
196
 
 
 
 
197
  img = np.array(img).astype(np.float32)
198
 
199
- if config.get("gamma_enable", False):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
200
  gamma = config.get("gamma", 1.0)
201
- img = np.power(img / 255.0, gamma) * 255.0
202
 
203
- if config.get("clahe_enable", False):
204
- lab = cv2.cvtColor(img.astype(np.uint8), cv2.COLOR_RGB2LAB)
 
 
 
 
 
 
 
 
 
 
 
205
  l, a, b = cv2.split(lab)
206
- clahe = cv2.createCLAHE(clipLimit=config.get("clahe_clip", 2.0), tileGridSize=(8,8))
 
 
 
 
 
207
  l = clahe.apply(l)
208
- img = cv2.merge((l,a,b))
209
  img = cv2.cvtColor(img, cv2.COLOR_LAB2RGB)
210
 
211
- return np.clip(img, 0, 255).astype(np.uint8)
 
 
 
 
 
 
 
212
  if __name__ == "__main__":
213
  main()
 
174
  # ---------------------------------------------------------
175
  def run_image_enhancement(img_np):
176
 
 
177
  result = enhancement_pipeline(img_np, {
178
+ "gamma": 1.2,
 
179
  "clahe_enable": True,
180
+ "clahe_clip": 2.0
 
 
 
181
  })
182
 
183
  return {
 
187
  "speedup": 1.0
188
  }
189
 
190
+ def estimate_brightness(img):
191
+ import numpy as np
192
+ return np.mean(img)
193
+
194
+
195
+ def is_low_light(img, threshold=100):
196
+ return estimate_brightness(img) < threshold
197
+
198
  def enhancement_pipeline(img, config):
199
 
200
+ import numpy as np
201
+ import cv2
202
+
203
  img = np.array(img).astype(np.float32)
204
 
205
+ brightness = np.mean(img)
206
+
207
+ # -------------------------------------------------
208
+ # 🚨 ADAPTIVE GATE
209
+ # -------------------------------------------------
210
+ if brightness > 160:
211
+ # bright image → DO NOTHING (prevents damage)
212
+ return img.astype(np.uint8)
213
+
214
+ elif brightness > 100:
215
+ # medium light → very mild correction only
216
+ gamma = 1.1
217
+ apply_clahe = False
218
+ else:
219
+ # low light → full enhancement
220
  gamma = config.get("gamma", 1.0)
221
+ apply_clahe = config.get("clahe_enable", True)
222
 
223
+ # -------------------------------------------------
224
+ # GAMMA
225
+ # -------------------------------------------------
226
+ img = img / 255.0
227
+ img = np.power(img, gamma) * 255.0
228
+
229
+ img = np.clip(img, 0, 255).astype(np.uint8)
230
+
231
+ # -------------------------------------------------
232
+ # CLAHE (ONLY FOR DARK IMAGES)
233
+ # -------------------------------------------------
234
+ if apply_clahe:
235
+ lab = cv2.cvtColor(img, cv2.COLOR_RGB2LAB)
236
  l, a, b = cv2.split(lab)
237
+
238
+ clahe = cv2.createCLAHE(
239
+ clipLimit=config.get("clahe_clip", 2.0),
240
+ tileGridSize=(8, 8)
241
+ )
242
+
243
  l = clahe.apply(l)
244
+ img = cv2.merge((l, a, b))
245
  img = cv2.cvtColor(img, cv2.COLOR_LAB2RGB)
246
 
247
+ # -------------------------------------------------
248
+ # FINAL OUTPUT SAFETY
249
+ # -------------------------------------------------
250
+ img = np.clip(img, 0, 255)
251
+
252
+ return img.astype(np.uint8)
253
+
254
+
255
  if __name__ == "__main__":
256
  main()