File size: 916 Bytes
31607dc |
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 |
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
class VGG16Classifier:
def __init__(self):
self.model = keras.applications.VGG16(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000)
def preprocess_image(self, image):
img = keras.preprocessing.image.array_to_img(image)
img = img.resize((224, 224))
img_array = keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0)
img_array = keras.applications.vgg16.preprocess_input(img_array)
return img_array
def classify_image(self, image):
# Preprocess the image
img_array = self.preprocess_image(image)
# Classify the image
predictions = self.model.predict(img_array)
predicted_classes = keras.applications.imagenet_utils.decode_predictions(predictions, top=3)[0]
return predicted_classes |