Spaces:
Runtime error
Runtime error
Commit ·
94d45d8
1
Parent(s): 372f1e2
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,34 @@
|
|
| 1 |
-
# Import necessary libraries
|
| 2 |
-
import numpy as np
|
| 3 |
-
import matplotlib.pyplot as plt
|
| 4 |
import gradio as gr
|
| 5 |
-
from skimage import io, color
|
| 6 |
-
from skimage.segmentation import slic, mark_boundaries
|
| 7 |
import rasterio
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
image = np.transpose(image, (1, 2, 0)) # Transpose to (height, width, channels)
|
| 15 |
-
|
| 16 |
-
# Plot the TIFF image using matplotlib
|
| 17 |
-
plt.imshow(image)
|
| 18 |
-
plt.axis('off') # Turn off axis
|
| 19 |
-
plt.show()
|
| 20 |
-
|
| 21 |
-
# Function to perform image segmentation using SLIC
|
| 22 |
-
def perform_segmentation(image):
|
| 23 |
-
# Convert the image to LAB color space for better segmentation
|
| 24 |
-
image_lab = color.rgb2lab(image)
|
| 25 |
-
|
| 26 |
-
# Perform image segmentation using SLIC (Simple Linear Iterative Clustering)
|
| 27 |
-
segments = slic(image_lab, n_segments=100, compactness=20, sigma=1)
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
mask = np.zeros(image.shape[:2], dtype=bool)
|
| 31 |
-
for segment_id in np.unique(segments):
|
| 32 |
-
mask[segments == segment_id] = 1
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
for
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
return [image, mark_boundaries(image, segments), segmented_image]
|
| 41 |
|
| 42 |
# Gradio Interface
|
| 43 |
iface = gr.Interface(
|
| 44 |
-
fn=
|
| 45 |
-
inputs=gr.inputs.File(type="file"
|
| 46 |
-
outputs=
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
)
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import rasterio
|
| 3 |
+
from rasterio.plot import show
|
| 4 |
+
from skimage.segmentation import slic
|
| 5 |
+
import numpy as np
|
| 6 |
|
| 7 |
+
def perform_segmentation(tiff_path):
|
| 8 |
+
# Open the TIFF image using Rasterio
|
| 9 |
+
with rasterio.open(tiff_path) as src:
|
| 10 |
+
# Read all the bands
|
| 11 |
+
bands = src.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# 'bands' is a 3D numpy array where each band is stored along the third dimension (axis=0)
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Perform segmentation on all the bands
|
| 16 |
+
segmented_bands = []
|
| 17 |
+
for band in bands:
|
| 18 |
+
# Modify the parameters of the slic function as needed for your specific segmentation task
|
| 19 |
+
segments = slic(band, n_segments=100, compactness=10.0, sigma=1)
|
| 20 |
+
segmented_bands.append(segments)
|
| 21 |
|
| 22 |
+
return segmented_bands
|
|
|
|
| 23 |
|
| 24 |
# Gradio Interface
|
| 25 |
iface = gr.Interface(
|
| 26 |
+
fn=perform_segmentation,
|
| 27 |
+
inputs=gr.inputs.File(type="file", label="Upload TIFF Image"),
|
| 28 |
+
outputs=gr.outputs.Image(type="numpy", label="Segmented Bands"),
|
| 29 |
+
live=True,
|
| 30 |
+
title="TIFF Image Segmentation",
|
| 31 |
+
description="Upload a TIFF image, and the app will perform segmentation on all bands.",
|
| 32 |
)
|
| 33 |
|
| 34 |
if __name__ == "__main__":
|