-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpredict.py
More file actions
41 lines (35 loc) · 1.11 KB
/
Copy pathpredict.py
File metadata and controls
41 lines (35 loc) · 1.11 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
from keras.models import load_model
from PIL import Image
import glob
import random
import numpy as np
LETTERS = 'abcdefghijklmnopqrstuvwxyz0123456789'
def onehot_to_string(vector):
_1 = vector[0:36]
# _2 = vector[36:72]
# _3 = vector[72:108]
# _4 = vector[108:144]
return LETTERS[list(_1).index(max(_1))] #+ LETTERS[list(_2).index(max(_2))] + LETTERS[list(_3).index(max(_3))] + LETTERS[list(_4).index(max(_4))]
def load_models():
return [load_model('index_{}_model.h5'.format(i)) for i in range(4)]
def predict(image, models):
result = ''
probability = 1.0
probabilities = []
image_np = np.array([np.array(image)])
for model in models:
predictions = model.predict_proba(image_np)[0]
result += onehot_to_string(predictions)
prob = max(predictions)
probability *= prob
probabilities.append(prob)
return result, probability, tuple(probabilities)
def main():
models = load_models()
while True:
test = Image.open(random.choice(glob.glob('tests/*.jpeg')))
test.show()
print predict(test, models)
raw_input("Press enter to continue")
if __name__ == "__main__":
main()