Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import tensorflow as tf
|
3 |
+
import gradio as gr
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
# Disable all GPUS
|
8 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
9 |
+
current_directory = os.path.abspath(os.path.dirname(__file__))
|
10 |
+
|
11 |
+
# Load your pre-trained model
|
12 |
+
def load_model():
|
13 |
+
model = tf.keras.models.load_model(os.path.join(current_directory, "model.h5")) # Replace with your model's path
|
14 |
+
return model
|
15 |
+
|
16 |
+
model = load_model()
|
17 |
+
|
18 |
+
# Define the labels (categories)
|
19 |
+
labels = ['Water', 'Cloudy', 'Desert', 'Green Area']
|
20 |
+
|
21 |
+
# Function to preprocess the image and predict the class
|
22 |
+
def classify_image(image):
|
23 |
+
# Ensure the image is in PIL format
|
24 |
+
if not isinstance(image, Image.Image):
|
25 |
+
image = Image.fromarray(image)
|
26 |
+
|
27 |
+
img = image.resize((128, 128)) # Resize the image
|
28 |
+
img = np.array(img) / 255.0 # Normalize the image
|
29 |
+
img = np.expand_dims(img, axis=0) # Add batch dimension
|
30 |
+
prediction = model.predict(img)
|
31 |
+
predicted_class = labels[np.argmax(prediction)]
|
32 |
+
|
33 |
+
# Prepare output with probabilities
|
34 |
+
return {labels[i]: float(prediction[0][i]) for i in range(len(labels))}
|
35 |
+
|
36 |
+
# Define the Gradio interface
|
37 |
+
image_input = gr.Image(type="pil") # Use "pil" as the type for PIL images
|
38 |
+
label_output = gr.Label(num_top_classes=4)
|
39 |
+
|
40 |
+
# Launch the interface
|
41 |
+
gr.Interface(fn=classify_image,
|
42 |
+
inputs=image_input,
|
43 |
+
outputs=label_output,
|
44 |
+
title="Satellite Image Classification",
|
45 |
+
description="Classify satellite images into four types: Water, Cloudy, Desert, Green Area").launch()
|