File size: 1,493 Bytes
174be90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
44
import gradio as gr
import tensorflow as tf
import numpy as np
from tensorflow.keras.preprocessing import image
from PIL import Image

# Load the trained model
MODEL_PATH = "setosys_dogs_model.h5"
model = tf.keras.models.load_model(MODEL_PATH)

# Define image preprocessing function
def preprocess_image(img):
    img = img.resize((224, 224))  # Resize image to model input size
    img_array = image.img_to_array(img)
    img_array = np.expand_dims(img_array, axis=0)  # Add batch dimension
    img_array = img_array / 255.0  # Normalize pixel values
    return img_array

# Define the prediction function
def predict_dog_breed(img):
    img_array = preprocess_image(img)
    predictions = model.predict(img_array)
    class_idx = np.argmax(predictions)  # Get class index with highest probability
    confidence = float(np.max(predictions))  # Get confidence score

    # Define your class labels (replace with your actual class names)
    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}

# Create a Gradio interface
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.",
)

# Launch the Gradio app
if __name__ == "__main__":
    interface.launch()