Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +61 -36
src/streamlit_app.py
CHANGED
|
@@ -1,53 +1,78 @@
|
|
| 1 |
-
import tempfile
|
| 2 |
import streamlit as st
|
|
|
|
|
|
|
| 3 |
import tensorflow as tf
|
| 4 |
from tensorflow import keras
|
| 5 |
-
import numpy as np
|
| 6 |
-
from PIL import Image
|
| 7 |
import io
|
| 8 |
-
from huggingface_hub import hf_hub_download
|
| 9 |
-
import os
|
| 10 |
-
|
| 11 |
-
# Set Streamlit page
|
| 12 |
-
st.set_page_config(page_title="NaxiLowLight Enhancer", layout="centered")
|
| 13 |
-
st.title("π NaxiLowLight - Low-Light Image Enhancer")
|
| 14 |
|
| 15 |
-
#
|
|
|
|
|
|
|
| 16 |
@st.cache_resource
|
| 17 |
-
def
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
return model
|
| 25 |
|
| 26 |
-
model =
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
output = np.clip(output * 255.0, 0, 255).astype(np.uint8)
|
| 35 |
return Image.fromarray(output)
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
| 41 |
image = Image.open(uploaded_file).convert("RGB")
|
| 42 |
-
st.image(image, caption="πΌοΈ Original Image", use_column_width=True)
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image, ImageOps
|
| 4 |
import tensorflow as tf
|
| 5 |
from tensorflow import keras
|
|
|
|
|
|
|
| 6 |
import io
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# ----------------------------
|
| 9 |
+
# Load the model
|
| 10 |
+
# ----------------------------
|
| 11 |
@st.cache_resource
|
| 12 |
+
def load_model():
|
| 13 |
+
model = keras.models.load_model(
|
| 14 |
+
"src/naxi_lowlight.keras",
|
| 15 |
+
custom_objects={
|
| 16 |
+
"charbonnier_loss": lambda y_true, y_pred: tf.reduce_mean(tf.sqrt(tf.square(y_true - y_pred) + 1e-6)),
|
| 17 |
+
"psnr_metric": lambda y_true, y_pred: tf.image.psnr(y_pred, y_true, max_val=1.0)
|
| 18 |
+
}
|
| 19 |
+
)
|
| 20 |
return model
|
| 21 |
|
| 22 |
+
model = load_model()
|
| 23 |
|
| 24 |
+
# ----------------------------
|
| 25 |
+
# Inference function
|
| 26 |
+
# ----------------------------
|
| 27 |
+
def enhance_image_pil(pil_img):
|
| 28 |
+
image = keras.utils.img_to_array(pil_img).astype("float32") / 255.0
|
| 29 |
+
image = np.expand_dims(image, axis=0)
|
| 30 |
+
output = model.predict(image)[0]
|
| 31 |
output = np.clip(output * 255.0, 0, 255).astype(np.uint8)
|
| 32 |
return Image.fromarray(output)
|
| 33 |
|
| 34 |
+
# ----------------------------
|
| 35 |
+
# Streamlit UI
|
| 36 |
+
# ----------------------------
|
| 37 |
+
st.set_page_config(page_title="NaxiLowLight Enhancement", layout="centered")
|
| 38 |
+
st.title("π NaxiLowLight: Low-Light Image Enhancer")
|
| 39 |
+
|
| 40 |
+
st.write("Upload a low-light image to enhance it using a deep learning model.")
|
| 41 |
|
| 42 |
+
uploaded_file = st.file_uploader("Choose a low-light image", type=["jpg", "jpeg", "png"])
|
| 43 |
+
|
| 44 |
+
if uploaded_file is not None:
|
| 45 |
+
# Load image
|
| 46 |
image = Image.open(uploaded_file).convert("RGB")
|
|
|
|
| 47 |
|
| 48 |
+
# Enhance
|
| 49 |
+
st.write("β¨ Enhancing image...")
|
| 50 |
+
enhanced_image = enhance_image_pil(image)
|
| 51 |
+
autocontrast_image = ImageOps.autocontrast(image)
|
| 52 |
+
|
| 53 |
+
# Show results
|
| 54 |
+
st.write("π· **Comparison:**")
|
| 55 |
+
col1, col2, col3 = st.columns(3)
|
| 56 |
+
|
| 57 |
+
with col1:
|
| 58 |
+
st.image(image, caption="Original", use_column_width=True)
|
| 59 |
+
|
| 60 |
+
with col2:
|
| 61 |
+
st.image(autocontrast_image, caption="Autocontrast", use_column_width=True)
|
| 62 |
+
|
| 63 |
+
with col3:
|
| 64 |
+
st.image(enhanced_image, caption="NaxiLowLight Enhanced", use_column_width=True)
|
| 65 |
|
| 66 |
+
# Download
|
| 67 |
+
buf = io.BytesIO()
|
| 68 |
+
enhanced_image.save(buf, format="PNG")
|
| 69 |
+
byte_im = buf.getvalue()
|
| 70 |
|
| 71 |
+
st.download_button(
|
| 72 |
+
label="β¬οΈ Download Enhanced Image",
|
| 73 |
+
data=byte_im,
|
| 74 |
+
file_name="enhanced_image.png",
|
| 75 |
+
mime="image/png"
|
| 76 |
+
)
|
| 77 |
+
else:
|
| 78 |
+
st.info("Upload a PNG or JPG image to get started.")
|