Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import keras.preprocessing.image as image_utils
- from keras.applications.imagenet_utils import decode_predictions, preprocess_input
- # You can import any other model if you would like to :)
- from keras.applications.resnet50 import ResNet50
- IMG_SIZE = (224, 224)
- # Loading the model only once
- CLASSIFICATION_MODEL = ResNet50()
- def get_top_label(img_path):
- """Given an image path, return the top detected label using a ResNet50 model. """
- image = image_utils.load_img(img_path, target_size=IMG_SIZE)
- image = image_utils.img_to_array(image)
- image = np.expand_dims(image, axis=0)
- image = preprocess_input(image)
- preds = CLASSIFICATION_MODEL.predict(image)
- top_label = decode_predictions(preds)[0][0][1]
- return top_label
Add Comment
Please, Sign In to add comment