Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,218 +1,241 @@
|
|
| 1 |
import tensorflow as tf
|
| 2 |
-
from tensorflow import
|
| 3 |
import streamlit as st
|
| 4 |
import cv2
|
| 5 |
import numpy as np
|
| 6 |
import time
|
| 7 |
from PIL import Image
|
| 8 |
import plotly.graph_objects as go
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
from modules.enhance import enhancement_pipeline, detect_brightness
|
| 10 |
from modules.parallel import parallel_enhancement
|
| 11 |
from modules.metrics import calculate_execution_metrics, get_image_info
|
| 12 |
from modules.dataset_loader import get_lol_dataset_samples, load_lol_pair
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
model = load_model("lowlight_enhancer.keras")
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
# ---------------------------------------------------------
|
| 20 |
-
# UI
|
| 21 |
# ---------------------------------------------------------
|
| 22 |
st.set_page_config(
|
| 23 |
page_title="Parallel Low Light Image Enhancement",
|
| 24 |
page_icon="⚡",
|
| 25 |
-
layout="wide"
|
| 26 |
-
initial_sidebar_state="expanded"
|
| 27 |
)
|
| 28 |
|
| 29 |
-
# Custom CSS
|
| 30 |
st.markdown("""
|
| 31 |
<style>
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
}
|
| 39 |
-
.stButton>button {
|
| 40 |
-
width: 100%;
|
| 41 |
-
border-radius: 8px;
|
| 42 |
-
height: 60px;
|
| 43 |
-
font-weight: bold;
|
| 44 |
-
text-transform: uppercase;
|
| 45 |
-
letter-spacing: 1px;
|
| 46 |
-
}
|
| 47 |
</style>
|
| 48 |
""", unsafe_allow_html=True)
|
| 49 |
|
| 50 |
# ---------------------------------------------------------
|
| 51 |
-
# SIDEBAR
|
| 52 |
# ---------------------------------------------------------
|
| 53 |
-
st.sidebar.
|
| 54 |
-
st.sidebar.caption("Parallel Low Light Smart Image Enhancement")
|
| 55 |
-
st.sidebar.markdown("---")
|
| 56 |
|
| 57 |
-
app_mode = st.sidebar.selectbox(
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
if app_mode != "Project Report":
|
| 60 |
-
st.sidebar.subheader("
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 65 |
st.sidebar.markdown("---")
|
| 66 |
-
|
| 67 |
cpu_count = multiprocessing.cpu_count()
|
| 68 |
-
st.sidebar.
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
st.sidebar.markdown("---")
|
| 72 |
-
st.sidebar.
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
# ---------------------------------------------------------
|
| 76 |
-
# CORE
|
| 77 |
# ---------------------------------------------------------
|
| 78 |
-
|
| 79 |
def run_image_enhancement(input_img):
|
|
|
|
| 80 |
config = {
|
| 81 |
-
"gamma_enable": True,
|
| 82 |
"gamma": gamma_val,
|
| 83 |
-
"
|
| 84 |
-
"clahe_clip": clahe_clip,
|
| 85 |
-
"denoise_enable": True,
|
| 86 |
"denoise_h": denoise_h,
|
| 87 |
-
"sharpen_enable": True,
|
| 88 |
"cores": parallel_cores
|
| 89 |
}
|
| 90 |
-
|
| 91 |
-
results = calculate_execution_metrics(
|
| 92 |
-
enhancement_pipeline,
|
| 93 |
-
parallel_enhancement,
|
| 94 |
-
input_img,
|
| 95 |
-
config
|
| 96 |
-
)
|
| 97 |
-
|
| 98 |
-
return results
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
def main():
|
|
|
|
|
|
|
| 101 |
if app_mode == "Image Workspace":
|
| 102 |
-
st.header("
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
if uploaded_file:
|
| 106 |
img = Image.open(uploaded_file).convert("RGB")
|
| 107 |
img_np = np.array(img)
|
| 108 |
-
|
| 109 |
info = get_image_info(img_np)
|
| 110 |
brightness = detect_brightness(img_np)
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
st.markdown("---")
|
| 123 |
-
if st.button("🚀 EXECUTE MULTIPROCESSING PIPELINE"):
|
| 124 |
-
with st.spinner("Synchronizing CPU workers..."):
|
| 125 |
metrics = run_image_enhancement(img_np)
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
with
|
| 130 |
-
st.subheader("
|
| 131 |
-
st.image(img_np
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
st.
|
| 135 |
-
|
| 136 |
-
|
| 137 |
st.markdown("---")
|
| 138 |
-
|
| 139 |
-
|
| 140 |
m1, m2, m3 = st.columns(3)
|
| 141 |
-
m1.metric("Sequential
|
| 142 |
-
m2.metric("Parallel
|
| 143 |
-
m3.metric("
|
| 144 |
-
|
| 145 |
fig = go.Figure(data=[
|
| 146 |
-
go.Bar(name='
|
| 147 |
-
go.Bar(name='
|
| 148 |
])
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
# Download
|
| 153 |
result_pil = Image.fromarray(metrics['result'])
|
| 154 |
buf = io.BytesIO()
|
| 155 |
result_pil.save(buf, format="PNG")
|
|
|
|
| 156 |
st.download_button(
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
mime="image/png"
|
| 161 |
)
|
| 162 |
|
|
|
|
| 163 |
elif app_mode == "LOL Dataset Explorer":
|
| 164 |
-
st.header("
|
| 165 |
-
|
| 166 |
-
|
| 167 |
samples = get_lol_dataset_samples()
|
|
|
|
| 168 |
if not samples:
|
| 169 |
-
st.
|
| 170 |
-
# Mock / Placeholder info
|
| 171 |
-
st.write("Dataset Structure Example:")
|
| 172 |
-
st.code("""
|
| 173 |
-
dataset/
|
| 174 |
-
└── LOLdataset/
|
| 175 |
-
├── low/ (485 images)
|
| 176 |
-
└── high/ (485 images)
|
| 177 |
-
""")
|
| 178 |
else:
|
| 179 |
-
|
| 180 |
-
sample = next(s for s in samples if s['name'] ==
|
| 181 |
-
|
| 182 |
low, high = load_lol_pair(sample)
|
| 183 |
-
|
| 184 |
col1, col2, col3 = st.columns(3)
|
| 185 |
-
with col1:
|
| 186 |
-
st.subheader("Low Light (Input)")
|
| 187 |
-
st.image(low, use_container_width=True)
|
| 188 |
-
with col2:
|
| 189 |
-
st.subheader("Ground Truth")
|
| 190 |
-
st.image(high, use_container_width=True)
|
| 191 |
-
with col3:
|
| 192 |
-
st.subheader("PLL-SIE Enhanced")
|
| 193 |
-
res = enhancement_pipeline(low, {"gamma": gamma_val, "clahe_limit": clahe_clip, "denoise_h": denoise_h})
|
| 194 |
-
st.image(res, use_container_width=True)
|
| 195 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
elif app_mode == "Webcam Live":
|
| 197 |
-
st.header("
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
img = Image.open(img_file_buffer).convert("RGB")
|
| 204 |
img_np = np.array(img)
|
| 205 |
-
with st.spinner("Optimizing stream..."):
|
| 206 |
-
metrics = run_image_enhancement(img_np)
|
| 207 |
-
st.image(metrics['result'], caption="Optimized View Result")
|
| 208 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
elif app_mode == "Project Report":
|
| 210 |
-
# Check if README.md exists
|
| 211 |
if os.path.exists("README.md"):
|
| 212 |
with open("README.md", "r") as f:
|
| 213 |
st.markdown(f.read())
|
| 214 |
else:
|
| 215 |
-
|
| 216 |
|
|
|
|
|
|
|
|
|
|
| 217 |
if __name__ == "__main__":
|
| 218 |
-
main()
|
|
|
|
| 1 |
import tensorflow as tf
|
| 2 |
+
from tensorflow.keras.models import load_model
|
| 3 |
import streamlit as st
|
| 4 |
import cv2
|
| 5 |
import numpy as np
|
| 6 |
import time
|
| 7 |
from PIL import Image
|
| 8 |
import plotly.graph_objects as go
|
| 9 |
+
import io
|
| 10 |
+
import os
|
| 11 |
+
import multiprocessing
|
| 12 |
+
|
| 13 |
from modules.enhance import enhancement_pipeline, detect_brightness
|
| 14 |
from modules.parallel import parallel_enhancement
|
| 15 |
from modules.metrics import calculate_execution_metrics, get_image_info
|
| 16 |
from modules.dataset_loader import get_lol_dataset_samples, load_lol_pair
|
| 17 |
+
|
| 18 |
+
# ---------------------------------------------------------
|
| 19 |
+
# LOAD MODEL
|
| 20 |
+
# ---------------------------------------------------------
|
| 21 |
model = load_model("lowlight_enhancer.keras")
|
| 22 |
|
| 23 |
+
def model_enhance(img):
|
| 24 |
+
img_resized = cv2.resize(img, (128,128)) / 255.0
|
| 25 |
+
pred = model.predict(np.expand_dims(img_resized, axis=0), verbose=0)[0]
|
| 26 |
+
pred = (pred * 255).astype(np.uint8)
|
| 27 |
+
return cv2.resize(pred, (img.shape[1], img.shape[0]))
|
| 28 |
+
|
| 29 |
# ---------------------------------------------------------
|
| 30 |
+
# UI CONFIG
|
| 31 |
# ---------------------------------------------------------
|
| 32 |
st.set_page_config(
|
| 33 |
page_title="Parallel Low Light Image Enhancement",
|
| 34 |
page_icon="⚡",
|
| 35 |
+
layout="wide"
|
|
|
|
| 36 |
)
|
| 37 |
|
| 38 |
+
# Custom CSS
|
| 39 |
st.markdown("""
|
| 40 |
<style>
|
| 41 |
+
.stMetric {
|
| 42 |
+
background-color: #1a1c24;
|
| 43 |
+
padding: 20px;
|
| 44 |
+
border-radius: 12px;
|
| 45 |
+
border: 1px solid #30363d;
|
| 46 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
</style>
|
| 48 |
""", unsafe_allow_html=True)
|
| 49 |
|
| 50 |
# ---------------------------------------------------------
|
| 51 |
+
# SIDEBAR
|
| 52 |
# ---------------------------------------------------------
|
| 53 |
+
st.sidebar.title("⚡ PLL-SIE System")
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
app_mode = st.sidebar.selectbox(
|
| 56 |
+
"Navigation",
|
| 57 |
+
["Image Workspace", "LOL Dataset Explorer", "Webcam Live", "Project Report"]
|
| 58 |
+
)
|
| 59 |
|
| 60 |
if app_mode != "Project Report":
|
| 61 |
+
st.sidebar.subheader("Enhancement Parameters")
|
| 62 |
+
|
| 63 |
+
gamma_val = st.sidebar.slider("Gamma", 0.5, 4.0, 1.8, 0.1)
|
| 64 |
+
clahe_clip = st.sidebar.slider("CLAHE", 1.0, 10.0, 3.0, 0.5)
|
| 65 |
+
denoise_h = st.sidebar.slider("Denoise", 0, 30, 12)
|
| 66 |
+
|
| 67 |
st.sidebar.markdown("---")
|
| 68 |
+
|
| 69 |
cpu_count = multiprocessing.cpu_count()
|
| 70 |
+
parallel_cores = st.sidebar.number_input(
|
| 71 |
+
"CPU Cores",
|
| 72 |
+
1,
|
| 73 |
+
cpu_count,
|
| 74 |
+
max(1, cpu_count-1)
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
# NEW MODE SELECTOR
|
| 78 |
st.sidebar.markdown("---")
|
| 79 |
+
st.sidebar.subheader("Processing Mode")
|
| 80 |
+
|
| 81 |
+
mode = st.sidebar.selectbox(
|
| 82 |
+
"Select Method",
|
| 83 |
+
["Parallel (CPU)", "Sequential", "AI Model"]
|
| 84 |
+
)
|
| 85 |
|
| 86 |
# ---------------------------------------------------------
|
| 87 |
+
# CORE FUNCTION
|
| 88 |
# ---------------------------------------------------------
|
|
|
|
| 89 |
def run_image_enhancement(input_img):
|
| 90 |
+
|
| 91 |
config = {
|
|
|
|
| 92 |
"gamma": gamma_val,
|
| 93 |
+
"clahe_limit": clahe_clip,
|
|
|
|
|
|
|
| 94 |
"denoise_h": denoise_h,
|
|
|
|
| 95 |
"cores": parallel_cores
|
| 96 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
start = time.time()
|
| 99 |
+
|
| 100 |
+
# AI MODEL
|
| 101 |
+
if mode == "AI Model":
|
| 102 |
+
result = model_enhance(input_img)
|
| 103 |
+
exec_time = time.time() - start
|
| 104 |
+
|
| 105 |
+
return {
|
| 106 |
+
"result": result,
|
| 107 |
+
"sequential_time": exec_time,
|
| 108 |
+
"parallel_time": exec_time,
|
| 109 |
+
"speedup": 1.0
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
# CLASSICAL METHODS
|
| 113 |
+
else:
|
| 114 |
+
return calculate_execution_metrics(
|
| 115 |
+
enhancement_pipeline,
|
| 116 |
+
parallel_enhancement,
|
| 117 |
+
input_img,
|
| 118 |
+
config
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
# ---------------------------------------------------------
|
| 122 |
+
# MAIN APP
|
| 123 |
+
# ---------------------------------------------------------
|
| 124 |
def main():
|
| 125 |
+
|
| 126 |
+
# ---------------- IMAGE WORKSPACE ----------------
|
| 127 |
if app_mode == "Image Workspace":
|
| 128 |
+
st.header("Image Enhancement Workspace")
|
| 129 |
+
|
| 130 |
+
uploaded_file = st.file_uploader(
|
| 131 |
+
"Upload Image",
|
| 132 |
+
type=['jpg', 'png', 'jpeg']
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
if uploaded_file:
|
| 136 |
img = Image.open(uploaded_file).convert("RGB")
|
| 137 |
img_np = np.array(img)
|
| 138 |
+
|
| 139 |
info = get_image_info(img_np)
|
| 140 |
brightness = detect_brightness(img_np)
|
| 141 |
+
|
| 142 |
+
c1, c2, c3 = st.columns(3)
|
| 143 |
+
|
| 144 |
+
c1.metric("Brightness", f"{brightness:.1f}")
|
| 145 |
+
c2.metric("Megapixels", f"{info['megapixels']:.2f}")
|
| 146 |
+
c3.metric("Status", "Low Light" if brightness < 70 else "Normal")
|
| 147 |
+
|
| 148 |
+
if st.button("Run Enhancement"):
|
| 149 |
+
with st.spinner("Processing..."):
|
| 150 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
metrics = run_image_enhancement(img_np)
|
| 152 |
+
|
| 153 |
+
col1, col2 = st.columns(2)
|
| 154 |
+
|
| 155 |
+
with col1:
|
| 156 |
+
st.subheader("Original")
|
| 157 |
+
st.image(img_np)
|
| 158 |
+
|
| 159 |
+
with col2:
|
| 160 |
+
st.subheader(f"Enhanced ({mode})")
|
| 161 |
+
st.image(metrics['result'])
|
| 162 |
+
|
| 163 |
st.markdown("---")
|
| 164 |
+
|
| 165 |
+
# PERFORMANCE
|
| 166 |
m1, m2, m3 = st.columns(3)
|
| 167 |
+
m1.metric("Sequential", f"{metrics['sequential_time']:.4f}s")
|
| 168 |
+
m2.metric("Parallel", f"{metrics['parallel_time']:.4f}s")
|
| 169 |
+
m3.metric("Speedup", f"{metrics['speedup']:.2f}x")
|
| 170 |
+
|
| 171 |
fig = go.Figure(data=[
|
| 172 |
+
go.Bar(name='Sequential', x=['Time'], y=[metrics['sequential_time']]),
|
| 173 |
+
go.Bar(name='Parallel', x=['Time'], y=[metrics['parallel_time']])
|
| 174 |
])
|
| 175 |
+
st.plotly_chart(fig)
|
| 176 |
+
|
| 177 |
+
# DOWNLOAD
|
|
|
|
| 178 |
result_pil = Image.fromarray(metrics['result'])
|
| 179 |
buf = io.BytesIO()
|
| 180 |
result_pil.save(buf, format="PNG")
|
| 181 |
+
|
| 182 |
st.download_button(
|
| 183 |
+
"Download Image",
|
| 184 |
+
buf.getvalue(),
|
| 185 |
+
"enhanced.png"
|
|
|
|
| 186 |
)
|
| 187 |
|
| 188 |
+
# ---------------- DATASET ----------------
|
| 189 |
elif app_mode == "LOL Dataset Explorer":
|
| 190 |
+
st.header("LOL Dataset Explorer")
|
| 191 |
+
|
|
|
|
| 192 |
samples = get_lol_dataset_samples()
|
| 193 |
+
|
| 194 |
if not samples:
|
| 195 |
+
st.warning("Dataset not found")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
else:
|
| 197 |
+
selected = st.selectbox("Select Sample", [s['name'] for s in samples])
|
| 198 |
+
sample = next(s for s in samples if s['name'] == selected)
|
| 199 |
+
|
| 200 |
low, high = load_lol_pair(sample)
|
| 201 |
+
|
| 202 |
col1, col2, col3 = st.columns(3)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
+
col1.image(low, caption="Low")
|
| 205 |
+
col2.image(high, caption="Ground Truth")
|
| 206 |
+
|
| 207 |
+
enhanced = enhancement_pipeline(low, {
|
| 208 |
+
"gamma": gamma_val,
|
| 209 |
+
"clahe_limit": clahe_clip,
|
| 210 |
+
"denoise_h": denoise_h
|
| 211 |
+
})
|
| 212 |
+
|
| 213 |
+
col3.image(enhanced, caption="Enhanced")
|
| 214 |
+
|
| 215 |
+
# ---------------- WEBCAM ----------------
|
| 216 |
elif app_mode == "Webcam Live":
|
| 217 |
+
st.header("Webcam Enhancement")
|
| 218 |
+
|
| 219 |
+
img_file = st.camera_input("Capture Image")
|
| 220 |
+
|
| 221 |
+
if img_file:
|
| 222 |
+
img = Image.open(img_file)
|
|
|
|
| 223 |
img_np = np.array(img)
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
+
result = run_image_enhancement(img_np)
|
| 226 |
+
|
| 227 |
+
st.image(result['result'])
|
| 228 |
+
|
| 229 |
+
# ---------------- REPORT ----------------
|
| 230 |
elif app_mode == "Project Report":
|
|
|
|
| 231 |
if os.path.exists("README.md"):
|
| 232 |
with open("README.md", "r") as f:
|
| 233 |
st.markdown(f.read())
|
| 234 |
else:
|
| 235 |
+
st.error("README not found")
|
| 236 |
|
| 237 |
+
# ---------------------------------------------------------
|
| 238 |
+
# RUN
|
| 239 |
+
# ---------------------------------------------------------
|
| 240 |
if __name__ == "__main__":
|
| 241 |
+
main()
|