File size: 2,338 Bytes
254b4c8
6c5474b
 
58aafa6
 
 
 
6c5474b
 
 
58aafa6
6c5474b
 
 
 
 
 
 
 
50f32e4
58aafa6
6c5474b
50f32e4
6c5474b
 
 
 
 
 
 
58aafa6
 
 
6c5474b
 
 
 
 
 
 
58aafa6
6c5474b
 
 
 
58aafa6
 
6c5474b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58aafa6
6c5474b
 
 
 
50f32e4
6c5474b
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import streamlit as st
import numpy as np
from PIL import Image, ImageOps
import tensorflow as tf
from tensorflow import keras
import io

# ----------------------------
# Load the model
# ----------------------------
@st.cache_resource
def load_model():
    model = keras.models.load_model(
        "src/naxi_lowlight.keras",
        custom_objects={
            "charbonnier_loss": lambda y_true, y_pred: tf.reduce_mean(tf.sqrt(tf.square(y_true - y_pred) + 1e-6)),
            "psnr_metric": lambda y_true, y_pred: tf.image.psnr(y_pred, y_true, max_val=1.0)
        }
    )
    return model

model = load_model()

# ----------------------------
# Inference function
# ----------------------------
def enhance_image_pil(pil_img):
    image = keras.utils.img_to_array(pil_img).astype("float32") / 255.0
    image = np.expand_dims(image, axis=0)
    output = model.predict(image)[0]
    output = np.clip(output * 255.0, 0, 255).astype(np.uint8)
    return Image.fromarray(output)

# ----------------------------
# Streamlit UI
# ----------------------------
st.set_page_config(page_title="NaxiLowLight Enhancement", layout="centered")
st.title("πŸŒ™ NaxiLowLight: Low-Light Image Enhancer")

st.write("Upload a low-light image to enhance it using a deep learning model.")

uploaded_file = st.file_uploader("Choose a low-light image", type=["jpg", "jpeg", "png"])

if uploaded_file is not None:
    # Load image
    image = Image.open(uploaded_file).convert("RGB")

    # Enhance
    st.write("✨ Enhancing image...")
    enhanced_image = enhance_image_pil(image)
    autocontrast_image = ImageOps.autocontrast(image)

    # Show results
    st.write("πŸ“· **Comparison:**")
    col1, col2, col3 = st.columns(3)

    with col1:
        st.image(image, caption="Original", use_column_width=True)

    with col2:
        st.image(autocontrast_image, caption="Autocontrast", use_column_width=True)

    with col3:
        st.image(enhanced_image, caption="NaxiLowLight Enhanced", use_column_width=True)

    # Download
    buf = io.BytesIO()
    enhanced_image.save(buf, format="PNG")
    byte_im = buf.getvalue()

    st.download_button(
        label="⬇️ Download Enhanced Image",
        data=byte_im,
        file_name="enhanced_image.png",
        mime="image/png"
    )
else:
    st.info("Upload a PNG or JPG image to get started.")