Spaces:
Runtime error
Runtime error
Create App.py
Browse files
App.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import cv2
|
3 |
+
import random
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image
|
6 |
+
os.environ["SM_FRAMEWORK"] = "tf.keras"
|
7 |
+
import segmentation_models as sm
|
8 |
+
from matplotlib import pyplot as plt
|
9 |
+
|
10 |
+
from keras import backend as K
|
11 |
+
from keras.models import load_model
|
12 |
+
|
13 |
+
import gradio as gr
|
14 |
+
|
15 |
+
|
16 |
+
def jaccard_coef(y_true, y_pred):
|
17 |
+
y_true_flatten = K.flatten(y_true)
|
18 |
+
y_pred_flatten = K.flatten(y_pred)
|
19 |
+
intersection = K.sum(y_true_flatten * y_pred_flatten)
|
20 |
+
final_coef_value = (intersection + 1.0) / (K.sum(y_true_flatten) + K.sum(y_pred_flatten) - intersection + 1.0)
|
21 |
+
return final_coef_value
|
22 |
+
|
23 |
+
weights = [0.1666, 0.1666, 0.1666, 0.1666, 0.1666, 0.1666]
|
24 |
+
dice_loss = sm.losses.DiceLoss(class_weights = weights)
|
25 |
+
focal_loss = sm.losses.CategoricalFocalLoss()
|
26 |
+
total_loss = dice_loss + (1 * focal_loss)
|
27 |
+
|
28 |
+
satellite_model = load_model('model/satellite_segmentation_full.h5', custom_objects=({'dice_loss_plus_1focal_loss': total_loss, 'jaccard_coef': jaccard_coef}))
|
29 |
+
|
30 |
+
|
31 |
+
def process_input_image(image_source):
|
32 |
+
# Convert the numpy array to a PIL Image object
|
33 |
+
image = Image.fromarray(np.uint8(image_source))
|
34 |
+
|
35 |
+
# Resize the image
|
36 |
+
image = image.resize((256, 256))
|
37 |
+
|
38 |
+
# Convert the image back to a numpy array
|
39 |
+
image = np.array(image)
|
40 |
+
|
41 |
+
# Expand the dimensions of the image to match the expected input shape of the model
|
42 |
+
image = np.expand_dims(image, axis=0)
|
43 |
+
|
44 |
+
# Predict the mask for the image
|
45 |
+
prediction = satellite_model.predict(image)
|
46 |
+
predicted_image = np.argmax(prediction, axis=3)
|
47 |
+
|
48 |
+
predicted_image = predicted_image[0,:,:]
|
49 |
+
predicted_image = predicted_image * 50
|
50 |
+
|
51 |
+
return 'Predicted Masked Image', predicted_image
|
52 |
+
|
53 |
+
my_app = gr.Blocks()
|
54 |
+
|
55 |
+
with my_app:
|
56 |
+
gr.Markdown("Statellite Image Segmentation Application UI with Gradio")
|
57 |
+
with gr.Tabs():
|
58 |
+
with gr.TabItem("Select your image"):
|
59 |
+
with gr.Row():
|
60 |
+
with gr.Column():
|
61 |
+
img_source = gr.Image(label="Please select source Image", shape=(256, 256))
|
62 |
+
source_image_loader = gr.Button("Load above Image")
|
63 |
+
with gr.Column():
|
64 |
+
output_label = gr.Label(label="Image Info")
|
65 |
+
img_output = gr.Image(label="Image Output")
|
66 |
+
source_image_loader.click(
|
67 |
+
process_input_image,
|
68 |
+
[
|
69 |
+
img_source
|
70 |
+
],
|
71 |
+
[
|
72 |
+
output_label,
|
73 |
+
img_output
|
74 |
+
]
|
75 |
+
)
|
76 |
+
|
77 |
+
my_app.launch(debug=True)
|