Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import tensorflow as tf | |
| import numpy as np | |
| from PIL import Image | |
| import os | |
| # Load the trained model | |
| model = tf.keras.models.load_model("eff_model.h5") | |
| # Same normalization you used in training | |
| def preprocess_image(image: Image.Image): | |
| image = image.resize((512, 512)).convert("RGB") | |
| image = np.array(image).astype(np.float32) / 255.0 | |
| mean = np.array([0.44101639, 0.45513914, 0.40195001]) | |
| std = np.array([0.28792392, 0.29775171, 0.29840153]) | |
| image = (image - mean) / std | |
| image = np.expand_dims(image, axis=0) # Add batch dimension | |
| return image | |
| def predict(image: Image.Image): | |
| processed = preprocess_image(image) | |
| prediction = model.predict(processed)[0][0] # sigmoid output | |
| label = "π Volcanic Eruption" if prediction > 0.5 else "β No Eruption" | |
| confidence = f"{prediction:.2%}" if prediction > 0.5 else f"{(1 - prediction):.2%}" | |
| return f"{label} (Confidence: {confidence})" | |
| # Gradio Interface | |
| interface = gr.Interface( | |
| fn=predict, | |
| inputs=gr.Image(type="pil", label="Upload Satellite Image"), | |
| outputs=gr.Textbox(label="Prediction"), | |
| title="Volcanic Eruption Detection", | |
| description="Upload a satellite image to detect a volcanic eruption using EfficientNetB7." | |
| ) | |
| if __name__ == "__main__": | |
| interface.launch() | |