|
import gradio as gr |
|
import tensorflow as tf |
|
from PIL import Image |
|
import numpy as np |
|
|
|
|
|
model_path = "best_model.keras" |
|
model = tf.keras.models.load_model(model_path) |
|
labels = ['Beagle', 'French Bulldog', 'German Shepherd', 'Golden Retriever', 'Labrador Retriever'] |
|
|
|
|
|
def preprocess_image(image): |
|
image = Image.fromarray(image.astype('uint8'), 'RGB') |
|
image = image.resize((150, 150)) |
|
image = np.array(image) |
|
image = image / 255.0 |
|
return image |
|
|
|
|
|
def predict_dog_breed(image): |
|
image = preprocess_image(image) |
|
prediction = model.predict(np.expand_dims(image, axis=0)) |
|
predicted_class = labels[np.argmax(prediction)] |
|
confidence = np.round(np.max(prediction) * 100, 2) |
|
result = f"{predicted_class}, Confidence: {confidence}%" |
|
return result |
|
|
|
|
|
input_image = gr.Image() |
|
output_text = gr.Textbox(label="Dog Breed") |
|
interface = gr.Interface(fn=predict_dog_breed, inputs=input_image, outputs=output_text, |
|
description="A dog breed classifier using a custom CNN model with data augmentation.", |
|
theme="default") |
|
|
|
if __name__ == "__main__": |
|
interface.launch(share=True) |