Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
+
from tensorflow.keras.preprocessing import image
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
# Load the trained model
|
8 |
+
MODEL_PATH = "setosys_dogs_model.h5"
|
9 |
+
model = tf.keras.models.load_model(MODEL_PATH)
|
10 |
+
|
11 |
+
# Define image preprocessing function
|
12 |
+
def preprocess_image(img):
|
13 |
+
img = img.resize((224, 224)) # Resize image to model input size
|
14 |
+
img_array = image.img_to_array(img)
|
15 |
+
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
16 |
+
img_array = img_array / 255.0 # Normalize pixel values
|
17 |
+
return img_array
|
18 |
+
|
19 |
+
# Define the prediction function
|
20 |
+
def predict_dog_breed(img):
|
21 |
+
img_array = preprocess_image(img)
|
22 |
+
predictions = model.predict(img_array)
|
23 |
+
class_idx = np.argmax(predictions) # Get class index with highest probability
|
24 |
+
confidence = float(np.max(predictions)) # Get confidence score
|
25 |
+
|
26 |
+
# Define your class labels (replace with your actual class names)
|
27 |
+
class_labels = ["Labrador Retriever", "German Shepherd", "Golden Retriever", "Bulldog", "Poodle"]
|
28 |
+
predicted_breed = class_labels[class_idx] if class_idx < len(class_labels) else "Unknown"
|
29 |
+
|
30 |
+
return {predicted_breed: confidence}
|
31 |
+
|
32 |
+
# Create a Gradio interface
|
33 |
+
interface = gr.Interface(
|
34 |
+
fn=predict_dog_breed,
|
35 |
+
inputs=gr.Image(type="pil"),
|
36 |
+
outputs=gr.Label(),
|
37 |
+
title="Dog Breed Classifier",
|
38 |
+
description="Upload an image of a dog to predict its breed.",
|
39 |
+
)
|
40 |
+
|
41 |
+
# Launch the Gradio app
|
42 |
+
if __name__ == "__main__":
|
43 |
+
interface.launch()
|