Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
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
|
16 |
+
import shutil
|
17 |
+
import gradio as gr
|
18 |
+
|
19 |
+
def predict(img):
|
20 |
+
# model = load_model('drive/My Drive/building_footprint_extraction_model.h5')
|
21 |
+
model = load_model('/content/drive/MyDrive/Colab Notebooks/building_footprint_extraction_model.h5')
|
22 |
+
img_array = img_to_array(img)
|
23 |
+
img_array = img_array.reshape((1, 256, 256, 3))
|
24 |
+
img_array = img_array / 255.0
|
25 |
+
predictions = model.predict(img_array)
|
26 |
+
predicted_image = np.argmax(predictions, axis=3)
|
27 |
+
predicted_image = predicted_image[0,:,:]
|
28 |
+
predicted_image = predicted_image * 255
|
29 |
+
return predictions,predicted_image
|
30 |
+
|
31 |
+
def get_shape_files(img):
|
32 |
+
# predictions,_ = predict(img)
|
33 |
+
model = load_model('/content/drive/MyDrive/Colab Notebooks/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:
|
41 |
+
print("No building pixels detected. Saving an empty shapefile.")
|
42 |
+
else:
|
43 |
+
labeled_mask = label(binary_mask)
|
44 |
+
building_polygons = []
|
45 |
+
props = regionprops(labeled_mask)
|
46 |
+
for prop in props:
|
47 |
+
polygon = Polygon([(point[1], point[0]) for point in prop.coords])
|
48 |
+
building_polygons.append(polygon)
|
49 |
+
gdf = gpd.GeoDataFrame(geometry=building_polygons, crs="EPSG:4326")
|
50 |
+
output_shapefile = "shapefiles/building_footprints.shp"
|
51 |
+
if os.path.exists('shapefiles'):
|
52 |
+
pass
|
53 |
+
else:
|
54 |
+
os.mkdir('shapefiles')
|
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
|
64 |
+
|
65 |
+
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 |
+
with gr.Row():
|
71 |
+
with gr.Column():
|
72 |
+
img_source = gr.Image(label="Please select source Image", shape=(256, 256))
|
73 |
+
source_image_loader = gr.Button("Load above Image")
|
74 |
+
with gr.Column():
|
75 |
+
img_output = gr.Image(label="Image Output")
|
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)
|