File size: 1,508 Bytes
3887fc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import tensorflow as tf
import gradio as gr
import numpy as np
from tensorflow.keras.preprocessing import image

# Load the model
model = tf.keras.models.load_model('model.keras')

# Define the class labels
class_labels = {
    0: "Buildings",
    1: "Forest",
    2: "Glacier",
    3: "Mountain",
    4: "Sea",
    5: "Street"
}

# Prediction function
def classify_image(img):
    # Resize the image to the input size expected by your model
    img = img.resize((150, 150))  # Replace 150 with your model's input size

    # Convert the image to a numpy array and preprocess it
    img_array = image.img_to_array(img)
    img_array = np.expand_dims(img_array, axis=0)
    img_array = img_array / 255.0  # Normalize if your model expects normalized inputs

    # Make a prediction
    predictions = model.predict(img_array)
    predicted_class = np.argmax(predictions, axis=1)

    # Get the class label from the predicted class index
    predicted_label = class_labels.get(predicted_class[0], "Unknown")

    # Return the predicted label
    return f"Predicted class: {predicted_label}"

# Gradio interface
interface = gr.Interface(
    fn=classify_image,  # Function to call
    inputs=gr.Image(type="pil"),  # Input type (image)
    outputs="text",  # Output type (text)
    title="CNN Image Classification",
    description="Upload an image, and the model will classify it into one of the following classes: Buildings, Forest, Glacier, Mountain, Sea, Street."
)

# Launch the interface
interface.launch()