Skip to content

Instantly share code, notes, and snippets.

@rizalespe
Last active January 24, 2019 06:02
Show Gist options
  • Save rizalespe/55d3abdd0cbef60e927672cd88baeedb to your computer and use it in GitHub Desktop.
Save rizalespe/55d3abdd0cbef60e927672cd88baeedb to your computer and use it in GitHub Desktop.
# importing all required package library
from keras.applications import ResNet50
from keras.preprocessing.image import img_to_array
from keras.applications import imagenet_utils
import numpy as np
# A class for image prediction based on ResNet pre-trained model
class ResNetPredict:
def __init__(self):
self.image = None
self.model = None
self.result_value = None
def load_model(self):
self.model = ResNet50(weights="imagenet")
def prepare_image(self, image, target):
if image.mode != "RGB":
image = image.convert("RGB")
image = image.resize(target)
image = img_to_array(image)
image = np.expand_dims(image, axis=0)
image = imagenet_utils.preprocess_input(image)
return image
def predict(self, image):
prediction = self.model.predict(image)
results = imagenet_utils.decode_predictions(prediction)
return results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment