|
import os |
|
import tensorflow as tf |
|
import gradio as gr |
|
import numpy as np |
|
from PIL import Image |
|
|
|
|
|
os.environ["CUDA_VISIBLE_DEVICES"] = "-1" |
|
current_directory = os.path.abspath(os.path.dirname(__file__)) |
|
|
|
|
|
def load_model(): |
|
model = tf.keras.models.load_model(os.path.join(current_directory, "model.h5")) |
|
return model |
|
|
|
model = load_model() |
|
|
|
|
|
labels = ['Water', 'Cloudy', 'Desert', 'Green Area'] |
|
|
|
|
|
def classify_image(image): |
|
|
|
if not isinstance(image, Image.Image): |
|
image = Image.fromarray(image) |
|
|
|
img = image.resize((128, 128)) |
|
img = np.array(img) / 255.0 |
|
img = np.expand_dims(img, axis=0) |
|
prediction = model.predict(img) |
|
predicted_class = labels[np.argmax(prediction)] |
|
|
|
|
|
return {labels[i]: float(prediction[0][i]) for i in range(len(labels))} |
|
|
|
|
|
image_input = gr.Image(type="pil") |
|
label_output = gr.Label(num_top_classes=4) |
|
|
|
|
|
gr.Interface(fn=classify_image, |
|
inputs=image_input, |
|
outputs=label_output, |
|
title="Satellite Image Classification", |
|
description="Classify satellite images into four types: Water, Cloudy, Desert, Green Area").launch() |