vaishanthr/Image-Classifier-TensorFlow
1
1import tensorflow as tf2from tensorflow import keras3from tensorflow.keras import layers4 5class VGG16Classifier:6 def __init__(self):7 self.model = keras.applications.VGG16(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000)8 9 def preprocess_image(self, image):10 img = keras.preprocessing.image.array_to_img(image)11 img = img.resize((224, 224))12 img_array = keras.preprocessing.image.img_to_array(img)13 img_array = tf.expand_dims(img_array, 0)14 img_array = keras.applications.vgg16.preprocess_input(img_array)15 return img_array16 17 def classify_image(self, image):18 19 # Preprocess the image20 img_array = self.preprocess_image(image)21 22 # Classify the image23 predictions = self.model.predict(img_array)24 predicted_classes = keras.applications.imagenet_utils.decode_predictions(predictions, top=3)[0]25 26 return predicted_classes