|
from tensorflow.keras.models import load_model |
|
from tensorflow.keras.preprocessing.image import load_img, img_to_array |
|
from tensorflow.keras.applications.efficientnet_v2 import preprocess_input |
|
import numpy as np |
|
from google.colab import files |
|
|
|
|
|
uploaded = files.upload() |
|
|
|
|
|
MODEL_PATH = "setosys_dogs_model.h5" |
|
model = load_model(MODEL_PATH) |
|
|
|
|
|
|
|
class_labels = ["Labrador Retriever", "German Shepherd", "Golden Retriever", "Bulldog", "Poodle"] |
|
|
|
|
|
def preprocess_image(image_path): |
|
"""Preprocess the image to match the model's input requirements.""" |
|
img = load_img(image_path, target_size=(224, 224)) |
|
img_array = img_to_array(img) |
|
img_array = preprocess_input(img_array) |
|
img_array = np.expand_dims(img_array, axis=0) |
|
return img_array |
|
|
|
|
|
def predict_dog_breed(image_path): |
|
"""Predict the breed of the dog in the uploaded image.""" |
|
img_array = preprocess_image(image_path) |
|
predictions = model.predict(img_array) |
|
|
|
|
|
print("Predictions Shape:", predictions.shape) |
|
|
|
class_idx = np.argmax(predictions) |
|
confidence = float(np.max(predictions)) |
|
|
|
|
|
predicted_breed = class_labels[class_idx] if class_idx < len(class_labels) else "Unknown" |
|
|
|
return predicted_breed, confidence |
|
|
|
|
|
uploaded_image = files.upload() |
|
image_path = list(uploaded_image.keys())[0] |
|
|
|
|
|
breed, confidence = predict_dog_breed(image_path) |
|
print(f"Predicted Breed: {breed}, Confidence: {confidence:.2f}") |
|
|