import cv2
import tensorflow as tf
import numpy as np
class ImageCategorizer:
def init(self):
# Load pre-trained model
self.model = tf.keras.models.load_model(“model.h5”)
def categorize(self, image_path):
# Read the image
image = cv2.imread(image_path)
# Pre-process the image
image = cv2.resize(image, (224, 224))
image = np.expand_dims(image, axis=0)
# Make predictions
predictions = self.model.predict(image)
# Get the class with the highest probability
class_idx = np.argmax(predictions[0])
# Map class index to class name
class_mapping = {0: ‘cat’, 1: ‘dog’, 2: ‘car’, …}
class_name = class_mapping[class_idx]
return class_name
categorizer = ImageCategorizer()
print(“The image is of a:”, categorizer.categorize(“image.jpg”))