-
Notifications
You must be signed in to change notification settings - Fork 13
/
model.py
45 lines (39 loc) · 1.47 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import tensorflow as tf
from keras.models import load_model
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.vgg19 import preprocess_input
from keras.applications.vgg19 import decode_predictions
model = tf.keras.models.load_model('./weights/my_model.h5', compile=False)
def process_image(image):
'''
Make an image ready-to-use by VGG19
'''
# convert the image pixels to a numpy array
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 VGG model
image = preprocess_input(image)
return image
def predict_class(image):
'''
Predict and render the class of a given image
'''
# predict the probability across all output classes
yhat = model.predict(image)
# convert the probabilities to class labels
label = decode_predictions(yhat)
# retrieve the most likely result, e.g. highest probability
label = label[0][0]
# return the classification
prediction = label[1]
percentage = '%.2f%%' % (label[2]*100)
return prediction, percentage
if __name__ == '__main__':
''' for test'''
# load an image from file
image = load_img('../image.jpg', target_size=(224, 224))
image = process_image(image)
prediction, percentage = predict_class(image)
print(prediction, percentage)