Chancee12 commited on
Commit
cfd9318
·
1 Parent(s): b83b6e7

Create app.py

Browse files

Created the app.py

Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
9
+ %env SM_FRAMEWORK=tf.keras
10
+ import segmentation_models as sm
11
+
12
+ from keras import backend as K
13
+ from keras.models import load_model
14
+
15
+ def jaccard_coef(y_true, y_pred):
16
+ y_true_flatten = K.flatten(y_true)
17
+ y_pred_flatten = K.flatten(y_pred)
18
+ intersection = K.sum(y_true_flatten * y_pred_flatten)
19
+ final_coef_value = (intersection + 1.0) / (K.sum(y_true_flatten) + K.sum(y_pred_flatten) - intersection + 1.0)
20
+ return final_coef_value
21
+
22
+ #six class for six weights
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('/content/satellite_segmentation_full.h5', custom_objects=({'dice_loss_plus_1focal_loss' : total_loss, 'jaccard_coef': jaccard_coef}))
29
+
30
+ def process_input_image(image_source):
31
+ #image = image_source
32
+ #image = image.resize((256,256))
33
+ #image = np.array(image)
34
+ image = np.expand_dims(image_source, 0)
35
+
36
+ prediction = satellite_model.predict(image)
37
+
38
+ predicted_image = np.argmax(prediction, axis=3)
39
+ predicted_image = predicted_image[0,:,:]
40
+ predicted_image = predicted_image * 50
41
+
42
+ return "Predicted Masked Image", predicted_image
43
+
44
+
45
+ my_app = gr.Blocks()
46
+
47
+ with my_app:
48
+ gr.Markdown("Image Processing Application UI with Gradio")
49
+ with gr.Tabs():
50
+ with gr.TabItem("Select your image"):
51
+ with gr.Row():
52
+ with gr.Column():
53
+ img_source = gr.Image(label="Please select source Image", shape=(256,256))
54
+ source_image_loader = gr.Button("Load above Image")
55
+ with gr.Column():
56
+ output_label = gr.Label(label="Image Info")
57
+ img_output = gr.Image(label="Image Output")
58
+ source_image_loader.click(
59
+ process_input_image,
60
+ [
61
+ img_source
62
+ ],
63
+ [
64
+ output_label,
65
+ img_output
66
+ ]
67
+ )
68
+
69
+ my_app.launch(debug=True)