File size: 2,043 Bytes
3c9542a
 
 
 
 
 
 
 
 
 
03aa314
 
 
 
3c9542a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e886cde
3c9542a
 
 
03aa314
3c9542a
 
 
 
 
 
 
 
 
 
6d15e3c
283a00d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import gradio as gr
import cv2
import numpy as np
from tensorflow.keras.models import load_model

# Define the class names
class_names = ['AnnualCrop', 'Forest', 'HerbaceousVegetation', 'Highway', 'Industrial', 'Pasture', 'PermanentCrop', 'Residential', 'River', 'SeaLake']

# Function to load the selected model
def load_selected_model(model_name):
    if model_name == "ResNet50":
        return load_model('resnet.h5')
    elif model_name == "CNN":
        return load_model('cnn.h5')
    else:
        raise ValueError("Invalid model name")

# Function to predict the image
def predict_image(image, model_name):
    model = load_selected_model(model_name)
    img = cv2.resize(image, (64, 64))
    img = img / 255.0
    img = np.expand_dims(img, axis=0)

    predictions = model.predict(img)[0]

    predicted_class_index = np.argmax(predictions)
    predicted_class_name = class_names[predicted_class_index]

    confidence = predictions[predicted_class_index]

    # Get the top 3 predictions
    top_indices = predictions.argsort()[-3:][::-1]
    top_classes = [class_names[i] for i in top_indices]
    top_confidences = [predictions[i] for i in top_indices]

    # Format the output
    result = "Top Predictions:\n"
    for i in range(3):
        result += f"{top_classes[i]} (Confidence: {top_confidences[i]:.2f})\n"

    return result

with gr.Blocks() as demo:
    gr.Markdown("# Satellite Image Classifier (CaseOh edition)")

    with gr.Row():
        with gr.Column():
            model_selector = gr.Dropdown(choices=["ResNet50", "CNN"], label="Select Model")
            image_input = gr.Image(type="numpy", label="Upload Image")
            predict_button = gr.Button("Predict")

        image_output = gr.Image(type="numpy", label="Uploaded Image")

    prediction_output = gr.Textbox(label="Prediction")

    predict_button.click(fn=predict_image, inputs=[image_input, model_selector], outputs=prediction_output)
    image_input.change(fn=lambda x: x, inputs=image_input, outputs=image_output)

demo.launch(share=True)