File size: 934 Bytes
0b10c0a 2278f6c 0b10c0a 2278f6c 0b10c0a 2278f6c 0b10c0a 2278f6c 0b10c0a 2278f6c 0b10c0a 2278f6c 0b10c0a 2278f6c 0b10c0a 2278f6c 0b10c0a 2278f6c 0b10c0a 2278f6c | 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 | import json
import numpy as np
import tensorflow as tf
from PIL import Image
import gradio as gr
# Load model
model = tf.keras.models.load_model("animal_cnn.keras")
# Load class names
with open("class_names.json", "r") as f:
class_names = json.load(f)
# Preprocess function
def preprocess_image(image):
image = image.resize((224, 224))
image = np.array(image) / 255.0
image = np.expand_dims(image, axis=0)
return image
# Prediction function
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}%)"
# Gradio UI
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() |