A1mal commited on
Commit
00aebc4
·
verified ·
1 Parent(s): 2100677

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -79
app.py CHANGED
@@ -1,115 +1,164 @@
 
1
  import io
2
  import numpy as np
3
- from PIL import Image
4
- import plotly.graph_objects as go
5
  import streamlit as st
 
 
6
 
7
  # ---------------------------------------------------------
8
- # RESULT DISPLAY
9
  # ---------------------------------------------------------
10
 
11
- with col2:
12
- st.subheader(f"Enhanced ({mode})")
13
 
14
- # Ensure correct dtype + color format
15
- result_img = metrics['result']
16
- if isinstance(result_img, np.ndarray):
17
- result_img = np.clip(result_img, 0, 255).astype(np.uint8)
18
 
19
- st.image(result_img, use_container_width=True)
 
 
 
20
 
21
- st.markdown("---")
 
 
 
22
 
23
- # ---------------------------------------------------------
24
- # METRICS
25
- # ---------------------------------------------------------
26
 
27
- m1, m2, m3 = st.columns(3)
28
- m1.metric("Sequential", f"{metrics['sequential_time']:.4f}s")
29
- m2.metric("Parallel", f"{metrics['parallel_time']:.4f}s")
30
- m3.metric("Speedup", f"{metrics['speedup']:.2f}x")
31
 
32
- # ---------------------------------------------------------
33
- # PLOTLY CHART
34
- # ---------------------------------------------------------
35
 
36
- fig = go.Figure(data=[
37
- go.Bar(name='Sequential', x=['Time'], y=[metrics['sequential_time']]),
38
- go.Bar(name='Parallel', x=['Time'], y=[metrics['parallel_time']])
39
- ])
40
 
41
- st.plotly_chart(fig, use_container_width=True)
42
 
43
- # ---------------------------------------------------------
44
- # DOWNLOAD BUTTON (FIXED BUFFER HANDLING)
45
- # ---------------------------------------------------------
46
 
47
- result_pil = Image.fromarray(np.uint8(result_img))
 
48
 
49
- buf = io.BytesIO()
50
- result_pil.save(buf, format="PNG")
51
- buf.seek(0) # 🔴 IMPORTANT FIX
52
 
53
- st.download_button(
54
- label="Download Image",
55
- data=buf,
56
- file_name="enhanced.png",
57
- mime="image/png"
58
- )
59
 
60
- # ---------------------------------------------------------
61
- # LOL DATASET EXPLORER
62
- # ---------------------------------------------------------
63
 
64
- elif app_mode == "LOL Dataset Explorer":
65
- st.header("LOL Dataset Explorer")
 
 
66
 
67
- samples = get_lol_dataset_samples()
 
 
68
 
69
- if not samples:
70
- st.warning("Dataset not found")
71
- else:
72
- selected = st.selectbox("Select Sample", [s['name'] for s in samples])
73
- sample = next(s for s in samples if s['name'] == selected)
 
 
74
 
75
- low, high = load_lol_pair(sample)
76
 
77
- col1, col2, col3 = st.columns(3)
 
 
 
78
 
79
- col1.image(low, caption="Low", use_container_width=True)
80
- col2.image(high, caption="Ground Truth", use_container_width=True)
 
81
 
82
- enhanced = enhancement_pipeline(low, {
83
- "gamma_enable": True,
84
- "gamma": gamma_val,
85
- "clahe_enable": True,
86
- "clahe_clip": clahe_clip,
87
- "denoise_enable": True,
88
- "denoise_h": denoise_h,
89
- "sharpen_enable": True
90
- })
91
 
92
- # FIX: ensure proper format
93
- enhanced = np.clip(enhanced, 0, 255).astype(np.uint8)
 
 
94
 
95
- col3.image(enhanced, caption="Enhanced", use_container_width=True)
96
 
97
- # ---------------------------------------------------------
98
- # WEBCAM MODE
99
- # ---------------------------------------------------------
 
 
 
 
100
 
101
- elif app_mode == "Webcam Live":
102
- st.header("Webcam Enhancement")
103
 
104
- img_file = st.camera_input("Capture Image")
105
 
106
- if img_file:
107
- img = Image.open(img_file).convert("RGB")
108
- img_np = np.array(img)
109
 
110
- result = run_image_enhancement(img_np)
 
 
 
 
 
 
 
 
111
 
112
- output = result['result']
113
- output = np.clip(output, 0, 255).astype(np.uint8)
114
 
115
- st.image(output, use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
  import io
3
  import numpy as np
 
 
4
  import streamlit as st
5
+ import plotly.graph_objects as go
6
+ from PIL import Image
7
 
8
  # ---------------------------------------------------------
9
+ # MAIN APP
10
  # ---------------------------------------------------------
11
 
12
+ def main():
 
13
 
14
+ st.title("Low Light Image Enhancement System")
 
 
 
15
 
16
+ app_mode = st.sidebar.selectbox(
17
+ "Choose Mode",
18
+ ["Single Image Enhancement", "LOL Dataset Explorer", "Webcam Live", "Project Report"]
19
+ )
20
 
21
+ # =====================================================
22
+ # SINGLE IMAGE ENHANCEMENT
23
+ # =====================================================
24
+ if app_mode == "Single Image Enhancement":
25
 
26
+ uploaded_file = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
 
 
27
 
28
+ if uploaded_file is not None:
 
 
 
29
 
30
+ image = Image.open(uploaded_file).convert("RGB")
31
+ image_np = np.array(image)
 
32
 
33
+ # Run enhancement pipeline
34
+ metrics = run_image_enhancement(image_np)
 
 
35
 
36
+ col1, col2 = st.columns(2)
37
 
38
+ with col1:
39
+ st.subheader("Original")
40
+ st.image(image_np, use_container_width=True)
41
 
42
+ with col2:
43
+ st.subheader(f"Enhanced ({mode})")
44
 
45
+ # 🔥 FIX: ensure safe dtype + range
46
+ result_img = metrics["result"]
47
+ result_img = np.clip(result_img, 0, 255).astype(np.uint8)
48
 
49
+ st.image(result_img, use_container_width=True)
 
 
 
 
 
50
 
51
+ st.markdown("---")
 
 
52
 
53
+ # =================================================
54
+ # METRICS DISPLAY
55
+ # =================================================
56
+ m1, m2, m3 = st.columns(3)
57
 
58
+ m1.metric("Sequential", f"{metrics['sequential_time']:.4f}s")
59
+ m2.metric("Parallel", f"{metrics['parallel_time']:.4f}s")
60
+ m3.metric("Speedup", f"{metrics['speedup']:.2f}x")
61
 
62
+ # =================================================
63
+ # PLOT
64
+ # =================================================
65
+ fig = go.Figure(data=[
66
+ go.Bar(name='Sequential', x=['Time'], y=[metrics['sequential_time']]),
67
+ go.Bar(name='Parallel', x=['Time'], y=[metrics['parallel_time']])
68
+ ])
69
 
70
+ st.plotly_chart(fig, use_container_width=True)
71
 
72
+ # =================================================
73
+ # DOWNLOAD FIX (IMPORTANT)
74
+ # =================================================
75
+ result_pil = Image.fromarray(result_img)
76
 
77
+ buf = io.BytesIO()
78
+ result_pil.save(buf, format="PNG")
79
+ buf.seek(0) # 🔴 CRITICAL FIX
80
 
81
+ st.download_button(
82
+ label="Download Enhanced Image",
83
+ data=buf,
84
+ file_name="enhanced.png",
85
+ mime="image/png"
86
+ )
 
 
 
87
 
88
+ # =====================================================
89
+ # LOL DATASET EXPLORER
90
+ # =====================================================
91
+ elif app_mode == "LOL Dataset Explorer":
92
 
93
+ st.header("LOL Dataset Explorer")
94
 
95
+ samples = get_lol_dataset_samples()
96
+
97
+ if not samples:
98
+ st.warning("Dataset not found")
99
+ else:
100
+ selected = st.selectbox("Select Sample", [s['name'] for s in samples])
101
+ sample = next(s for s in samples if s['name'] == selected)
102
 
103
+ low, high = load_lol_pair(sample)
 
104
 
105
+ col1, col2, col3 = st.columns(3)
106
 
107
+ col1.image(low, caption="Low", use_container_width=True)
108
+ col2.image(high, caption="Ground Truth", use_container_width=True)
 
109
 
110
+ enhanced = enhancement_pipeline(low, {
111
+ "gamma_enable": True,
112
+ "gamma": gamma_val,
113
+ "clahe_enable": True,
114
+ "clahe_clip": clahe_clip,
115
+ "denoise_enable": True,
116
+ "denoise_h": denoise_h,
117
+ "sharpen_enable": True
118
+ })
119
 
120
+ # 🔥 FIX: prevent parallel artifacts
121
+ enhanced = np.clip(enhanced, 0, 255).astype(np.uint8)
122
 
123
+ col3.image(enhanced, caption="Enhanced", use_container_width=True)
124
+
125
+ # =====================================================
126
+ # WEBCAM MODE
127
+ # =====================================================
128
+ elif app_mode == "Webcam Live":
129
+
130
+ st.header("Webcam Enhancement")
131
+
132
+ img_file = st.camera_input("Capture Image")
133
+
134
+ if img_file:
135
+
136
+ img = Image.open(img_file).convert("RGB")
137
+ img_np = np.array(img)
138
+
139
+ result = run_image_enhancement(img_np)
140
+
141
+ output = result["result"]
142
+
143
+ # 🔥 FIX: prevent weird brightness/lines
144
+ output = np.clip(output, 0, 255).astype(np.uint8)
145
+
146
+ st.image(output, use_container_width=True)
147
+
148
+ # =====================================================
149
+ # PROJECT REPORT
150
+ # =====================================================
151
+ elif app_mode == "Project Report":
152
+
153
+ if os.path.exists("README.md"):
154
+ with open("README.md", "r") as f:
155
+ st.markdown(f.read())
156
+ else:
157
+ st.error("README not found")
158
+
159
+
160
+ # ---------------------------------------------------------
161
+ # RUN APP
162
+ # ---------------------------------------------------------
163
+ if __name__ == "__main__":
164
+ main()