In [39]:
from __future__ import print_function
import numpy as np
import cv2
import cPickle
import matplotlib.pyplot as plt
from keras.models import model_from_json
import PIL
from PIL import Image
def extractImagesAndLabels(path, file):
f = open(path+file, 'rb')
dict = cPickle.load(f)
images = dict['data']
images = np.reshape(images, (10000, 3, 32, 32))
labels = dict['labels']
return images, labels
def extractCategories(path, file):
f = open(path+file, 'rb')
dict = cPickle.load(f)
return dict['label_names']
def getImage(images, id):
image = images[id]
image = image.transpose([1, 2, 0])
image = image.astype('float32')
image /= 255
return image
def loadModel(json_desc, weights):
# load json and create model
json_file = open(json_desc, 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights(weights)
return loaded_model
def showImage(id):
image = getImage(images,id)
print (image.shape)
%matplotlib inline
imgplot = plt.imshow(image)
labelid = labels[id]
category = categories[labelid]
print("category : "+category)
def predictImage(id):
image = getImage(images, id)
showImage(id)
image = np.expand_dims(image, axis=0)
print (image.shape)
print (image)
result = model.predict(image)
result = result[0].tolist()
best_index=result.index(max(result))
print ("prediction : "+categories[best_index])
i=0
while (i < 10):
print (str(categories[i])+" : "+str(result[i]))
i+=1
model = loadModel('models/allconv-model.json', 'models/allconv-weights-90.hdf5')
images, labels = extractImagesAndLabels("data/CIFAR-10/cifar-10-batches-py/", "test_batch")
categories = extractCategories("data/CIFAR-10/cifar-10-batches-py/", "batches.meta")
print("done")
In [40]:
predictImage(129)
In [73]:
#load image
import urllib
url1="http://www.locationsphotography.co.uk/wp-content/uploads/2017/07/Truck-Professional-Photography-Square-Tile2-400x400.jpg"
url2="http://p3ky.com/wp-content/uploads/2017/11/LWC-Truck_Square.jpg"
url3="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ7a70Fqqx6WaWOEUg1irrgzLQvJX0B7Bx8SQVStKwpjEe7-RVhpg"
url4="https://rlv.zcache.co.uk/semi_truck_cab_square_sticker-rfe992504321541beaefdc3263c2b65d1_v9wf3_8byvr_400.jpg"
url5="https://www.iotsolutionprovider.com/sites/iotsolutionprovider/files/articles/Fleet%20Management_square.jpg"
url6="https://rlv.zcache.com/red_dump_truck_boy_construction_birthday_party_square_sticker-r93c318e9d4f64dc3888bff21f6fa88d6_v9i40_8byvr_400.jpg"
url7="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQaqBfc5b78ED6bM8gN2yn47O1ph6JP5ix8OE82MP8z8klCHAtU"
url8="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQO4SYXHBQOcmWIS3DoQRvBN2EI9V_kGyUz74qs30LwIVqmTTya"
url9="https://www.trossachs-sar.com/pubd/images/square.58d8deb4-RN-Sea-king-6.jpg"
url10="https://www.picclickimg.com/d/l400/pict/123325311402_/New-Fall-Dog-Ride-old-Blue-Truck-wood.jpg"
url11="https://cdn.shopify.com/s/files/1/1132/0194/files/District_Mobile_Slider_truck_400x.jpg?v=1517957865"
url12="https://i.pinimg.com/736x/bb/46/1a/bb461a25fdcb613e88f4b130b17842d4--scottish-terrier-ranges.jpg"
url12="https://www.arlingtondogandcat.com/imagebank/eVetSites/DogCats/052016_EVS_CatDog5.jpg"
url = url12
img = Image.open(urllib.urlopen(url))
img = img.resize((32, 32), PIL.Image.ANTIALIAS)
img = img.convert('RGB')
#img.save('images/test.png', 'PNG')
#img = Image.open( 'images/test.png' )
#img.load()
#show image
%matplotlib inline
imgplot = plt.imshow(img)
#process image data
data = np.reshape(img, (1, 32, 32, 3))
data = data.astype('float32')
data /= 255
#make prediction
result = model.predict(data)
result = result[0].tolist()
best_index=result.index(max(result))
#print result
print ("prediction : "+categories[best_index])
i=0
while (i < 10):
print (str(categories[i])+" : "+str(result[i]))
i+=1
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: