Skip to content

Instantly share code, notes, and snippets.

@PallawiSinghal
Last active March 14, 2019 11:32
Show Gist options
  • Save PallawiSinghal/9e4f5d8e382315d70011a6ba4352a064 to your computer and use it in GitHub Desktop.
Save PallawiSinghal/9e4f5d8e382315d70011a6ba4352a064 to your computer and use it in GitHub Desktop.
# import the necessary packages
from keras.models import load_model
import argparse
import pickle
import cv2
import os
test_image_path = "/test_image/cats.jpg"
model_path = "/simple_multiclass_classifcation_model.model"
label_binarizer_path = "/simple_multiclass_classifcation_lb.pickle"
image = cv2.imread(test_image_path)
output = image.copy()
image = cv2.resize(image, (32,32))
# scale the pixel values to [0, 1]
image = image.astype("float") / 255.0
image = image.flatten()
print ("image after flattening",len(image))
image = image.reshape((1, image.shape[0]))
print ("image--reshape",image.shape)
# load the model and label binarizer
print("[INFO] loading network and label binarizer...")
model = load_model(model_path)
lb = pickle.loads(open(label_binarizer_path, "rb").read())
# # make a prediction on the image
print (image.shape)
preds = model.predict(image)
# find the class label index with the largest corresponding
# probability
print ("preds.argmax(axis=1)",preds.argmax(axis=1))
i = preds.argmax(axis=1)[0]
print (i)
label = lb.classes_[i]
# draw the class label + probability on the output image
text = "{}: {:.2f}%".format(label, preds[0][i] * 100)
cv2.putText(output, text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7,
(0, 0, 255), 2)
# show the output image
cv2.imshow("Image", output)
cv2.waitKey(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment