Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,14 +2,10 @@ import os
|
|
| 2 |
import cv2
|
| 3 |
from PIL import Image
|
| 4 |
import numpy as np
|
| 5 |
-
from keras import backend as K
|
| 6 |
-
from matplotlib import pyplot as plt
|
| 7 |
-
from tensorflow.keras.utils import to_categorical
|
| 8 |
import geopandas as gpd
|
| 9 |
import matplotlib.pyplot as plt
|
| 10 |
from keras.models import load_model
|
| 11 |
from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
| 12 |
-
from google.colab.patches import cv2_imshow
|
| 13 |
import geopandas as gpd
|
| 14 |
from skimage.measure import regionprops, label
|
| 15 |
from shapely.geometry import Polygon
|
|
@@ -29,12 +25,7 @@ def predict(img):
|
|
| 29 |
return predictions,predicted_image
|
| 30 |
|
| 31 |
def get_shape_files(img):
|
| 32 |
-
|
| 33 |
-
model = load_model('building_footprint_extraction_model.h5')
|
| 34 |
-
img_array = img_to_array(img)
|
| 35 |
-
img_array = img_array.reshape((1, 256, 256, 3))
|
| 36 |
-
img_array = img_array / 255.0
|
| 37 |
-
predictions = model.predict(img_array)
|
| 38 |
threshold = 0.5
|
| 39 |
binary_mask = (predictions > threshold).astype(np.uint8)[:, :, 1]
|
| 40 |
if np.sum(binary_mask) == 0:
|
|
@@ -55,9 +46,6 @@ def get_shape_files(img):
|
|
| 55 |
gdf.to_file(output_shapefile)
|
| 56 |
|
| 57 |
# To get Masked Image
|
| 58 |
-
predicted_image = np.argmax(predictions, axis=3)
|
| 59 |
-
predicted_image = predicted_image[0,:,:]
|
| 60 |
-
predicted_image = predicted_image * 255
|
| 61 |
cv2.imwrite('shapefiles/mask.jpg',predicted_image)
|
| 62 |
shutil.make_archive('shapefile', 'zip', 'shapefiles')
|
| 63 |
return 'shapefile.zip',predicted_image
|
|
@@ -66,25 +54,25 @@ my_app = gr.Blocks()
|
|
| 66 |
with my_app:
|
| 67 |
gr.Markdown("<center><h1>Building Footprint Extraction</h1></center>")
|
| 68 |
with gr.Tabs():
|
| 69 |
-
with gr.TabItem("Get Mask Image"):
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
source_image_loader.click(predict,img_source,img_output)
|
| 77 |
-
with gr.TabItem("Get Shapefiles"):
|
| 78 |
with gr.Row():
|
| 79 |
with gr.Column():
|
| 80 |
img_source = gr.Image(label="Please select source Image", shape=(256, 256))
|
| 81 |
get_shape_loader = gr.Button("Get Shape File")
|
| 82 |
-
|
| 83 |
with gr.Column():
|
| 84 |
with gr.Row():
|
| 85 |
mask_img=gr.Image(label="Image Output")
|
| 86 |
with gr.Row():
|
| 87 |
output_zip = gr.outputs.File()
|
| 88 |
-
|
| 89 |
get_shape_loader.click(get_shape_files,img_source,[output_zip,mask_img])
|
| 90 |
my_app.launch(debug = True)
|
|
|
|
| 2 |
import cv2
|
| 3 |
from PIL import Image
|
| 4 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
| 5 |
import geopandas as gpd
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
from keras.models import load_model
|
| 8 |
from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
|
|
|
| 9 |
import geopandas as gpd
|
| 10 |
from skimage.measure import regionprops, label
|
| 11 |
from shapely.geometry import Polygon
|
|
|
|
| 25 |
return predictions,predicted_image
|
| 26 |
|
| 27 |
def get_shape_files(img):
|
| 28 |
+
predictions,predicted_image= predict(img)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
threshold = 0.5
|
| 30 |
binary_mask = (predictions > threshold).astype(np.uint8)[:, :, 1]
|
| 31 |
if np.sum(binary_mask) == 0:
|
|
|
|
| 46 |
gdf.to_file(output_shapefile)
|
| 47 |
|
| 48 |
# To get Masked Image
|
|
|
|
|
|
|
|
|
|
| 49 |
cv2.imwrite('shapefiles/mask.jpg',predicted_image)
|
| 50 |
shutil.make_archive('shapefile', 'zip', 'shapefiles')
|
| 51 |
return 'shapefile.zip',predicted_image
|
|
|
|
| 54 |
with my_app:
|
| 55 |
gr.Markdown("<center><h1>Building Footprint Extraction</h1></center>")
|
| 56 |
with gr.Tabs():
|
| 57 |
+
# with gr.TabItem("Get Mask Image"):
|
| 58 |
+
# with gr.Row():
|
| 59 |
+
# with gr.Column():
|
| 60 |
+
# img_source = gr.Image(label="Please select source Image", shape=(256, 256))
|
| 61 |
+
# source_image_loader = gr.Button("Load above Image")
|
| 62 |
+
# with gr.Column():
|
| 63 |
+
# img_output = gr.Image(label="Image Output")
|
| 64 |
+
# source_image_loader.click(predict,img_source,[img_output])
|
| 65 |
+
with gr.TabItem("Get Mask Image and Shapefiles"):
|
| 66 |
with gr.Row():
|
| 67 |
with gr.Column():
|
| 68 |
img_source = gr.Image(label="Please select source Image", shape=(256, 256))
|
| 69 |
get_shape_loader = gr.Button("Get Shape File")
|
| 70 |
+
|
| 71 |
with gr.Column():
|
| 72 |
with gr.Row():
|
| 73 |
mask_img=gr.Image(label="Image Output")
|
| 74 |
with gr.Row():
|
| 75 |
output_zip = gr.outputs.File()
|
| 76 |
+
|
| 77 |
get_shape_loader.click(get_shape_files,img_source,[output_zip,mask_img])
|
| 78 |
my_app.launch(debug = True)
|