File size: 1,454 Bytes
5d14bf1
dde0842
 
5d14bf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dde0842
5d14bf1
 
 
 
 
ed93e6f
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
import numpy as np

import gradio as gr
from PIL import Image
import tensorflow as tf
import keras
from huggingface_hub import from_pretrained_keras
#dfvds

model = from_pretrained_keras("hbpkillerX/low_light_img_enhancer", compile=False)

#model= tf.saved_model.load("hbpkillerX/low_light_img_enhancer/")

def autocontrast(tensor, cutoff=0):
    tensor = tf.cast(tensor, dtype=tf.float32)
    min_val = tf.reduce_min(tensor)
    max_val = tf.reduce_max(tensor)
    range_val = max_val - min_val
    adjusted_tensor = tf.clip_by_value(tf.cast(tf.round((tensor - min_val - cutoff) * (255 / (range_val - 2 * cutoff))), tf.uint8), 0, 255)
    return adjusted_tensor



def infer(original_image):
    image = keras.utils.img_to_array(original_image)
    image = autocontrast(image)
    image = image.astype("float32") / 255.0
    image = np.expand_dims(image, axis=0)
    output = model.predict(image)
    output_image = output[0] * 255.0
    output_image = output_image.clip(0, 255)
    output_image = output_image.reshape(
        (np.shape(output_image)[0], np.shape(output_image)[1], 3)
    )
    output_image = np.uint32(output_image)
    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", shape=(960, 640))],
    outputs="image",
    cache_examples=True)