Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
#
|
| 9 |
# ---------------------------------------------------------
|
| 10 |
|
| 11 |
-
|
| 12 |
-
st.subheader(f"Enhanced ({mode})")
|
| 13 |
|
| 14 |
-
|
| 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.
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
# METRICS
|
| 25 |
-
# ---------------------------------------------------------
|
| 26 |
|
| 27 |
-
|
| 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 |
-
|
| 34 |
-
# ---------------------------------------------------------
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
go.Bar(name='Parallel', x=['Time'], y=[metrics['parallel_time']])
|
| 39 |
-
])
|
| 40 |
|
| 41 |
-
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
|
| 47 |
-
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
-
st.
|
| 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 |
-
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
|
| 76 |
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
"denoise_h": denoise_h,
|
| 89 |
-
"sharpen_enable": True
|
| 90 |
-
})
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
-
|
| 102 |
-
st.header("Webcam Enhancement")
|
| 103 |
|
| 104 |
-
|
| 105 |
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
img_np = np.array(img)
|
| 109 |
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|