Skip to content

Instantly share code, notes, and snippets.

@omiq
Created June 8, 2018 20:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omiq/41c800353311f25f7c1b08f764aa174a to your computer and use it in GitHub Desktop.
Save omiq/41c800353311f25f7c1b08f764aa174a to your computer and use it in GitHub Desktop.
Image recognition demo. Set filename as first parameter.
import sys
import numpy as np
from keras.preprocessing import image
from keras.applications import resnet50
# Load the Keras image database
model = resnet50.ResNet50()
# Load the picture as 224x224 (maximum size this model can cope with)
picture = image.load_img(sys.argv[1], target_size=(224, 224))
# Convert to image array
x = image.img_to_array(picture)
# Expand as if it is an array of images
x = np.expand_dims(x, axis=0)
# Pre-process to the scale of the trained network
x = resnet50.preprocess_input(x)
# Run the prediction
predictions = model.predict(x)
# Get the classes of the top 10 results
predicted_classes = resnet50.decode_predictions(predictions, top=10)
print("YOUR PICTURE IS OF A:")
for imagenet_id, name, likelihood in predicted_classes[0]:
print(" - {}: {}".format(name, likelihood))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment