Spaces:
Runtime error
Runtime error
Commit ·
e5aa9c3
1
Parent(s): 6ff5490
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,98 +1,58 @@
|
|
| 1 |
-
# Make sure to install the required libraries if you haven't already
|
| 2 |
-
# pip install joblib
|
| 3 |
-
# pip install rasterio
|
| 4 |
-
# pip install matplotlib
|
| 5 |
-
# pip install gradio
|
| 6 |
|
| 7 |
-
import joblib
|
| 8 |
import rasterio
|
| 9 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
| 10 |
import gradio as gr
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
Returns:
|
| 20 |
-
model: The loaded machine learning model.
|
| 21 |
-
"""
|
| 22 |
-
model = joblib.load(file_path)
|
| 23 |
-
return model
|
| 24 |
-
|
| 25 |
-
def open_tiff_image(file_path):
|
| 26 |
-
"""
|
| 27 |
-
Open and read the TIFF image using rasterio.
|
| 28 |
-
|
| 29 |
-
Parameters:
|
| 30 |
-
file_path (str): The path to the TIFF image file.
|
| 31 |
-
|
| 32 |
-
Returns:
|
| 33 |
-
data (numpy array): A 3D NumPy array representing the TIFF image data.
|
| 34 |
-
"""
|
| 35 |
-
with rasterio.open(file_path) as dataset:
|
| 36 |
-
data = dataset.read()
|
| 37 |
-
return data
|
| 38 |
-
|
| 39 |
-
def visualize_all_bands(tiff_data):
|
| 40 |
-
"""
|
| 41 |
-
Visualize all 10 bands of the TIFF image using matplotlib.
|
| 42 |
-
|
| 43 |
-
Parameters:
|
| 44 |
-
tiff_data (numpy array): The 3D NumPy array representing the TIFF image data.
|
| 45 |
-
|
| 46 |
-
Returns:
|
| 47 |
-
None
|
| 48 |
-
"""
|
| 49 |
-
num_bands = tiff_data.shape[0]
|
| 50 |
-
|
| 51 |
-
fig, axs = plt.subplots(2, 5, figsize=(15, 6))
|
| 52 |
-
|
| 53 |
-
for band_num, ax in enumerate(axs.ravel()):
|
| 54 |
-
band_to_visualize = tiff_data[band_num]
|
| 55 |
-
ax.imshow(band_to_visualize, cmap='jet')
|
| 56 |
-
ax.set_title(f'Band {band_num + 1}')
|
| 57 |
-
ax.axis('off')
|
| 58 |
-
|
| 59 |
-
plt.tight_layout()
|
| 60 |
plt.show()
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
model_file_path (str): The path to the model.pkl file.
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
""
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
# Open and read the TIFF image from the uploaded file
|
| 76 |
-
with rasterio.open(tiff_image.name) as dataset:
|
| 77 |
-
tiff_image_data = dataset.read()
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
if __name__ == "__main__":
|
| 83 |
-
# Create a Gradio interface
|
| 84 |
-
iface = gr.Interface(
|
| 85 |
-
fn=visualize_with_gradio,
|
| 86 |
-
inputs=[
|
| 87 |
-
gr.inputs.File(label="Upload TIFF Image", type="file"),
|
| 88 |
-
"text"
|
| 89 |
-
],
|
| 90 |
-
outputs=None,
|
| 91 |
-
live=True,
|
| 92 |
-
title="Visualize 10 Bands of TIFF Image",
|
| 93 |
-
description="Upload a TIFF image and provide the path to the model.pkl file.",
|
| 94 |
-
article="",
|
| 95 |
-
)
|
| 96 |
-
|
| 97 |
-
# Launch the Gradio interface
|
| 98 |
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
|
|
|
| 2 |
import rasterio
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
+
from skimage.segmentation import slic
|
| 5 |
+
from skimage.color import label2rgb
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
+
# Function to visualize a single band
|
| 9 |
+
def plot_band(band_data, band_id):
|
| 10 |
+
plt.figure(figsize=(8, 8))
|
| 11 |
+
plt.imshow(band_data, cmap='viridis')
|
| 12 |
+
plt.title(f"Band {band_id}")
|
| 13 |
+
plt.colorbar(shrink=0.6)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
plt.show()
|
| 15 |
|
| 16 |
+
# Function to perform segmentation on a single band
|
| 17 |
+
def segment_band(band_data, band_id):
|
| 18 |
+
# Perform SLIC (Simple Linear Iterative Clustering) segmentation
|
| 19 |
+
segments = slic(band_data, n_segments=100, compactness=10, sigma=1)
|
| 20 |
|
| 21 |
+
# Map segment labels to different colors
|
| 22 |
+
segmented_image = label2rgb(segments, image=band_data, kind='avg')
|
|
|
|
| 23 |
|
| 24 |
+
plt.figure(figsize=(8, 8))
|
| 25 |
+
plt.imshow(segmented_image)
|
| 26 |
+
plt.title(f"Segmented Band {band_id}")
|
| 27 |
+
plt.show()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
def main(image_path):
|
| 30 |
+
try:
|
| 31 |
+
# Open the TIFF file in read mode
|
| 32 |
+
with rasterio.open(image_path) as dataset:
|
| 33 |
+
# Check the number of bands in the image
|
| 34 |
+
num_bands = dataset.count
|
| 35 |
+
print(f"The image has {num_bands} bands.")
|
| 36 |
+
|
| 37 |
+
# Read all the bands into separate numpy arrays
|
| 38 |
+
bands_data = [dataset.read(band_id) for band_id in range(1, num_bands + 1)]
|
| 39 |
+
|
| 40 |
+
# Plot and segment each band
|
| 41 |
+
for band_id, band_data in enumerate(bands_data, 1):
|
| 42 |
+
plot_band(band_data, band_id)
|
| 43 |
+
segment_band(band_data, band_id)
|
| 44 |
+
|
| 45 |
+
except Exception as e:
|
| 46 |
+
print(f"An error occurred: {e}")
|
| 47 |
+
|
| 48 |
+
# Gradio Interface
|
| 49 |
+
iface = gr.Interface(fn=main,
|
| 50 |
+
inputs="text",
|
| 51 |
+
outputs=None,
|
| 52 |
+
title="TIFF Image Band Visualization and Segmentation",
|
| 53 |
+
description="Enter the path to a TIFF image containing multiple bands.",
|
| 54 |
+
examples=[['Composite_10_bands_Purulia_2.tif']],
|
| 55 |
+
allow_flagging=False)
|
| 56 |
|
| 57 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
iface.launch()
|