Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
from tensorflow.keras.preprocessing import image
|
5 |
+
|
6 |
+
# Load the model
|
7 |
+
model = tf.keras.models.load_model('model.keras')
|
8 |
+
|
9 |
+
# Define the class labels
|
10 |
+
class_labels = {
|
11 |
+
0: "Buildings",
|
12 |
+
1: "Forest",
|
13 |
+
2: "Glacier",
|
14 |
+
3: "Mountain",
|
15 |
+
4: "Sea",
|
16 |
+
5: "Street"
|
17 |
+
}
|
18 |
+
|
19 |
+
# Prediction function
|
20 |
+
def classify_image(img):
|
21 |
+
# Resize the image to the input size expected by your model
|
22 |
+
img = img.resize((150, 150)) # Replace 150 with your model's input size
|
23 |
+
|
24 |
+
# Convert the image to a numpy array and preprocess it
|
25 |
+
img_array = image.img_to_array(img)
|
26 |
+
img_array = np.expand_dims(img_array, axis=0)
|
27 |
+
img_array = img_array / 255.0 # Normalize if your model expects normalized inputs
|
28 |
+
|
29 |
+
# Make a prediction
|
30 |
+
predictions = model.predict(img_array)
|
31 |
+
predicted_class = np.argmax(predictions, axis=1)
|
32 |
+
|
33 |
+
# Get the class label from the predicted class index
|
34 |
+
predicted_label = class_labels.get(predicted_class[0], "Unknown")
|
35 |
+
|
36 |
+
# Return the predicted label
|
37 |
+
return f"Predicted class: {predicted_label}"
|
38 |
+
|
39 |
+
# Gradio interface
|
40 |
+
interface = gr.Interface(
|
41 |
+
fn=classify_image, # Function to call
|
42 |
+
inputs=gr.Image(type="pil"), # Input type (image)
|
43 |
+
outputs="text", # Output type (text)
|
44 |
+
title="CNN Image Classification",
|
45 |
+
description="Upload an image, and the model will classify it into one of the following classes: Buildings, Forest, Glacier, Mountain, Sea, Street."
|
46 |
+
)
|
47 |
+
|
48 |
+
# Launch the interface
|
49 |
+
interface.launch()
|