Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,24 +17,24 @@ sam = SamGeo(
|
|
| 17 |
)
|
| 18 |
|
| 19 |
def get_all_file_paths(directory):
|
| 20 |
-
|
| 21 |
# initializing empty file paths list
|
| 22 |
file_paths = []
|
| 23 |
-
|
| 24 |
# crawling through directory and subdirectories
|
| 25 |
for root, directories, files in os.walk(directory):
|
| 26 |
for filename in files:
|
| 27 |
# join the two strings in order to form the full filepath.
|
| 28 |
filepath = os.path.join(root, filename)
|
| 29 |
file_paths.append(filepath)
|
| 30 |
-
|
| 31 |
# returning all file paths
|
| 32 |
-
return file_paths
|
| 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
|
| 38 |
#mkdir for app
|
| 39 |
storage='app_store'
|
| 40 |
if os.path.exists(storage):
|
|
@@ -47,7 +47,7 @@ def get_shape_files(image):
|
|
| 47 |
pass
|
| 48 |
else:
|
| 49 |
os.mkdir(zip_folder)
|
| 50 |
-
|
| 51 |
out_file=f'{storage}/out_files'
|
| 52 |
if os.path.exists(out_file):
|
| 53 |
pass
|
|
@@ -57,10 +57,15 @@ def get_shape_files(image):
|
|
| 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 |
|
| 61 |
# writing files to a zipfile
|
| 62 |
file_paths=get_all_file_paths(out_file)
|
| 63 |
-
|
| 64 |
# for file_name in file_paths:
|
| 65 |
# print(file_name)
|
| 66 |
|
|
@@ -68,10 +73,10 @@ def get_shape_files(image):
|
|
| 68 |
# writing each file one by one
|
| 69 |
for file in file_paths:
|
| 70 |
zip.write(file)
|
| 71 |
-
|
| 72 |
-
print('All files zipped successfully!')
|
| 73 |
|
| 74 |
-
|
|
|
|
|
|
|
| 75 |
# get_shape_files(image)
|
| 76 |
else:
|
| 77 |
return "Try uploading .tif file for processing!."
|
|
@@ -86,6 +91,7 @@ with my_app:
|
|
| 86 |
img_source = gr.File(label="Please select source tif")
|
| 87 |
source_image_loader = gr.Button("Get Shape File")
|
| 88 |
with gr.Column():
|
|
|
|
| 89 |
output = gr.outputs.File("zip")
|
| 90 |
-
source_image_loader.click(get_shape_files,img_source,output)
|
| 91 |
my_app.launch(debug = True)
|
|
|
|
| 17 |
)
|
| 18 |
|
| 19 |
def get_all_file_paths(directory):
|
| 20 |
+
|
| 21 |
# initializing empty file paths list
|
| 22 |
file_paths = []
|
| 23 |
+
|
| 24 |
# crawling through directory and subdirectories
|
| 25 |
for root, directories, files in os.walk(directory):
|
| 26 |
for filename in files:
|
| 27 |
# join the two strings in order to form the full filepath.
|
| 28 |
filepath = os.path.join(root, filename)
|
| 29 |
file_paths.append(filepath)
|
| 30 |
+
|
| 31 |
# returning all file paths
|
| 32 |
+
return file_paths
|
| 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='app_store'
|
| 40 |
if os.path.exists(storage):
|
|
|
|
| 47 |
pass
|
| 48 |
else:
|
| 49 |
os.mkdir(zip_folder)
|
| 50 |
+
|
| 51 |
out_file=f'{storage}/out_files'
|
| 52 |
if os.path.exists(out_file):
|
| 53 |
pass
|
|
|
|
| 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)
|
| 68 |
+
|
| 69 |
# for file_name in file_paths:
|
| 70 |
# print(file_name)
|
| 71 |
|
|
|
|
| 73 |
# writing each file one by one
|
| 74 |
for file in file_paths:
|
| 75 |
zip.write(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!."
|
|
|
|
| 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)
|