Update app.py
Browse files
app.py
CHANGED
|
@@ -9,9 +9,7 @@ from samgeo import SamGeo, show_image, download_file, overlay_images, tms_to_geo
|
|
| 9 |
from zipfile import ZipFile
|
| 10 |
|
| 11 |
|
| 12 |
-
|
| 13 |
-
Here we are using a pretrained model
|
| 14 |
-
'''
|
| 15 |
sam = SamGeo(
|
| 16 |
model_type="vit_h",
|
| 17 |
checkpoint='sam_vit_h_4b8939.pth',
|
|
@@ -35,10 +33,10 @@ def get_all_file_paths(directory):
|
|
| 35 |
|
| 36 |
def get_shape_files(image):
|
| 37 |
filename, file_extension = os.path.splitext(image.name)
|
| 38 |
-
print(filename,file_extension)
|
| 39 |
-
if file_extension
|
| 40 |
#mkdir for app
|
| 41 |
-
storage='
|
| 42 |
if os.path.exists(storage):
|
| 43 |
pass
|
| 44 |
else:
|
|
@@ -59,6 +57,11 @@ def get_shape_files(image):
|
|
| 59 |
# load_model()
|
| 60 |
sam.generate(image.name, output=f"{out_file}/masks.tif", foreground=True, unique=True)
|
| 61 |
sam.tiff_to_vector(f"{out_file}/masks.tif", out_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
# writing files to a zipfile
|
| 64 |
file_paths=get_all_file_paths(out_file)
|
|
@@ -73,7 +76,7 @@ def get_shape_files(image):
|
|
| 73 |
|
| 74 |
print('All files zipped successfully!')
|
| 75 |
|
| 76 |
-
return f'{zip_folder}/shapefile.zip'
|
| 77 |
# get_shape_files(image)
|
| 78 |
else:
|
| 79 |
return "Try uploading .tif file for processing!."
|
|
@@ -81,13 +84,14 @@ def get_shape_files(image):
|
|
| 81 |
my_app = gr.Blocks()
|
| 82 |
with my_app:
|
| 83 |
gr.Markdown("<center><h1>Farmland Segmentation using Satellite Image data</h1></center>")
|
| 84 |
-
gr.Markdown("<center><h3>Provided with a .tif image file of satellite
|
| 85 |
with gr.TabItem("Get Shapefiles"):
|
| 86 |
with gr.Row():
|
| 87 |
with gr.Column():
|
| 88 |
img_source = gr.File(label="Please select source tif")
|
| 89 |
source_image_loader = gr.Button("Get Shape File")
|
| 90 |
with gr.Column():
|
|
|
|
| 91 |
output = gr.outputs.File("zip")
|
| 92 |
-
source_image_loader.click(get_shape_files,img_source,output)
|
| 93 |
my_app.launch(debug = True)
|
|
|
|
| 9 |
from zipfile import ZipFile
|
| 10 |
|
| 11 |
|
| 12 |
+
#initialize the model
|
|
|
|
|
|
|
| 13 |
sam = SamGeo(
|
| 14 |
model_type="vit_h",
|
| 15 |
checkpoint='sam_vit_h_4b8939.pth',
|
|
|
|
| 33 |
|
| 34 |
def get_shape_files(image):
|
| 35 |
filename, file_extension = os.path.splitext(image.name)
|
| 36 |
+
# print(filename,file_extension)
|
| 37 |
+
if file_extension in ['.tif','tiff']:
|
| 38 |
#mkdir for app
|
| 39 |
+
storage='ShapeFiles_Folder'
|
| 40 |
if os.path.exists(storage):
|
| 41 |
pass
|
| 42 |
else:
|
|
|
|
| 57 |
# load_model()
|
| 58 |
sam.generate(image.name, output=f"{out_file}/masks.tif", foreground=True, unique=True)
|
| 59 |
sam.tiff_to_vector(f"{out_file}/masks.tif", out_file)
|
| 60 |
+
sam.show_anns(axis="off", alpha=1, output=f"{out_file}/annotations.tif")
|
| 61 |
+
|
| 62 |
+
#converting tif to jpg
|
| 63 |
+
image = cv2.imread(f"{out_file}/annotations.tif")
|
| 64 |
+
cv2.imwrite("{}/{}.jpg".format(out_file,'converted'), image)
|
| 65 |
|
| 66 |
# writing files to a zipfile
|
| 67 |
file_paths=get_all_file_paths(out_file)
|
|
|
|
| 76 |
|
| 77 |
print('All files zipped successfully!')
|
| 78 |
|
| 79 |
+
return f"{out_file}/converted.jpg",f'{zip_folder}/shapefile.zip'
|
| 80 |
# get_shape_files(image)
|
| 81 |
else:
|
| 82 |
return "Try uploading .tif file for processing!."
|
|
|
|
| 84 |
my_app = gr.Blocks()
|
| 85 |
with my_app:
|
| 86 |
gr.Markdown("<center><h1>Farmland Segmentation using Satellite Image data</h1></center>")
|
| 87 |
+
gr.Markdown("<center><h3>Provided with a .tif image file of satellite, this app can segment the farmlands</h></center>")
|
| 88 |
with gr.TabItem("Get Shapefiles"):
|
| 89 |
with gr.Row():
|
| 90 |
with gr.Column():
|
| 91 |
img_source = gr.File(label="Please select source tif")
|
| 92 |
source_image_loader = gr.Button("Get Shape File")
|
| 93 |
with gr.Column():
|
| 94 |
+
image=gr.Image()
|
| 95 |
output = gr.outputs.File("zip")
|
| 96 |
+
source_image_loader.click(get_shape_files,img_source,[image,output])
|
| 97 |
my_app.launch(debug = True)
|