Clik here to view.

Keras
Keras - Python Deep Learning library provides high level API for deep learning using python. It runs on top of Tensorflow or Theano. Keras takes away the complexities of deep learning models and provides very high level, readable API.Image Classifier / Predictor using Keras
from keras.preprocessing import image
from keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions
model = ResNet50(weights='imagenet')
target_size = (224, 224)
def predict_object(model, img, target_size, top_n=5):
if img.size != target_size:
img = img.resize(target_size)
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
_predictions = model.predict(x)
return decode_predictions(_predictions, top=top_n)[0]
The Image Classifier runs on top of tensorfow and imagenet. The predict_object method takes 3 mandatory arguments,
model - keras model.
img - image object of PIL format.
target_size - tuple of width and height.
top_n - no. of predictions to return. defaults to 5
and returns predicted labels and its probabilities
Putting the image predictor all together.
import sys
import argparse
import numpy as np
from PIL import Image
from io import BytesIO
import requests
from keras.preprocessing import image
from keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions
model = ResNet50(weights='imagenet')
target_size = (224, 224)
def predict_object(model, img, target_size, top_n=5):
if img.size != target_size:
img = img.resize(target_size)
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
_predictions = model.predict(x)
return decode_predictions(_predictions, top=top_n)[0]
if __name__=="__main__":
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument("--image", help="path to image")
arg_parser.add_argument("--link", help="url to image")
args = arg_parser.parse_args()
if args.image is None and args.link is None:
arg_parser.print_help()
sys.exit(1)
if args.image is not None:
img = Image.open(args.image)
predictions = predict_object(model, img, target_size)
print(predictions)
if args.link is not None:
response = requests.get(args.link)
img = Image.open(BytesIO(response.content))
predictions = predict_object(model, img, target_size)
print (predictions)
Install Dependencies
Install Keras
Install Tensorflow
Install the below packages via pip
pip install requests Pillow h5py
Running the Image Classifier
python object-classy.py --image /path/to/image.png
python object-classy.py --link http://path/to/image.png