Update app.py
Browse files
app.py
CHANGED
@@ -8,34 +8,41 @@ from PIL import Image
|
|
8 |
MODEL_PATH = "setosys_dogs_model.h5"
|
9 |
model = tf.keras.models.load_model(MODEL_PATH)
|
10 |
|
11 |
-
#
|
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
|
17 |
return img_array
|
18 |
|
19 |
-
#
|
20 |
-
def predict_dog_breed(img):
|
|
|
21 |
img_array = preprocess_image(img)
|
22 |
predictions = model.predict(img_array)
|
23 |
-
class_idx = np.argmax(predictions) #
|
24 |
-
confidence = float(np.max(predictions)) #
|
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
|
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
|
|
|
8 |
MODEL_PATH = "setosys_dogs_model.h5"
|
9 |
model = tf.keras.models.load_model(MODEL_PATH)
|
10 |
|
11 |
+
# Image preprocessing function
|
12 |
+
def preprocess_image(img: Image.Image) -> np.ndarray:
|
13 |
+
"""Preprocess the image to match the model's input requirements."""
|
14 |
img = img.resize((224, 224)) # Resize image to model input size
|
15 |
img_array = image.img_to_array(img)
|
16 |
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
17 |
+
img_array /= 255.0 # Normalize pixel values
|
18 |
return img_array
|
19 |
|
20 |
+
# Prediction function
|
21 |
+
def predict_dog_breed(img: Image.Image) -> dict:
|
22 |
+
"""Predict the breed of the dog in the uploaded image."""
|
23 |
img_array = preprocess_image(img)
|
24 |
predictions = model.predict(img_array)
|
25 |
+
class_idx = np.argmax(predictions) # Index of the highest prediction probability
|
26 |
+
confidence = float(np.max(predictions)) # Confidence score
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
# Get class labels from the model
|
29 |
+
class_labels = model.classes_ if hasattr(model, 'classes_') else None
|
30 |
+
|
31 |
+
# If class labels are available, return the predicted breed with confidence score
|
32 |
+
if class_labels is not None:
|
33 |
+
predicted_breed = class_labels[class_idx]
|
34 |
+
else:
|
35 |
+
predicted_breed = "Unknown"
|
36 |
+
|
37 |
return {predicted_breed: confidence}
|
38 |
|
39 |
+
# Create Gradio interface
|
40 |
interface = gr.Interface(
|
41 |
fn=predict_dog_breed,
|
42 |
inputs=gr.Image(type="pil"),
|
43 |
outputs=gr.Label(),
|
44 |
title="Dog Breed Classifier",
|
45 |
+
description="Upload an image of a dog to predict its breed."
|
46 |
)
|
47 |
|
48 |
# Launch the Gradio app
|