-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict.py
More file actions
42 lines (32 loc) · 1.01 KB
/
predict.py
File metadata and controls
42 lines (32 loc) · 1.01 KB
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
import os
import numpy as np
import matplotlib.pyplot as plt
import cv2
from keras.preprocessing import image
from keras.models import model_from_json
from scripts.tools import preprocess_img
def load_model(name = 'model'):
'''
Load model
:param name: name of saved model
:return: compiled and trained model
'''
json_file = open(name+'.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)
model.load_weights(name+'.h5')
return model
def predict_image(img, model):
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, dsize=(32, 32))
img = preprocess_img(img)
img = np.expand_dims(img, axis=0)
pred = model.predict(img)
pred = np.argmax(pred)
print("Prediction for {} is {}".format(file, pred))
model = load_model('pretrained/model0.31_0.93')
for r, d, f in os.walk('sample'): #predict 10 images in sample directory
for file in f:
img = cv2.imread("sample/{}".format(file), cv2.IMREAD_COLOR) #load image
predict_image(img, model)