Create app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495import streamlit as stimport osimport numpy as npimport matplotlib.pyplot as pltfrom tensorflow.keras.models import load_modelfrom your_dataset_module import readDataset # Replace with your actual dataset module
# ConfigurationHEIGHT = WIDTH = 256SAR_SHAPE = (HEIGHT, WIDTH, 1)OPTIC_SHAPE = (HEIGHT, WIDTH, 3)MASK_SHAPE = (HEIGHT, WIDTH, 4) # One-hot encoded masks with 4 classes
# Class colors: non-mining (green), illegal mining (red), beach (black)CLASS_COLORS = [ [115/255, 178/255, 115/255], [1, 0, 0], [0, 0, 0]]
# Streamlit App Titlest.title("Satellite Mining Segmentation: SAR + Optic Image Inference")
# Sidebar inputsdataset_path = st.text_input("Enter dataset path", "path/to/your/dataset")model_path = st.text_input("Enter path to trained model (.h5)", "model.h5")num_samples = st.slider("Number of test samples to visualize", 1, 10, 3)
if st.button("Run Inference"): with st.spinner("Loading data and model..."):
# Prepare paths sar_path = os.path.join(dataset_path, 'sar_images') optic_path = os.path.join(dataset_path, 'optic_images') mask_path = os.path.join(dataset_path, 'masks')
# Read dataset dataset = readDataset( sarPathes=sar_path, opticPathes=optic_path, masksPathes=mask_path ) dataset.readPathes()
sar_images = dataset.readImages(dataset.sarImages, typeData='s', width=WIDTH, height=HEIGHT) optic_images = dataset.readImages(dataset.opticImages, typeData='o', width=WIDTH, height=HEIGHT) masks = dataset.readImages(dataset.masks, typeData='m', width=WIDTH, height=HEIGHT)
sar_images = dataset.normalizeImages(sar_images, 's') optic_images = dataset.normalizeImages(optic_images, 'i')
# Load model model = load_model(model_path)
# Predict pred_masks = model.predict([optic_images, sar_images], verbose=0) is_multiclass = pred_masks.shape[-1] > 1
num_samples = min(num_samples, len(sar_images))
# Plotting fig, axes = plt.subplots(num_samples, 4, figsize=(21, 6 * num_samples))
for i in range(num_samples): ax = axes[i] if num_samples > 1 else axes
ax[0].imshow(sar_images[i].squeeze(), cmap='gray') ax[0].set_title(f"SAR Image {i+1}") ax[0].axis('off')
ax[1].imshow(optic_images[i]) ax[1].set_title(f"Optic Image {i+1}") ax[1].axis('off')
if is_multiclass: gt_color_mask = np.zeros((*masks[i].shape[:2], 3)) for j, color in enumerate(CLASS_COLORS): gt_color_mask += masks[i][:,:,j][:,:,np.newaxis] * np.array(color) ax[2].imshow(gt_color_mask) else: ax[2].imshow(masks[i], cmap='gray') ax[2].set_title(f"Ground Truth Mask {i+1}") ax[2].axis('off')
if is_multiclass: pred_color_mask = np.zeros((*pred_masks[i].shape[:2], 3)) for j, color in enumerate(CLASS_COLORS): pred_color_mask += pred_masks[i][:,:,j][:,:,np.newaxis] * np.array(color) ax[3].imshow(pred_color_mask) else: ax[3].imshow(pred_masks[i], cmap='gray') ax[3].set_title(f"Predicted Mask {i+1}") ax[3].axis('off')
st.pyplot(fig)app.pyAdded+95