A1mal commited on
Commit
8d2073e
·
verified ·
1 Parent(s): 514c864

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +218 -0
app.py ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import cv2
3
+ import numpy as np
4
+ import time
5
+ from PIL import Image
6
+ import plotly.graph_objects as go
7
+ from modules.enhance import enhancement_pipeline, detect_brightness
8
+ from modules.parallel import parallel_enhancement
9
+ from modules.metrics import calculate_execution_metrics, get_image_info
10
+ from modules.dataset_loader import get_lol_dataset_samples, load_lol_pair
11
+ import io
12
+ import os
13
+ import multiprocessing
14
+
15
+ # ---------------------------------------------------------
16
+ # UI CONFIGURATION
17
+ # ---------------------------------------------------------
18
+ st.set_page_config(
19
+ page_title="Parallel Low Light Image Enhancement",
20
+ page_icon="⚡",
21
+ layout="wide",
22
+ initial_sidebar_state="expanded"
23
+ )
24
+
25
+ # Custom CSS for Professional Branding
26
+ st.markdown("""
27
+ <style>
28
+ .stMetric {
29
+ background-color: #1a1c24;
30
+ padding: 20px;
31
+ border-radius: 12px;
32
+ border: 1px solid #30363d;
33
+ box-shadow: 0 4px 6px rgba(0,0,0,0.1);
34
+ }
35
+ .stButton>button {
36
+ width: 100%;
37
+ border-radius: 8px;
38
+ height: 60px;
39
+ font-weight: bold;
40
+ text-transform: uppercase;
41
+ letter-spacing: 1px;
42
+ }
43
+ </style>
44
+ """, unsafe_allow_html=True)
45
+
46
+ # ---------------------------------------------------------
47
+ # SIDEBAR CONTROLS
48
+ # ---------------------------------------------------------
49
+ st.sidebar.markdown("<h1 style='color:#ff4b4b;'>⚡ PLL-SIE System</h1>", unsafe_allow_html=True)
50
+ st.sidebar.caption("Parallel Low Light Smart Image Enhancement")
51
+ st.sidebar.markdown("---")
52
+
53
+ app_mode = st.sidebar.selectbox("Navigation", ["Image Workspace", "LOL Dataset Explorer", "Webcam Live", "Project Report"])
54
+
55
+ if app_mode != "Project Report":
56
+ st.sidebar.subheader("🎚️ Enhancement Params")
57
+ gamma_val = st.sidebar.slider("Gamma Correction", 0.5, 4.0, 1.8, 0.1)
58
+ clahe_clip = st.sidebar.slider("CLAHE Contrast", 1.0, 10.0, 3.0, 0.5)
59
+ denoise_h = st.sidebar.slider("Noise Reduction", 0, 30, 12, 1)
60
+
61
+ st.sidebar.markdown("---")
62
+ st.sidebar.subheader("⚙️ System Status")
63
+ cpu_count = multiprocessing.cpu_count()
64
+ st.sidebar.info(f"Available Logical Cores: {cpu_count}")
65
+ parallel_cores = st.sidebar.number_input("Cores to allocate", 1, cpu_count, max(1, cpu_count-1))
66
+
67
+ st.sidebar.markdown("---")
68
+ st.sidebar.markdown("### Development Info")
69
+ st.sidebar.write("v1.2.0-Production")
70
+
71
+ # ---------------------------------------------------------
72
+ # CORE LOGIC
73
+ # ---------------------------------------------------------
74
+
75
+ def run_image_enhancement(input_img):
76
+ config = {
77
+ "gamma_enable": True,
78
+ "gamma": gamma_val,
79
+ "clahe_enable": True,
80
+ "clahe_clip": clahe_clip,
81
+ "denoise_enable": True,
82
+ "denoise_h": denoise_h,
83
+ "sharpen_enable": True,
84
+ "cores": parallel_cores
85
+ }
86
+
87
+ results = calculate_execution_metrics(
88
+ enhancement_pipeline,
89
+ parallel_enhancement,
90
+ input_img,
91
+ config
92
+ )
93
+
94
+ return results
95
+
96
+ def main():
97
+ if app_mode == "Image Workspace":
98
+ st.header("📸 Image Enhancement Workspace")
99
+ uploaded_file = st.file_uploader("Select source image", type=['jpg', 'jpeg', 'png', 'tiff'])
100
+
101
+ if uploaded_file:
102
+ img = Image.open(uploaded_file).convert("RGB")
103
+ img_np = np.array(img)
104
+
105
+ info = get_image_info(img_np)
106
+ brightness = detect_brightness(img_np)
107
+
108
+ # Metadata Dashboard
109
+ col_b1, col_b2, col_b3 = st.columns(3)
110
+ with col_b1:
111
+ st.metric("Mean Brightness", f"{brightness:.1f} cd/m²")
112
+ with col_b2:
113
+ st.metric("Total Pixels", f"{info['megapixels']:.2f} MP")
114
+ with col_b3:
115
+ status = "🔴 UNDEREXPOSED" if brightness < 70 else "🟢 OPTIMAL"
116
+ st.metric("System Auto-Diagnosis", status)
117
+
118
+ st.markdown("---")
119
+ if st.button("🚀 EXECUTE MULTIPROCESSING PIPELINE"):
120
+ with st.spinner("Synchronizing CPU workers..."):
121
+ metrics = run_image_enhancement(img_np)
122
+
123
+ # Visualization
124
+ c1, c2 = st.columns(2)
125
+ with c1:
126
+ st.subheader("Input Frame")
127
+ st.image(img_np, use_container_width=True)
128
+ with c2:
129
+ st.subheader("Enhanced Signal")
130
+ st.image(metrics['result'], use_container_width=True)
131
+
132
+ # Performance Graph
133
+ st.markdown("---")
134
+ st.subheader("📈 Throughput Analytics")
135
+
136
+ m1, m2, m3 = st.columns(3)
137
+ m1.metric("Sequential (ST)", f"{metrics['sequential_time']:.4f}s")
138
+ m2.metric("Parallel (MP)", f"{metrics['parallel_time']:.4f}s")
139
+ m3.metric("Efficiency Gain", f"{metrics['speedup']:.2f}x")
140
+
141
+ fig = go.Figure(data=[
142
+ go.Bar(name='Single-Thread', x=['Execution'], y=[metrics['sequential_time']], marker_color='#ff4b4b'),
143
+ go.Bar(name='Multi-Process', x=['Execution'], y=[metrics['parallel_time']], marker_color='#00d1ff')
144
+ ])
145
+ fig.update_layout(template="plotly_dark", barmode='group', height=300)
146
+ st.plotly_chart(fig, use_container_width=True)
147
+
148
+ # Download
149
+ result_pil = Image.fromarray(metrics['result'])
150
+ buf = io.BytesIO()
151
+ result_pil.save(buf, format="PNG")
152
+ st.download_button(
153
+ label="💾 Export High-Fidelity Result",
154
+ data=buf.getvalue(),
155
+ file_name="enhanced_parallel_output.png",
156
+ mime="image/png"
157
+ )
158
+
159
+ elif app_mode == "LOL Dataset Explorer":
160
+ st.header("📂 LOL Dataset Browser")
161
+ st.write("Browse and compare ground truth vs low-light enhancement from the LOL dataset.")
162
+
163
+ samples = get_lol_dataset_samples()
164
+ if not samples:
165
+ st.info("Place the LOL dataset in folder: `dataset/LOLdataset/low` to use this feature.")
166
+ # Mock / Placeholder info
167
+ st.write("Dataset Structure Example:")
168
+ st.code("""
169
+ dataset/
170
+ └── LOLdataset/
171
+ ├── low/ (485 images)
172
+ └── high/ (485 images)
173
+ """)
174
+ else:
175
+ selected_sample_name = st.selectbox("Select Sample", [s['name'] for s in samples])
176
+ sample = next(s for s in samples if s['name'] == selected_sample_name)
177
+
178
+ low, high = load_lol_pair(sample)
179
+
180
+ col1, col2, col3 = st.columns(3)
181
+ with col1:
182
+ st.subheader("Low Light (Input)")
183
+ st.image(low, use_container_width=True)
184
+ with col2:
185
+ st.subheader("Ground Truth")
186
+ st.image(high, use_container_width=True)
187
+ with col3:
188
+ st.subheader("PLL-SIE Enhanced")
189
+ res = enhancement_pipeline(low, {"gamma": gamma_val, "clahe_limit": clahe_clip, "denoise_h": denoise_h})
190
+ st.image(res, use_container_width=True)
191
+
192
+ elif app_mode == "Webcam Live":
193
+ st.header("🎥 Live Signal Capture")
194
+ st.markdown("Use your camera to capture and optimize dark environment frames.")
195
+
196
+ img_file_buffer = st.camera_input("Snapshot for Smart Optimization")
197
+
198
+ if img_file_buffer:
199
+ img = Image.open(img_file_buffer).convert("RGB")
200
+ img_np = np.array(img)
201
+ with st.spinner("Optimizing stream..."):
202
+ metrics = run_image_enhancement(img_np)
203
+ st.image(metrics['result'], caption="Optimized View Result")
204
+
205
+ elif app_mode == "Project Report":
206
+ # Check if README.md exists
207
+ if os.path.exists("README.md"):
208
+ with open("README.md", "r") as f:
209
+ st.markdown(f.read())
210
+ else:
211
+ st.error("Report (README.md) not found.")
212
+
213
+ if __name__ == "__main__":
214
+ main()
215
+
216
+
217
+ if __name__ == "__main__":
218
+ main()