|
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): |
|
|
|
|
|
img_array = self.preprocess_image(image) |
|
|
|
|
|
predictions = self.model.predict(img_array) |
|
predicted_classes = keras.applications.imagenet_utils.decode_predictions(predictions, top=3)[0] |
|
|
|
return predicted_classes |