import numpy as np
import gradio as gr
from PIL import Image
import tensorflow as tf
from tensorflow import keras
from huggingface_hub import from_pretrained_keras
model = from_pretrained_keras("Harveenchadha/low-light-image-enhancement", compile=False)
#examples = ['examples/179.png', 'examples/493.png', 'examples/780.png']
def get_enhanced_image(data, output):
r1 = output[:, :, :, :3]
r2 = output[:, :, :, 3:6]
r3 = output[:, :, :, 6:9]
r4 = output[:, :, :, 9:12]
r5 = output[:, :, :, 12:15]
r6 = output[:, :, :, 15:18]
r7 = output[:, :, :, 18:21]
r8 = output[:, :, :, 21:24]
x = data + r1 * (tf.square(data) - data)
x = x + r2 * (tf.square(x) - x)
x = x + r3 * (tf.square(x) - x)
enhanced_image = x + r4 * (tf.square(x) - x)
x = enhanced_image + r5 * (tf.square(enhanced_image) - enhanced_image)
x = x + r6 * (tf.square(x) - x)
x = x + r7 * (tf.square(x) - x)
enhanced_image = x + r8 * (tf.square(x) - x)
return enhanced_image
def infer(original_image):
image = keras.preprocessing.image.img_to_array(original_image)
image = image.astype("float32") / 255.0
image = np.expand_dims(image, axis=0)
output = model.predict(image)
output = get_enhanced_image(image, output)
output_image = tf.cast((output[0, :, :, :] * 255), dtype=np.uint8)
output_image = Image.fromarray(output_image.numpy())
# output_image = output_image.numpy()
# print(output_image.shape())
return output_image
iface = gr.Interface(
fn=infer,
title="Low Light Image Enhancement",
description = "Keras Implementation of MIRNet model for light up the dark image 🌆🎆",
inputs=[gr.inputs.Image(label="image", type="pil")],
outputs=[gr.outputs.Image(label="image", type="numpy")],
#examples=examples,
article = "Author: Vu Minh Chien. Based on the keras example from Soumik Rakshit",
).launch(debug=True)