Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,74 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import cv2
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import numpy as np
|
| 6 |
from matplotlib import pyplot as plt
|
| 7 |
import random
|
| 8 |
from keras.utils import get_custom_objects
|
| 9 |
import os
|
| 10 |
|
| 11 |
-
os.environ['SM_FRAMEWORK'] = 'tf.keras'
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
-
#%env SM_FRAMEWORK=tf.keras
|
| 16 |
import segmentation_models as sm
|
| 17 |
|
| 18 |
from keras import backend as K
|
| 19 |
from keras.models import load_model
|
| 20 |
|
|
|
|
| 21 |
def jaccard_coef(y_true, y_pred):
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
weights = [0.1666, 0.1666, 0.1666, 0.1666, 0.1666, 0.1666]
|
| 30 |
-
dice_loss = sm.losses.DiceLoss(class_weights
|
| 31 |
focal_loss = sm.losses.CategoricalFocalLoss()
|
| 32 |
total_loss = dice_loss + (1 * focal_loss)
|
| 33 |
|
| 34 |
-
satellite_model = load_model('model/satellite_segmentation_full.h5',
|
|
|
|
| 35 |
|
| 36 |
-
def process_input_image(image_source):
|
| 37 |
-
#image = image_source
|
| 38 |
-
#image = image.resize((256,256))
|
| 39 |
-
#image = np.array(image)
|
| 40 |
-
image = np.expand_dims(image_source, 0)
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
predicted_image = predicted_image * 50
|
| 47 |
|
| 48 |
-
|
| 49 |
|
| 50 |
|
| 51 |
my_app = gr.Blocks()
|
| 52 |
|
| 53 |
with my_app:
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
my_app.launch(debug=True)
|
|
|
|
| 1 |
+
class_building = '#3C1098'
|
| 2 |
+
class_building = class_building.lstrip('#')
|
| 3 |
+
class_building = np.array(tuple(int(class_building[i:i+2], 16) for i in (0,2,4)))
|
| 4 |
+
|
| 5 |
+
class_land = '#8429F6'
|
| 6 |
+
class_land = class_land.lstrip('#')
|
| 7 |
+
class_land = np.array(tuple(int(class_land[i:i+2], 16) for i in (0,2,4)))
|
| 8 |
+
|
| 9 |
+
class_road = '#6EC1E4'
|
| 10 |
+
class_road = class_road.lstrip('#')
|
| 11 |
+
class_road = np.array(tuple(int(class_road[i:i+2], 16) for i in (0,2,4)))
|
| 12 |
+
|
| 13 |
+
class_vegetation = '#FEDD3A'
|
| 14 |
+
class_vegetation = class_vegetation.lstrip('#')
|
| 15 |
+
class_vegetation = np.array(tuple(int(class_vegetation[i:i+2], 16) for i in (0,2,4)))
|
| 16 |
+
|
| 17 |
+
class_water = '#E2A929'
|
| 18 |
+
class_water = class_water.lstrip('#')
|
| 19 |
+
class_water = np.array(tuple(int(class_water[i:i+2], 16) for i in (0,2,4)))
|
| 20 |
+
|
| 21 |
+
class_unlabeled = '#9B9B9B'
|
| 22 |
+
class_unlabeled = class_unlabeled.lstrip('#')
|
| 23 |
+
class_unlabeled = np.array(tuple(int(class_unlabeled[i:i+2], 16) for i in (0,2,4)))
|
| 24 |
+
|
| 25 |
+
|
| 26 |
import gradio as gr
|
| 27 |
import os
|
| 28 |
import cv2
|
| 29 |
+
from PIL import Image
|
| 30 |
+
import numpy as np
|
| 31 |
from matplotlib import pyplot as plt
|
| 32 |
import random
|
| 33 |
from keras.utils import get_custom_objects
|
| 34 |
import os
|
| 35 |
|
| 36 |
+
#os.environ['SM_FRAMEWORK'] = 'tf.keras'
|
|
|
|
|
|
|
| 37 |
|
|
|
|
| 38 |
import segmentation_models as sm
|
| 39 |
|
| 40 |
from keras import backend as K
|
| 41 |
from keras.models import load_model
|
| 42 |
|
| 43 |
+
|
| 44 |
def jaccard_coef(y_true, y_pred):
|
| 45 |
+
y_true_flatten = K.flatten(y_true)
|
| 46 |
+
y_pred_flatten = K.flatten(y_pred)
|
| 47 |
+
intersection = K.sum(y_true_flatten * y_pred_flatten)
|
| 48 |
+
final_coef_value = (intersection + 1.0) / (
|
| 49 |
+
K.sum(y_true_flatten) + K.sum(y_pred_flatten) - intersection + 1.0)
|
| 50 |
+
return final_coef_value
|
| 51 |
+
|
| 52 |
|
| 53 |
+
# six class for six weights
|
| 54 |
weights = [0.1666, 0.1666, 0.1666, 0.1666, 0.1666, 0.1666]
|
| 55 |
+
dice_loss = sm.losses.DiceLoss(class_weights=weights)
|
| 56 |
focal_loss = sm.losses.CategoricalFocalLoss()
|
| 57 |
total_loss = dice_loss + (1 * focal_loss)
|
| 58 |
|
| 59 |
+
satellite_model = load_model('model/satellite_segmentation_full.h5',
|
| 60 |
+
custom_objects=({'dice_loss_plus_1focal_loss': total_loss, 'jaccard_coef': jaccard_coef}))
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
def label_to_rgb(label_segment):
|
| 64 |
+
rgb_image = np.zeros((label_segment.shape[0], label_segment.shape[1], 3), dtype=np.uint8)
|
| 65 |
+
|
| 66 |
+
rgb_image[label_segment == 0] = class_water
|
| 67 |
+
rgb_image[label_segment == 1] = class_land
|
| 68 |
+
rgb_image[label_segment == 2] = class_road
|
| 69 |
+
rgb_image[label_segment == 3] = class_building
|
| 70 |
+
rgb_image[label_segment == 4] = class_vegetation
|
| 71 |
+
rgb_image[label_segment == 5] = class_unlabeled
|
| 72 |
+
|
| 73 |
+
return rgb_image
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def process_input_image(image_source):
|
| 77 |
+
image = np.expand_dims(image_source, 0)
|
| 78 |
+
prediction = satellite_model.predict(image)
|
| 79 |
+
predicted_image = np.argmax(prediction, axis=3)
|
| 80 |
+
predicted_image = predicted_image[0, :, :]
|
| 81 |
|
| 82 |
+
# Convert the predicted image labels to RGB
|
| 83 |
+
colored_predicted_image = label_to_rgb(predicted_image)
|
|
|
|
| 84 |
|
| 85 |
+
return "Predicted Masked Image", colored_predicted_image
|
| 86 |
|
| 87 |
|
| 88 |
my_app = gr.Blocks()
|
| 89 |
|
| 90 |
with my_app:
|
| 91 |
+
gr.Markdown("Image Processing Application UI with Gradio")
|
| 92 |
+
with gr.Tabs():
|
| 93 |
+
with gr.TabItem("Select your image"):
|
| 94 |
+
with gr.Row():
|
| 95 |
+
with gr.Column():
|
| 96 |
+
img_source = gr.Image(label="Please select source Image", shape=(256, 256))
|
| 97 |
+
source_image_loader = gr.Button("Load above Image")
|
| 98 |
+
with gr.Column():
|
| 99 |
+
output_label = gr.Label(label="Image Info")
|
| 100 |
+
img_output = gr.Image(label="Image Output")
|
| 101 |
+
source_image_loader.click(
|
| 102 |
+
process_input_image,
|
| 103 |
+
[
|
| 104 |
+
img_source
|
| 105 |
+
],
|
| 106 |
+
[
|
| 107 |
+
output_label,
|
| 108 |
+
img_output
|
| 109 |
+
]
|
| 110 |
+
)
|
| 111 |
my_app.launch(debug=True)
|