A1mal commited on
Commit
f93c2d1
·
verified ·
1 Parent(s): c368a23

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -233
app.py CHANGED
@@ -1,268 +1,78 @@
1
  import os
2
- import io
3
- import tensorflow as tf
4
- import cv2
5
- import multiprocessing
6
- import threading
7
  import numpy as np
 
8
  import streamlit as st
9
- import plotly.graph_objects as go
10
  from PIL import Image
11
- import sys
12
- from modules.enhance import enhancement_pipeline
13
- from modules.dataset_loader import get_lol_dataset_samples, load_lol_pair
14
- from modules.parallel import run_parallel_enhancement
15
- def calculate_metrics(*args, **kwargs):
16
- return {
17
- "psnr": 0.0,
18
- "ssim": 0.0,
19
- "mse": 0.0
20
- }
21
-
22
- # Importing the Modules
23
- sys.path.append(os.path.join(os.path.dirname(__file__), "modules"))
24
- # ---------------------------------------------------------
25
- # SAFE HELPER
26
- # ---------------------------------------------------------
27
- def safe_uint8(img):
28
- img = np.array(img)
29
- img = np.nan_to_num(img)
30
- img = np.clip(img, 0, 255)
31
- return img.astype(np.uint8)
32
-
33
- # ---------------------------------------------------------
34
- # MAIN APP
35
- # ---------------------------------------------------------
36
-
37
- def main():
38
-
39
- st.title("Low Light Image Enhancement System")
40
-
41
- app_mode = st.sidebar.selectbox(
42
- "Choose Mode",
43
- ["Single Image Enhancement", "LOL Dataset Explorer", "Webcam Live", "Project Report"]
44
- )
45
-
46
- # =====================================================
47
- # SINGLE IMAGE ENHANCEMENT
48
- # =====================================================
49
- if app_mode == "Single Image Enhancement":
50
-
51
- uploaded_file = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
52
-
53
- if uploaded_file is not None:
54
-
55
- # ---------- SAFE IMAGE LOAD ----------
56
- image = Image.open(uploaded_file).convert("RGB")
57
- image_np = np.array(image)
58
-
59
- # ---------- RUN PIPELINE ----------
60
- metrics = run_image_enhancement(image_np)
61
-
62
- col1, col2 = st.columns(2)
63
-
64
- with col1:
65
- st.subheader("Original")
66
- st.image(image_np, use_container_width=True)
67
-
68
- with col2:
69
- st.subheader("Enhanced")
70
-
71
- # ---------- IMAGE ----------
72
- result_img = safe_uint8(metrics["result"])
73
-
74
- st.image(result_img, use_container_width=True)
75
-
76
- st.markdown("---")
77
-
78
- # =================================================
79
- # METRICS
80
- # =================================================
81
- m1, m2, m3 = st.columns(3)
82
- m1.metric("Sequential", f"{metrics['sequential_time']:.4f}s")
83
- m2.metric("Parallel", f"{metrics['parallel_time']:.4f}s")
84
- m3.metric("Speedup", f"{metrics['speedup']:.2f}x")
85
-
86
- # =================================================
87
- # PLOT
88
- # =================================================
89
- fig = go.Figure(data=[
90
- go.Bar(name='Sequential', x=['Time'], y=[metrics['sequential_time']]),
91
- go.Bar(name='Parallel', x=['Time'], y=[metrics['parallel_time']])
92
- ])
93
-
94
- st.plotly_chart(fig, use_container_width=True)
95
-
96
- # =================================================
97
- # DOWNLOAD
98
- # =================================================
99
- result_pil = Image.fromarray(safe_uint8(result_img))
100
-
101
- buf = io.BytesIO()
102
- result_pil.save(buf, format="PNG")
103
- buf.seek(0)
104
-
105
- st.download_button(
106
- label="Download Enhanced Image",
107
- data=buf,
108
- file_name="enhanced.png",
109
- mime="image/png"
110
- )
111
-
112
- # =====================================================
113
- # LOL DATASET EXPLORER
114
- # =====================================================
115
- elif app_mode == "LOL Dataset Explorer":
116
-
117
- st.header("LOL Dataset Explorer")
118
-
119
- samples = get_lol_dataset_samples()
120
-
121
- if not samples:
122
- st.warning("Dataset not found")
123
- return
124
-
125
- selected = st.selectbox("Select Sample", [s['name'] for s in samples])
126
- sample = next(s for s in samples if s['name'] == selected)
127
-
128
- low, high = load_lol_pair(sample)
129
-
130
- col1, col2, col3 = st.columns(3)
131
-
132
- col1.image(low, caption="Low", use_container_width=True)
133
- col2.image(high, caption="Ground Truth", use_container_width=True)
134
-
135
- enhanced = enhancement_pipeline(low, {
136
- "gamma_enable": True,
137
- "gamma": gamma_val,
138
- "clahe_enable": True,
139
- "clahe_clip": clahe_clip,
140
- "denoise_enable": True,
141
- "denoise_h": denoise_h,
142
- "sharpen_enable": True
143
- })
144
-
145
- samples = get_lol_dataset_samples()
146
 
147
- # ---------- SAFETY NET ----------
148
- enhanced = safe_uint8(enhanced)
 
 
149
 
150
- col3.image(enhanced, caption="Enhanced", use_container_width=True)
151
-
152
- # =====================================================
153
- # WEBCAM MODE
154
- # =====================================================
155
- elif app_mode == "Webcam Live":
156
-
157
- st.header("Webcam Enhancement")
158
-
159
- img_file = st.camera_input("Capture Image")
160
-
161
- if img_file:
162
-
163
- img = Image.open(img_file).convert("RGB")
164
- img_np = np.array(img)
165
-
166
- result = run_image_enhancement(img_np)
167
-
168
- output = result["result"]
169
-
170
- # ---------- FIX APPLIED ONLY ----------
171
- output = safe_uint8(output)
172
-
173
- st.image(output, use_container_width=True)
174
-
175
- # =====================================================
176
- # PROJECT REPORT
177
- # =====================================================
178
- elif app_mode == "Project Report":
179
-
180
- if os.path.exists("README.md"):
181
- with open("README.md", "r") as f:
182
- st.markdown(f.read())
183
- else:
184
- st.error("README not found")
185
-
186
-
187
- # ---------------------------------------------------------
188
- # RUN
189
- # ---------------------------------------------------------
190
- def run_image_enhancement(img_np):
191
-
192
- # sequential / baseline enhancement
193
- result = enhancement_pipeline(img_np)
194
-
195
- return {
196
- "result": result,
197
- "sequential_time": 0.0,
198
- "parallel_time": 0.0,
199
- "speedup": 1.0
200
- }
201
-
202
- def estimate_brightness(img):
203
- import numpy as np
204
- return np.mean(img)
205
-
206
-
207
- def is_low_light(img, threshold=100):
208
- return estimate_brightness(img) < threshold
209
-
210
- def enhancement_pipeline(img, config):
211
-
212
- import numpy as np
213
- import cv2
214
 
215
  img = np.array(img).astype(np.float32)
216
-
217
  brightness = np.mean(img)
218
 
219
- # -------------------------------------------------
220
- # 🚨 ADAPTIVE GATE
221
- # -------------------------------------------------
222
  if brightness > 160:
223
- # bright image → DO NOTHING (prevents damage)
224
  return img.astype(np.uint8)
225
 
226
- elif brightness > 100:
227
- # medium light → very mild correction only
228
  gamma = 1.1
229
  apply_clahe = False
230
  else:
231
- # low light → full enhancement
232
- gamma = config.get("gamma", 1.0)
233
  apply_clahe = config.get("clahe_enable", True)
234
 
235
- # -------------------------------------------------
236
- # GAMMA
237
- # -------------------------------------------------
238
  img = img / 255.0
239
  img = np.power(img, gamma) * 255.0
240
-
241
  img = np.clip(img, 0, 255).astype(np.uint8)
242
 
243
- # -------------------------------------------------
244
- # CLAHE (ONLY FOR DARK IMAGES)
245
- # -------------------------------------------------
246
  if apply_clahe:
247
  lab = cv2.cvtColor(img, cv2.COLOR_RGB2LAB)
248
  l, a, b = cv2.split(lab)
249
 
250
- clahe = cv2.createCLAHE(
251
- clipLimit=config.get("clahe_clip", 2.0),
252
- tileGridSize=(8, 8)
253
- )
254
-
255
  l = clahe.apply(l)
 
256
  img = cv2.merge((l, a, b))
257
  img = cv2.cvtColor(img, cv2.COLOR_LAB2RGB)
258
 
259
- # -------------------------------------------------
260
- # FINAL OUTPUT SAFETY
261
- # -------------------------------------------------
262
- img = np.clip(img, 0, 255)
 
 
 
 
 
 
 
263
 
264
- return img.astype(np.uint8)
265
 
 
 
 
 
 
 
 
266
 
 
 
 
 
 
 
 
 
 
267
  if __name__ == "__main__":
268
  main()
 
1
  import os
 
 
 
 
 
2
  import numpy as np
3
+ import cv2
4
  import streamlit as st
 
5
  from PIL import Image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ # -----------------------------
8
+ # ADAPTIVE PIPELINE
9
+ # -----------------------------
10
+ def enhancement_pipeline(img, config=None):
11
 
12
+ if config is None:
13
+ config = {}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  img = np.array(img).astype(np.float32)
 
16
  brightness = np.mean(img)
17
 
18
+ # Bright image → no change
 
 
19
  if brightness > 160:
 
20
  return img.astype(np.uint8)
21
 
22
+ # Medium light
23
+ if brightness > 100:
24
  gamma = 1.1
25
  apply_clahe = False
26
  else:
27
+ gamma = config.get("gamma", 1.3)
 
28
  apply_clahe = config.get("clahe_enable", True)
29
 
30
+ # Gamma correction
 
 
31
  img = img / 255.0
32
  img = np.power(img, gamma) * 255.0
 
33
  img = np.clip(img, 0, 255).astype(np.uint8)
34
 
35
+ # CLAHE only for dark images
 
 
36
  if apply_clahe:
37
  lab = cv2.cvtColor(img, cv2.COLOR_RGB2LAB)
38
  l, a, b = cv2.split(lab)
39
 
40
+ clahe = cv2.createCLAHE(2.0, (8, 8))
 
 
 
 
41
  l = clahe.apply(l)
42
+
43
  img = cv2.merge((l, a, b))
44
  img = cv2.cvtColor(img, cv2.COLOR_LAB2RGB)
45
 
46
+ return np.clip(img, 0, 255).astype(np.uint8)
47
+
48
+
49
+ # -----------------------------
50
+ # STREAMLIT APP
51
+ # -----------------------------
52
+ def main():
53
+
54
+ st.title("Low Light Enhancement System")
55
+
56
+ uploaded = st.file_uploader("Upload Image", type=["jpg", "png", "jpeg"])
57
 
58
+ if uploaded is not None:
59
 
60
+ img = Image.open(uploaded).convert("RGB")
61
+ img_np = np.array(img)
62
+
63
+ result = enhancement_pipeline(img_np, {
64
+ "gamma": 1.2,
65
+ "clahe_enable": True
66
+ })
67
 
68
+ col1, col2 = st.columns(2)
69
+
70
+ col1.image(img_np, caption="Original")
71
+ col2.image(result, caption="Enhanced")
72
+
73
+
74
+ # -----------------------------
75
+ # RUN
76
+ # -----------------------------
77
  if __name__ == "__main__":
78
  main()