Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from keras.utils import normalize
|
| 5 |
+
|
| 6 |
+
def dice_coef(y_true, y_pred):
|
| 7 |
+
smooth = 1e-5
|
| 8 |
+
intersection = K.sum(y_true * y_pred, axis=[1, 2, 3])
|
| 9 |
+
union = K.sum(y_true, axis=[1, 2, 3]) + K.sum(y_pred, axis=[1, 2, 3])
|
| 10 |
+
return K.mean((2.0 * intersection + smooth) / (union + smooth), axis=0)
|
| 11 |
+
|
| 12 |
+
def predict_segmentation(image):
|
| 13 |
+
SIZE_X = 128
|
| 14 |
+
SIZE_Y = 128
|
| 15 |
+
|
| 16 |
+
img = cv2.resize(image, (SIZE_Y, SIZE_X))
|
| 17 |
+
img = np.expand_dims(img, axis=2)
|
| 18 |
+
img = normalize(img, axis=1)
|
| 19 |
+
|
| 20 |
+
# Prepare image for prediction
|
| 21 |
+
img = np.expand_dims(img, axis=0)
|
| 22 |
+
|
| 23 |
+
# Predict
|
| 24 |
+
prediction = model.predict(img)
|
| 25 |
+
predicted_img = np.argmax(prediction, axis=3)[0, :, :]
|
| 26 |
+
|
| 27 |
+
return predicted_img
|
| 28 |
+
|
| 29 |
+
# Load the model
|
| 30 |
+
model = tf.keras.models.load_model("path_to_your_model_directory", custom_objects={'dice_coef': dice_coef})
|
| 31 |
+
|
| 32 |
+
# Gradio Interface
|
| 33 |
+
iface = gr.Interface(
|
| 34 |
+
fn=predict_segmentation,
|
| 35 |
+
inputs="image",
|
| 36 |
+
outputs="image",
|
| 37 |
+
live=False
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
iface.launch()
|