catandog / predictor.py
okeowo1014's picture
Upload 3 files
e1ede78 verified
raw
history blame contribute delete
No virus
1.26 kB
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]}")