dogbreeds / app.py
kiki7555's picture
Update app.py
b4ee810 verified
raw
history blame contribute delete
No virus
1.34 kB
import gradio as gr
import tensorflow as tf
from PIL import Image
import numpy as np
# Load the dog breed classifier model
model_path = "best_model.keras"
model = tf.keras.models.load_model(model_path)
labels = ['Beagle', 'French Bulldog', 'German Shepherd', 'Golden Retriever', 'Labrador Retriever']
# Define function for dog breed classification with data augmentation
def preprocess_image(image):
image = Image.fromarray(image.astype('uint8'), 'RGB')
image = image.resize((150, 150))
image = np.array(image)
image = image / 255.0 # Normalization of pixel values
return image
# Prediction function
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
# Create Gradio interface
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)