abhijeetsarkargis commited on
Commit
94d45d8
·
1 Parent(s): 372f1e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -40
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
- # Function to open and visualize the TIFF image using rasterio and matplotlib
10
- def visualize_tiff_image(image_path):
11
- # Open the TIFF image using rasterio
12
- with rasterio.open(image_path) as dataset:
13
- image = dataset.read() # Read image data (bands)
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
- # Create a mask for each segment
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
- # Create a color-coded segmented image
35
- segmented_image = np.zeros_like(image)
36
- for segment_id in np.unique(segments):
37
- segmented_image[segments == segment_id] = np.mean(image[segments == segment_id], axis=0)
 
 
38
 
39
- # Return the original image and the segmented images
40
- return [image, mark_boundaries(image, segments), segmented_image]
41
 
42
  # Gradio Interface
43
  iface = gr.Interface(
44
- fn=visualize_tiff_image, # Updated function to visualize the TIFF image
45
- inputs=gr.inputs.File(type="file"), # Input will be a file path to the TIFF image
46
- outputs=[gr.outputs.Image(), gr.outputs.Image(), gr.outputs.Image()],
47
- title="TIFF Image Visualization",
48
- description="Upload a TIFF image and visualize it using matplotlib.",
49
- live=True
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__":