Guest User

Untitled

a guest
Feb 17th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. import numpy as np
  2. import keras.preprocessing.image as image_utils
  3. from keras.applications.imagenet_utils import decode_predictions, preprocess_input
  4. # You can import any other model if you would like to :)
  5. from keras.applications.resnet50 import ResNet50
  6.  
  7. IMG_SIZE = (224, 224)
  8.  
  9. # Loading the model only once
  10. CLASSIFICATION_MODEL = ResNet50()
  11.  
  12.  
  13. def get_top_label(img_path):
  14. """Given an image path, return the top detected label using a ResNet50 model. """
  15. image = image_utils.load_img(img_path, target_size=IMG_SIZE)
  16. image = image_utils.img_to_array(image)
  17. image = np.expand_dims(image, axis=0)
  18. image = preprocess_input(image)
  19. preds = CLASSIFICATION_MODEL.predict(image)
  20. top_label = decode_predictions(preds)[0][0][1]
  21. return top_label
Add Comment
Please, Sign In to add comment