File size: 1,830 Bytes
42818d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e24923
42818d5
 
 
 
 
 
 
 
 
662e28f
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
## THIS IS FOR DENOISING


import gradio as gr
from PIL import Image
import tensorflow as tf
import tensorflow_hub as hub
import numpy as np
import requests
import cv2


def resize_image(image, target_dim):
    shape = tf.cast(tf.shape(image)[1:-1], tf.float32)
    short_dim = min(shape)
    scale = target_dim / short_dim
    new_shape = tf.cast(shape * scale, tf.int32)
    image = tf.image.resize(image, new_shape)
    image = tf.image.resize_with_crop_or_pad(image, target_dim, target_dim)
    return image


def process_image(image_path, target_dim=256):
    input_img = cv2.cvtColor(image_path, cv2.COLOR_BGR2RGB)
    input_img=np.asarray(input_img,np.float32)/255.0
    input_img = tf.expand_dims(input_img, axis=0)
    input_img = resize_image(input_img, target_dim)
    return input_img


def infer(image_path: str, model: tf.keras.Model, input_resolution=(256, 256)):
    preprocessed_image = process_image(image_path, input_resolution[0])
    preds = model.predict(preprocessed_image)
    if isinstance(preds, list):
        preds = preds[-1]
        if isinstance(preds, list):
            preds = preds[-1]
    preds = np.array(preds[0], np.float32)
    final_pred_image = np.array((np.clip(preds, 0.0, 1.0)).astype(np.float32))
    return final_pred_image
input_resolution = (256,256)


def get_model(model_url: str, input_resolution: tuple) -> tf.keras.Model:
    inputs = tf.keras.Input((*input_resolution, 3))
    hub_module = hub.KerasLayer(model_url)
    outputs = hub_module(inputs)
    return tf.keras.Model(inputs, outputs)

denoise_model = get_model('./denoise_graph', input_resolution)


def predict_image(image):
    return infer(image,denoise_model,input_resolution)

image=gr.inputs.Image()

irface=gr.Interface(fn=predict_image, inputs=image, outputs=image,interpretation='default')

irface.launch()