CoolFace
Apppublic

Eleawa/EuroSat_Image_Classification

sourceHugging Faceapache-2.0updated 1y agoView on Hugging Face
0likes
app.py61 linesDownload Raw Back to root
1import gradio as gr2import numpy as np3from PIL import Image4import tensorflow as tf5 6# Load the trained model7model = tf.keras.models.load_model("preprocessed_model.keras")8input_size = (224, 224)9 10# EuroSAT class names11class_names = [12    'AnnualCrop', 'Forest', 'HerbaceousVegetation',13    'Highway', 'Industrial', 'Pasture',14    'PermanentCrop', 'Residential', 'River',15    'SeaLake'16]17 18# Prediction function19def classify_image(img: Image.Image):20    try:21        # Resize and preprocess the image22        img_resized = img.resize(input_size)23        img_array = np.array(img_resized) / 255.0  # Normalize24        img_array = np.expand_dims(img_array, axis=0)25 26        # Predict27        predictions = model.predict(img_array)[0]28        predicted_index = np.argmax(predictions)29        predicted_class = class_names[predicted_index]30        confidence = predictions[predicted_index]31 32        # Create dictionary of class probabilities33        result = {34            class_names[i]: float(predictions[i])35            for i in range(len(class_names))36        }37 38        return predicted_class, confidence, result39    except Exception as e:40        return f"Error: {str(e)}", 0.0, {}41 42# Gradio Interface43image_input = gr.Image(type="pil", label="Upload EuroSAT Image")44label_output = gr.Label(num_top_classes=3, label="Top Predictions")45text_output = gr.Textbox(label="Predicted Class with Confidence")46 47interface = gr.Interface(48    fn=lambda img: (49        classify_image(img)[0] + f" ({classify_image(img)[1]*100:.2f}%)",50        classify_image(img)[2]51    ),52    inputs=image_input,53    outputs=[text_output, label_output],54    title="EuroSAT Land Cover Classifier",55    description="Upload a satellite image (EuroSAT-like) to classify its land cover type using a deep learning model."56)57 58# Launch locally or on HF Spaces59if __name__ == "__main__":60    interface.launch()61