ajoy0071998 commited on
Commit
10dc532
·
verified ·
1 Parent(s): d71e2d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -26
app.py CHANGED
@@ -1,9 +1,10 @@
1
  import streamlit as st
2
  import numpy as np
3
  import tensorflow as tf
4
- from tensorflow.keras.preprocessing.image import load_img, img_to_array
5
- from PIL import Image
6
  import json
 
 
7
 
8
  # Load the model
9
  model = tf.keras.models.load_model("EuroSAT_model.h5")
@@ -11,38 +12,52 @@ model = tf.keras.models.load_model("EuroSAT_model.h5")
11
  # Load class labels
12
  with open("label_map.json", "r") as f:
13
  class_labels = json.load(f)
14
-
15
- # Reverse the class labels to get index to class name mapping
16
  class_labels = {v: k for k, v in class_labels.items()}
17
 
18
- # Function to preprocess the image
19
- def preprocess_image(image, target_size=(224, 224)):
20
- image = image.resize(target_size)
21
- image = img_to_array(image)
22
- image = np.expand_dims(image, axis=0)
23
- image = image / 255.0 # Normalizing
24
- return image
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  # Streamlit App
27
- st.title("Satellite Image Classification")
28
 
29
- st.write("Upload a satellite image, and the model will predict its class.")
30
 
31
- # Image uploader
32
- uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
33
 
34
  if uploaded_file is not None:
35
- # Display the uploaded image
36
- image = Image.open(uploaded_file)
37
- st.image(image, caption="Uploaded Image", use_column_width=True)
38
-
39
- # Preprocess the image
40
- processed_image = preprocess_image(image)
41
-
42
- # Make prediction
 
43
  predictions = model.predict(processed_image)
44
  class_idx = np.argmax(predictions, axis=1)[0]
45
  predicted_class = class_labels[class_idx]
46
-
47
- # Show the predicted class
48
- st.write(f"Predicted Class: {predicted_class}")
 
 
 
 
1
  import streamlit as st
2
  import numpy as np
3
  import tensorflow as tf
4
+ import rasterio
 
5
  import json
6
+ import tempfile
7
+ import os
8
 
9
  # Load the model
10
  model = tf.keras.models.load_model("EuroSAT_model.h5")
 
12
  # Load class labels
13
  with open("label_map.json", "r") as f:
14
  class_labels = json.load(f)
 
 
15
  class_labels = {v: k for k, v in class_labels.items()}
16
 
17
+ # Function to preprocess a .tif image and compute indices
18
+ def preprocess_tif(file_path, target_size=(224, 224)):
19
+ with rasterio.open(file_path) as src:
20
+ B3 = src.read(3).astype(np.float32) # Green
21
+ B4 = src.read(4).astype(np.float32) # Red
22
+ B5 = src.read(5).astype(np.float32) # NIR
23
+ B6 = src.read(6).astype(np.float32) # SWIR1
24
+
25
+ NDVI = (B5 - B4) / (B5 + B4 + 1e-5)
26
+ NDBI = (B6 - B5) / (B6 + B5 + 1e-5)
27
+ NDWI = (B3 - B5) / (B3 + B5 + 1e-5)
28
+
29
+ index_img = np.stack([NDBI, NDVI, NDWI], axis=-1)
30
+ index_img = np.nan_to_num(index_img)
31
+ index_img = tf.image.resize(index_img, target_size).numpy()
32
+ index_img = np.clip(index_img, -1, 1)
33
+ index_img = np.expand_dims(index_img, axis=0) # Add batch dimension
34
+
35
+ return index_img
36
 
37
  # Streamlit App
38
+ st.title("Satellite Image Classification with Spectral Indices")
39
 
40
+ st.write("Upload a .tif satellite image. The model computes NDVI, NDBI, and NDWI, then predicts the class.")
41
 
42
+ # File uploader
43
+ uploaded_file = st.file_uploader("Choose a .tif image...", type=["tif", "tiff"])
44
 
45
  if uploaded_file is not None:
46
+ # Save uploaded file to a temporary location
47
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".tif") as tmp:
48
+ tmp.write(uploaded_file.read())
49
+ tmp_path = tmp.name
50
+
51
+ # Preprocess image
52
+ processed_image = preprocess_tif(tmp_path)
53
+
54
+ # Predict
55
  predictions = model.predict(processed_image)
56
  class_idx = np.argmax(predictions, axis=1)[0]
57
  predicted_class = class_labels[class_idx]
58
+
59
+ # Show results
60
+ st.write(f"Predicted Class: **{predicted_class}**")
61
+
62
+ # Cleanup temp file
63
+ os.remove(tmp_path)