import gradio as gr import tensorflow as tf import keras from keras import layers from tensorflow.keras.utils import get_custom_objects import numpy as np from PIL import Image def build_dce_net(): input_img = keras.Input(shape=[None, None, 3]) conv1 = layers.Conv2D( 32, (3, 3), strides=(1, 1), activation="relu", padding="same" )(input_img) conv2 = layers.Conv2D( 32, (3, 3), strides=(1, 1), activation="relu", padding="same" )(conv1) conv3 = layers.Conv2D( 32, (3, 3), strides=(1, 1), activation="relu", padding="same" )(conv2) conv4 = layers.Conv2D( 32, (3, 3), strides=(1, 1), activation="relu", padding="same" )(conv3) int_con1 = layers.Concatenate(axis=-1)([conv4, conv3]) conv5 = layers.Conv2D( 32, (3, 3), strides=(1, 1), activation="relu", padding="same" )(int_con1) int_con2 = layers.Concatenate(axis=-1)([conv5, conv2]) conv6 = layers.Conv2D( 32, (3, 3), strides=(1, 1), activation="relu", padding="same" )(int_con2) int_con3 = layers.Concatenate(axis=-1)([conv6, conv1]) x_r = layers.Conv2D(24, (3, 3), strides=(1, 1), activation="tanh", padding="same")( int_con3 ) return keras.Model(inputs=input_img, outputs=x_r) def load_model(): class ZeroDCE(keras.Model): def __init__(self, **kwargs): super().__init__(**kwargs) self.dce_model = build_dce_net() def get_enhanced_image(self, 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 call(self, data): dce_net_output = self.dce_model(data) return self.get_enhanced_image(data, dce_net_output) get_custom_objects()['ZeroDCE'] = ZeroDCE model = tf.keras.models.load_model('zero_dce_model.h5') return model def predict_image(image): model = load_model() image = image.resize((256, 256)) image_array = np.array(image) / 255.0 image_array = np.expand_dims(image_array, axis=0) predictions = model.predict(image_array) predicted_image = np.clip(predictions[0], 0, 1) predicted_image = (predicted_image * 255).astype(np.uint8) predicted_image_pil = Image.fromarray(predicted_image) return predicted_image_pil iface = gr.Interface( fn=predict_image, inputs=gr.Image(type="pil"), outputs=gr.Image(type="pil"), live=True ) iface.launch()