Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import tensorflow as tf
|
4 |
+
import keras
|
5 |
+
from huggingface_hub import from_pretrained_keras
|
6 |
+
from PIL import Image
|
7 |
+
import io
|
8 |
+
import gc
|
9 |
+
|
10 |
+
# Load MIRNet model from Hugging Face
|
11 |
+
model = from_pretrained_keras("keras-io/lowlight-enhance-mirnet", compile=False)
|
12 |
+
|
13 |
+
# TensorFlow graph mode for performance
|
14 |
+
@tf.function
|
15 |
+
def enhance_image(img, passes):
|
16 |
+
for _ in tf.range(passes):
|
17 |
+
img = model(img)
|
18 |
+
return img
|
19 |
+
|
20 |
+
# Inference function
|
21 |
+
def process_image(input_img: Image.Image, passes: int):
|
22 |
+
try:
|
23 |
+
# Convert to RGB, Resize, Normalize
|
24 |
+
input_img = input_img.convert("RGB").resize((256, 256), Image.LANCZOS)
|
25 |
+
img_array = keras.preprocessing.image.img_to_array(input_img).astype("float32") / 255.0
|
26 |
+
img_array = np.expand_dims(img_array, axis=0)
|
27 |
+
|
28 |
+
# Enhance
|
29 |
+
output = enhance_image(tf.convert_to_tensor(img_array), passes)
|
30 |
+
enhanced_img = (output[0].numpy() * 255.0).clip(0, 255).astype('uint8')
|
31 |
+
result_img = Image.fromarray(enhanced_img, "RGB")
|
32 |
+
|
33 |
+
# Return both images for preview
|
34 |
+
return result_img
|
35 |
+
finally:
|
36 |
+
# Memory cleanup
|
37 |
+
del img_array, output, enhanced_img
|
38 |
+
gc.collect()
|
39 |
+
|
40 |
+
# Gradio Interface
|
41 |
+
title = "π Low-Light Image Enhancer"
|
42 |
+
description = """
|
43 |
+
Boost visibility of dark images using deep learning (MIRNet)<br>
|
44 |
+
Built for Bharatiya Antariksh Hackathon 2025 π β Team <strong>CodeKarma</strong>
|
45 |
+
"""
|
46 |
+
|
47 |
+
demo = gr.Interface(
|
48 |
+
fn=process_image,
|
49 |
+
inputs=[
|
50 |
+
gr.Image(type="pil", label="π· Upload Low-Light Image (JPG/PNG)"),
|
51 |
+
gr.Slider(minimum=1, maximum=3, value=1, step=1, label="π Enhancement Passes")
|
52 |
+
],
|
53 |
+
outputs=[
|
54 |
+
gr.Image(type="pil", label="β¨ Enhanced Image")
|
55 |
+
],
|
56 |
+
title=title,
|
57 |
+
description=description,
|
58 |
+
allow_flagging="never",
|
59 |
+
theme="soft",
|
60 |
+
)
|
61 |
+
|
62 |
+
if __name__ == "__main__":
|
63 |
+
demo.launch()
|