Spaces:
Sleeping
Sleeping
itzjunayed
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
import tensorflow as tf
|
3 |
import numpy as np
|
4 |
-
import cv2
|
5 |
from keras.utils import normalize
|
6 |
|
7 |
def dice_coef(y_true, y_pred):
|
@@ -14,28 +14,21 @@ def predict_segmentation(image):
|
|
14 |
SIZE_X = 128
|
15 |
SIZE_Y = 128
|
16 |
|
17 |
-
|
18 |
-
|
19 |
img = cv2.resize(image, (SIZE_Y, SIZE_X))
|
20 |
-
|
|
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
X_test = train_images
|
26 |
|
27 |
custom_objects = {'dice_coef': dice_coef}
|
28 |
with tf.keras.utils.custom_object_scope(custom_objects):
|
29 |
model = tf.keras.models.load_model("model100.h5")
|
30 |
-
|
31 |
-
|
32 |
-
# Normalize the test image
|
33 |
-
test_img = X_test[0]
|
34 |
-
test_img_norm = test_img[:, :, 0][:, :, None]
|
35 |
-
test_img_input = np.expand_dims(test_img_norm, 0)
|
36 |
|
37 |
# Get the prediction
|
38 |
-
prediction = model.predict(
|
39 |
predicted_img = np.argmax(prediction, axis=3)[0, :, :]
|
40 |
|
41 |
# Create an RGB image with a transparent background
|
@@ -49,12 +42,10 @@ def predict_segmentation(image):
|
|
49 |
rgba_img[:, :, i] = np.where(predicted_img > 0, segmented_color[i], 0)
|
50 |
|
51 |
# Create an alpha channel: 1 where there is segmentation, 0 otherwise
|
52 |
-
|
53 |
-
rgba_img[:, :, 3] = alpha_channel
|
54 |
|
55 |
return rgba_img
|
56 |
|
57 |
-
|
58 |
# Gradio Interface
|
59 |
iface = gr.Interface(
|
60 |
fn=predict_segmentation,
|
@@ -63,4 +54,4 @@ iface = gr.Interface(
|
|
63 |
live=False
|
64 |
)
|
65 |
|
66 |
-
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import tensorflow as tf
|
3 |
import numpy as np
|
4 |
+
import cv2
|
5 |
from keras.utils import normalize
|
6 |
|
7 |
def dice_coef(y_true, y_pred):
|
|
|
14 |
SIZE_X = 128
|
15 |
SIZE_Y = 128
|
16 |
|
17 |
+
# The image is already a NumPy array, resize and convert to grayscale if needed
|
|
|
18 |
img = cv2.resize(image, (SIZE_Y, SIZE_X))
|
19 |
+
if len(img.shape) == 3 and img.shape[2] == 3: # If the image is RGB
|
20 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Convert to grayscale
|
21 |
|
22 |
+
img = np.expand_dims(img, axis=2) # Add the channel dimension
|
23 |
+
img = normalize(img, axis=1)
|
24 |
+
X_test = np.expand_dims(img, axis=0) # Add the batch dimension
|
|
|
25 |
|
26 |
custom_objects = {'dice_coef': dice_coef}
|
27 |
with tf.keras.utils.custom_object_scope(custom_objects):
|
28 |
model = tf.keras.models.load_model("model100.h5")
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
# Get the prediction
|
31 |
+
prediction = model.predict(X_test)
|
32 |
predicted_img = np.argmax(prediction, axis=3)[0, :, :]
|
33 |
|
34 |
# Create an RGB image with a transparent background
|
|
|
42 |
rgba_img[:, :, i] = np.where(predicted_img > 0, segmented_color[i], 0)
|
43 |
|
44 |
# Create an alpha channel: 1 where there is segmentation, 0 otherwise
|
45 |
+
rgba_img[:, :, 3] = np.where(predicted_img > 0, 1, 0)
|
|
|
46 |
|
47 |
return rgba_img
|
48 |
|
|
|
49 |
# Gradio Interface
|
50 |
iface = gr.Interface(
|
51 |
fn=predict_segmentation,
|
|
|
54 |
live=False
|
55 |
)
|
56 |
|
57 |
+
iface.launch(share=True)
|