|
import gradio as gr |
|
import tensorflow as tf |
|
import numpy as np |
|
from tensorflow.keras.preprocessing import image |
|
from PIL import Image |
|
|
|
|
|
MODEL_PATH = "setosys_dogs_model.h5" |
|
model = tf.keras.models.load_model(MODEL_PATH) |
|
|
|
|
|
def preprocess_image(img): |
|
img = img.resize((224, 224)) |
|
img_array = image.img_to_array(img) |
|
img_array = np.expand_dims(img_array, axis=0) |
|
img_array = img_array / 255.0 |
|
return img_array |
|
|
|
|
|
def predict_dog_breed(img): |
|
img_array = preprocess_image(img) |
|
predictions = model.predict(img_array) |
|
class_idx = np.argmax(predictions) |
|
confidence = float(np.max(predictions)) |
|
|
|
|
|
class_labels = ["Labrador Retriever", "German Shepherd", "Golden Retriever", "Bulldog", "Poodle"] |
|
predicted_breed = class_labels[class_idx] if class_idx < len(class_labels) else "Unknown" |
|
|
|
return {predicted_breed: confidence} |
|
|
|
|
|
interface = gr.Interface( |
|
fn=predict_dog_breed, |
|
inputs=gr.Image(type="pil"), |
|
outputs=gr.Label(), |
|
title="Dog Breed Classifier", |
|
description="Upload an image of a dog to predict its breed.", |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
interface.launch() |
|
|