Spaces:
Sleeping
Sleeping
itzjunayed
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ 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):
|
8 |
smooth = 1e-5
|
@@ -11,11 +12,13 @@ def dice_coef(y_true, y_pred):
|
|
11 |
return K.mean((2.0 * intersection + smooth) / (union + smooth), axis=0)
|
12 |
|
13 |
def predict_segmentation(image):
|
|
|
|
|
|
|
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 |
|
@@ -31,26 +34,34 @@ def predict_segmentation(image):
|
|
31 |
prediction = model.predict(X_test)
|
32 |
predicted_img = np.argmax(prediction, axis=3)[0, :, :]
|
33 |
|
34 |
-
#
|
35 |
-
|
|
|
|
|
|
|
36 |
|
37 |
# Define the color for the segmented area (e.g., red)
|
38 |
-
segmented_color = [
|
39 |
|
40 |
# Set the segmented area to the desired color
|
41 |
for i in range(3):
|
42 |
-
rgba_img[:, :, i] = np.where(
|
|
|
|
|
|
|
43 |
|
44 |
-
#
|
45 |
-
|
|
|
|
|
46 |
|
47 |
-
return
|
48 |
|
49 |
# Gradio Interface
|
50 |
iface = gr.Interface(
|
51 |
fn=predict_segmentation,
|
52 |
inputs="image",
|
53 |
-
outputs="
|
54 |
live=False
|
55 |
)
|
56 |
|
|
|
3 |
import numpy as np
|
4 |
import cv2
|
5 |
from keras.utils import normalize
|
6 |
+
from PIL import Image
|
7 |
|
8 |
def dice_coef(y_true, y_pred):
|
9 |
smooth = 1e-5
|
|
|
12 |
return K.mean((2.0 * intersection + smooth) / (union + smooth), axis=0)
|
13 |
|
14 |
def predict_segmentation(image):
|
15 |
+
original_size = (image.shape[1], image.shape[0]) # (width, height)
|
16 |
+
|
17 |
+
# Resize to the model's input size
|
18 |
SIZE_X = 128
|
19 |
SIZE_Y = 128
|
|
|
|
|
20 |
img = cv2.resize(image, (SIZE_Y, SIZE_X))
|
21 |
+
|
22 |
if len(img.shape) == 3 and img.shape[2] == 3: # If the image is RGB
|
23 |
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Convert to grayscale
|
24 |
|
|
|
34 |
prediction = model.predict(X_test)
|
35 |
predicted_img = np.argmax(prediction, axis=3)[0, :, :]
|
36 |
|
37 |
+
# Resize prediction back to original image size
|
38 |
+
predicted_img_resized = cv2.resize(predicted_img, original_size, interpolation=cv2.INTER_NEAREST)
|
39 |
+
|
40 |
+
# Create an RGBA image with a transparent background
|
41 |
+
rgba_img = np.zeros((predicted_img_resized.shape[0], predicted_img_resized.shape[1], 4), dtype=np.uint8)
|
42 |
|
43 |
# Define the color for the segmented area (e.g., red)
|
44 |
+
segmented_color = [255, 0, 0] # Red color in RGB
|
45 |
|
46 |
# Set the segmented area to the desired color
|
47 |
for i in range(3):
|
48 |
+
rgba_img[:, :, i] = np.where(predicted_img_resized > 0, segmented_color[i], 0)
|
49 |
+
|
50 |
+
# Create an alpha channel: 255 where there is segmentation, 0 otherwise
|
51 |
+
rgba_img[:, :, 3] = np.where(predicted_img_resized > 0, 255, 0)
|
52 |
|
53 |
+
# Convert the numpy array to an image and save it as a PNG file
|
54 |
+
output_image = Image.fromarray(rgba_img)
|
55 |
+
output_image_path = "segmented_output.png"
|
56 |
+
output_image.save(output_image_path)
|
57 |
|
58 |
+
return output_image_path
|
59 |
|
60 |
# Gradio Interface
|
61 |
iface = gr.Interface(
|
62 |
fn=predict_segmentation,
|
63 |
inputs="image",
|
64 |
+
outputs="file",
|
65 |
live=False
|
66 |
)
|
67 |
|