Paushigaa commited on
Commit
22d1ca1
·
1 Parent(s): 1445fd8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import torch
4
+ import matplotlib.pyplot as plt
5
+ import cv2
6
+ import os
7
+ import leafmap
8
+ from samgeo import SamGeo, show_image, download_file, overlay_images, tms_to_geotiff
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',
18
+ sam_kwargs=None,
19
+ )
20
+
21
+ def get_all_file_paths(directory):
22
+
23
+ # initializing empty file paths list
24
+ file_paths = []
25
+
26
+ # crawling through directory and subdirectories
27
+ for root, directories, files in os.walk(directory):
28
+ for filename in files:
29
+ # join the two strings in order to form the full filepath.
30
+ filepath = os.path.join(root, filename)
31
+ file_paths.append(filepath)
32
+
33
+ # returning all file paths
34
+ return file_paths
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=='.tif':
40
+ #mkdir for app
41
+ storage='app_store'
42
+ if os.path.exists(storage):
43
+ pass
44
+ else:
45
+ os.mkdir(storage)
46
+
47
+ zip_folder=f'{storage}/zip_folder'
48
+ if os.path.exists(zip_folder):
49
+ pass
50
+ else:
51
+ os.mkdir(zip_folder)
52
+
53
+ out_file=f'{storage}/out_files'
54
+ if os.path.exists(out_file):
55
+ pass
56
+ else:
57
+ os.mkdir(out_file)
58
+
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)
65
+
66
+ # for file_name in file_paths:
67
+ # print(file_name)
68
+
69
+ with ZipFile(f'{zip_folder}/shapefile.zip','w') as zip:
70
+ # writing each file one by one
71
+ for file in file_paths:
72
+ zip.write(file)
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!."
80
+
81
+ my_app = gr.Blocks()
82
+ with my_app:
83
+ gr.Markdown("<center><h1>Segmenting Satellite Image</h1></center>")
84
+ gr.Markdown("<center><h3>Processing time depends on the file size and since the instance is running on CPU it takes longer time.</h></center>")
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)