| import json |
| import numpy as np |
| import tensorflow as tf |
| from PIL import Image |
| import gradio as gr |
|
|
| |
| model = tf.keras.models.load_model("animal_cnn.keras") |
|
|
| |
| with open("class_names.json", "r") as f: |
| class_names = json.load(f) |
|
|
| |
| def preprocess_image(image): |
| image = image.resize((224, 224)) |
| image = np.array(image) / 255.0 |
| image = np.expand_dims(image, axis=0) |
| return image |
|
|
| |
| def predict(image): |
| image = preprocess_image(image) |
| predictions = model.predict(image)[0] |
|
|
| top_index = np.argmax(predictions) |
| confidence = float(predictions[top_index]) |
|
|
| return f"{class_names[top_index]} ({confidence*100:.2f}%)" |
|
|
| |
| interface = gr.Interface( |
| fn=predict, |
| inputs=gr.Image(type="pil"), |
| outputs="text", |
| title="🐾 Animal Classifier", |
| description="Upload an image to detect the animal" |
| ) |
|
|
| interface.launch() |