File size: 1,022 Bytes
fa811d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import tensorflow as tf
import numpy as np
from keras.utils import normalize

def dice_coef(y_true, y_pred):
    smooth = 1e-5
    intersection = K.sum(y_true * y_pred, axis=[1, 2, 3])
    union = K.sum(y_true, axis=[1, 2, 3]) + K.sum(y_pred, axis=[1, 2, 3])
    return K.mean((2.0 * intersection + smooth) / (union + smooth), axis=0)

def predict_segmentation(image):
    SIZE_X = 128
    SIZE_Y = 128

    img = cv2.resize(image, (SIZE_Y, SIZE_X))
    img = np.expand_dims(img, axis=2)
    img = normalize(img, axis=1)

    # Prepare image for prediction
    img = np.expand_dims(img, axis=0)

    # Predict
    prediction = model.predict(img)
    predicted_img = np.argmax(prediction, axis=3)[0, :, :]

    return predicted_img

# Load the model
model = tf.keras.models.load_model("path_to_your_model_directory", custom_objects={'dice_coef': dice_coef})

# Gradio Interface
iface = gr.Interface(
    fn=predict_segmentation,
    inputs="image",
    outputs="image",
    live=False
)

iface.launch()