Keras
CDC-Processing / app.py
nxt-shreyash's picture
Create app.py
949bf24 verified
Raw History Blame Contribute Delete
1.3 kB
import numpy as np
from keras.models import load_model
from PIL import Image
import tensorflow as tf
# Load the model
model = load_model("model_img_pro.keras") # Update with your actual model path
# Function to preprocess the image
def preprocess_image(image_path, target_size):
# Load and resize the image
image = Image.open(image_path).convert('RGB')
image = image.resize(target_size)
# Convert image to array and normalize
image_array = np.array(image) / 255.0
image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
return image_array
# Define target size based on your model
target_size = (224, 224) # Example size; update based on your model's expected input
# Path to the image you want to classify
image_path = "path_to_your_image.jpg" # Update with your image path
# Preprocess the image
processed_image = preprocess_image(image_path, target_size)
# Make predictions
predictions = model.predict(processed_image)
# Print the predictions
print(predictions)
# Optionally, decode the predictions if needed
# For binary classification (cat vs dog), you might have a single output neuron
# Example: if predictions[0][0] > 0.5, it's a dog; otherwise, it's a cat
if predictions[0][0] > 0.5:
print("Dog")
else:
print("Cat")