File size: 1,258 Bytes
5d3b587
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
from tensorflow.keras.models import load_model
from PIL import Image
import gradio as gr

# Load model
model = load_model("unet_model.h5", compile=False)

# Preprocess function
def preprocess_image(image, target_size=(192, 176)):
    image = image.resize((target_size[1], target_size[0]))  # width, height
    image = np.array(image) / 255.0
    if image.ndim == 2:
        image = np.expand_dims(image, axis=-1)
    return np.expand_dims(image, axis=0)

# Prediction function for Gradio
def predict(img):
    # Convert to grayscale
    img = img.convert("L")

    # Preprocess
    input_data = preprocess_image(img)

    # Model prediction
    pred = model.predict(input_data)[0]

    # Remove channel
    if pred.ndim == 3 and pred.shape[-1] == 1:
        pred = np.squeeze(pred, axis=-1)

    # Convert to image
    pred_img = (pred * 255).astype(np.uint8)
    pred_img = Image.fromarray(pred_img)

    return pred_img

# Gradio UI
interface = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil", label="Upload Image"),
    outputs=gr.Image(type="pil", label="Denoised Output"),
    title="U-Net Image Denoising",
    description="Upload a grayscale image and get the denoised result using a U-Net model."
)

interface.launch()