Skip to content

Instantly share code, notes, and snippets.

@SmiffyKMc
Created June 17, 2022 19:44
Show Gist options
  • Save SmiffyKMc/99cf1ae2d21ec9dee557072db8fbcb87 to your computer and use it in GitHub Desktop.
Save SmiffyKMc/99cf1ae2d21ec9dee557072db8fbcb87 to your computer and use it in GitHub Desktop.
Function to test predictions
from PIL import Image
import tensorflow as tf
from tensorflow import keras
from keras import layers
import os
import matplotlib.pyplot as plt
import numpy as np
def download_and_predict(url, filename):
test_model = keras.models.load_model(f"{hotDogDir}hotdog_classifier_v4.keras")
# download the image from the url and save
os.system("curl -s {} -o {}".format(url, filename))
# open the image
img = Image.open(filename)
# save the image
img.save(filename)
# convert it to RGB
img = img.convert('RGB')
# show image
plt.imshow(img)
plt.axis('off')
# resize the image for VGG16 model
img = img.resize((256, 256))
# calculate probabilities of images if they are hotdogs or not
img_test = tf.expand_dims(np.array(img), axis=0)
predicted = test_model.predict(img_test)[0][0]
if predicted > 0.8:
plt.title(f"predicted : Not Hot Dog")
else:
plt.title(f"predicted : Hot Dog")
plt.axis('off')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment