Spaces:
Runtime error
Runtime error
File size: 1,259 Bytes
e1ede78 |
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 |
import cv2 # Assuming you have OpenCV installed
import numpy as np
from tensorflow.keras.preprocessing import image
import tensorflow as tf
# Load the saved model
model = tf.keras.models.load_model('cat_dog_classifier.keras') # Replace with your model filename
img_width, img_height = 224, 224 # VGG16 expects these dimensions
# Function to preprocess an image for prediction
def preprocess_image(img_path):
img = cv2.imread(img_path) # Read the image
img = cv2.resize(img, (img_width, img_height)) # Resize according to model input size
img = img.astype('float32') / 255.0 # Normalize pixel values
img = np.expand_dims(img, axis=0) # Add a batch dimension (model expects batch of images)
return img
# Get the path to your new image
new_image_path = 'test1/11.jpg' # Replace with your image path
# Preprocess the image
preprocessed_image = preprocess_image(new_image_path)
# Make prediction
prediction = model.predict(preprocessed_image)
# Decode the prediction (assuming class 0 is cat, 1 is dog)
predicted_class = int(prediction[0][0] > 0.5) # Threshold of 0.5 for binary classification
class_names = ['cat', 'dog'] # Adjust class names according to your model
print(f"Predicted class: {class_names[predicted_class]}")
|