File size: 3,247 Bytes
49d4ae1
 
7d4e870
 
d528d5c
 
 
49d4ae1
42f2f45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a14257
49d4ae1
3a14257
49d4ae1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0531fd8
49d4ae1
 
3a14257
49d4ae1
 
 
3a14257
3efa2c3
49d4ae1
 
 
3a14257
49d4ae1
 
3a14257
49d4ae1
 
 
 
 
 
3a14257
49d4ae1
 
3a14257
 
 
49d4ae1
 
3a14257
49d4ae1
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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()