abhijeetsarkargis commited on
Commit
d1d8cb3
·
1 Parent(s): 5349407

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -23
app.py CHANGED
@@ -1,35 +1,35 @@
1
  import gradio as gr
2
  import rasterio
3
- import numpy as np
4
 
5
- # Function to visualize all bands of the TIFF image
6
- def visualize_bands(tiff_image):
7
- # Open the TIFF image using rasterio
8
- with rasterio.open(tiff_image.name) as dataset:
9
- # Read all the raster bands
10
- image = dataset.read()
11
 
12
- # Get the number of bands in the image
13
- num_bands = image.shape[0]
 
 
14
 
15
- # Stack the bands into a single 3D array
16
- stacked_image = np.transpose(image, (1, 2, 0))
17
 
18
- # Normalize the pixel values for visualization (optional but recommended)
19
- stacked_image = (stacked_image - stacked_image.min()) / (stacked_image.max() - stacked_image.min())
 
 
 
20
 
21
- return stacked_image
 
22
 
23
- # Define the Gradio interface
 
 
24
  iface = gr.Interface(
25
  fn=visualize_bands,
26
- inputs=gr.inputs.Image(type="file", label="Upload a 10-band .tif image"),
27
- outputs=gr.outputs.Image(label="Visualized Image"),
28
- title="10-Band TIFF Image Visualization",
29
- description="Upload a 10-band .tif image and visualize all its bands.",
30
- live=True
31
  )
32
 
33
- # Launch the Gradio app
34
- if __name__ == "__main__":
35
- iface.launch()
 
1
  import gradio as gr
2
  import rasterio
3
+ import matplotlib.pyplot as plt
4
 
5
+ def visualize_bands(image_path):
6
+ # Open the image file
7
+ src = rasterio.open(image_path)
 
 
 
8
 
9
+ # Get information about the image
10
+ num_bands = src.count
11
+ height = src.height
12
+ width = src.width
13
 
14
+ # Create a subplot for each band
15
+ fig, axes = plt.subplots(nrows=2, ncols=5, figsize=(15, 6), sharex=True, sharey=True)
16
 
17
+ # Loop through each band and plot it
18
+ for i, ax in enumerate(axes.flat):
19
+ band_data = src.read(i + 1)
20
+ ax.imshow(band_data, cmap='gray')
21
+ ax.set_title(f"Band {i + 1}")
22
 
23
+ plt.tight_layout()
24
+ plt.close() # Close the plot to avoid displaying it as an output
25
 
26
+ return plt # Return the plot for Gradio to display
27
+
28
+ # Interface for Gradio app
29
  iface = gr.Interface(
30
  fn=visualize_bands,
31
+ inputs=gr.inputs.File(type="file", label="10-band .tif Image"),
32
+ outputs=gr.outputs.Image(type="plot", label="Bands Visualization")
 
 
 
33
  )
34
 
35
+ iface.launch(share=True)