File size: 2,137 Bytes
06eb4c6
538d369
 
 
 
a07f3ca
538d369
 
 
 
 
 
 
 
 
 
 
a07f3ca
06eb4c6
538d369
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
import gradio as gr
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import load_img, img_to_array

# Load the trained model
MODEL_PATH = "best_model.weights.h5"
model = load_model(MODEL_PATH)

# Define the class names
class_names = [
    "Bear", "Bird", "Cat", "Cow", "Deer", 
    "Dog", "Dolphin", "Elephant", "Giraffe", 
    "Horse", "Kangaroo", "Lion", "Panda", 
    "Tiger", "Zebra"
]

def classify_image(image):
    img = image.resize((256, 256))
    img_array = img_to_array(img) / 255.0
    img_array = np.expand_dims(img_array, axis=0)
    predictions = model.predict(img_array)
    predicted_class = class_names[np.argmax(predictions)]
    return f"Predicted Class: {predicted_class}"

def instruction():
    return (
        "**Important Note:**\n\n"
        "This model is specifically trained to classify images into the following **15 animal categories**:\n\n"
        "- Bear\n"
        "- Bird\n"
        "- Cat\n"
        "- Cow\n"
        "- Deer\n"
        "- Dog\n"
        "- Dolphin\n"
        "- Elephant\n"
        "- Giraffe\n"
        "- Horse\n"
        "- Kangaroo\n"
        "- Lion\n"
        "- Panda\n"
        "- Tiger\n"
        "- Zebra\n\n"
        "**Usage Limitation:**\n\n"
        "- The model will only recognize images containing these animals.\n"
        "- Uploading an image of an animal not listed above or a non-animal image may result in inaccurate or undefined predictions.\n\n"
        "Ensure the uploaded image is clear, contains a single animal, and resembles the categories listed for the best results."
    )

# Gradio Interface
with gr.Blocks() as app:
    gr.Markdown("# Animal Classifier")
    gr.Markdown(instruction())

    with gr.Row():
        with gr.Column():
            image_input = gr.Image(label="Upload an Image", type="pil")
            predict_button = gr.Button("Classify Image")
        with gr.Column():
            result_output = gr.Textbox(label="Prediction Result", lines=3)

    predict_button.click(classify_image, inputs=image_input, outputs=result_output)

app.launch()