def classify_random_image(): from PIL import Image import random import imageio image_path = filename[random.randrange(0,len(filename),1)][0] image = Image.open(image_path) image.show() my_image = imageio.imread(image_path) my_image = np.expand_dims(my_image, axis = 0) my_image = my_image/255. my_image_prediction = round(shipModel.predict(my_image)[0,0]) print(class_dict[my_image_prediction])
def predict(): model = build_model() model.load_weights('aug_model.h5') image = load_img('fedex-logo.jpg', target_size=(224, 224)) image.show() image = img_to_array(image) # reshape data for the model #image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) # prepare the image for the ResNet50 model image = np.expand_dims(image, axis=0) image = preprocess_input(image) # predict the probability across all output classes yhat = model.predict(image) print(yhat) max_val = np.argmax(yhat) if yhat[0][max_val] * 100 >= 70: print('%s (%.2f%%)' % (parse_predictions[max_val], yhat[0][max_val] * 100)) else: print("no logo detected")
''' Part e ''' path = 'dataset/single_prediction/diseased_or_healthy_2.jpg' test_image = image.load_img(path, target_size=(128,128)) #Dimensions already specified # Now since the data that we need to have should be a 3D, 3rd being the 3 for RGB test_image = image.img_to_array(test_image) # The data for the prediction needs to be 4D. 4th being the batch. Even though it is single but predict function # expects the data to be in form of batches test_image = np.expand_dims(test_image, axis=0) result = classifier.predict(test_image) # training_set.class_indices can be used to check the category encoding animal = "" if result==0: animal = "Diseased" else: animal = "Healthy" from PIL import Image, ImageDraw, ImageFont image = Image.open(path) font_type = ImageFont.truetype('arial.ttf',30) draw = ImageDraw.Draw(image) draw.text(xy=(0,0),text=animal,fill=(0,0,0),font=font_type) image.show() """ To improve accuracy, one more convulation layer can be added with either same or different parameters. Add it after the max pooling and before the flattening. Next layer won't be having input_shape parameter. Keras will automatically accept the pool of previous layer."""
model.summary() # Start Fine-tuning # Start Fine-tuning model.fit(X_train, Y_train,batch_size=batch_size,epochs=nb_epoch,shuffle=True,verbose=1,validation_data=(X_valid, Y_valid)) # Make predictions predictions_valid = model.predict(X_valid, batch_size=batch_size, verbose=1) # Cross-entropy loss score score = log_loss(Y_valid, predictions_valid) ''' #https://machinelearningmastery.com/use-pre-trained-vgg-model-classify-objects-photographs/ ''' from keras.preprocessing.image import load_img from keras.preprocessing.image import img_to_array from keras.applications.vgg16 import preprocess_input from keras.applications.vgg16 import decode_predictions from keras.applications.vgg16 import VGG16 # load the model model = VGG16() # load an image from file #image_path = "/home/terrence/Desktop/PHONE/Camera/" #x = '20140218_170332' #print(image_data(x)) image = load_img('/home/terrence/Desktop/PHONE/Camera/20140218_170332.jpg', target_size=(224, 224)) image.show() # convert the image pixels to a numpy array
from keras.models import load_model from keras.preprocessing.image import load_img, img_to_array from numpy import array, expand_dims from keras.preprocessing import image #Loading Our Traied Model model = load_model("face_actor_VGG_TL.h5") # load an image from file testing_image = "./data/httpabsolumentgratuitfreefrimagesbenaffleckjpg.jpg" image = load_img(testing_image, target_size=(224, 224)) #Showing the image image.show(testing_image) # Convert to array and make 4D image = array(image) image = expand_dims(image, axis=0) #Decoding Predictions if model.predict(image)[0][0] > 0.9: print("BEN_AFFLEK") if model.predict(image)[0][1] > 0.9: print("ELTON_JOHN") if model.predict(image)[0][2] > 0.9: print("JERRY_SEINFELD") if model.predict(image)[0][3] > 0.9: print("MADONNA") if model.predict(image)[0][4] > 0.9:
def affiche_image(rawda): data_set = ImageDataSet(filepath="data/01_raw/elephant.jpg") image = data_set.load() image.show()
def show(image): image = image[0] * 255 image = image.astype(np.uint8) image = Image.fromarray(image) image.show()