-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCard_Predictor.py
162 lines (145 loc) · 2.81 KB
/
Card_Predictor.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import keras
from keras.preprocessing import image
from keras.applications.xception import (Xception, preprocess_input, decode_predictions)
from keras import models
from keras.models import load_model
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
import numpy as np
import pandas as pd
import os
from os import listdir
from PIL import Image
class_list = ['1DGE',
'1DGF',
'1DGS',
'1DPE',
'1DPF',
'1DPS',
'1DRE',
'1DRF',
'1DRS',
'1OGE',
'1OGF',
'1OGS',
'1OPE',
'1OPF',
'1OPS',
'1ORE',
'1ORF',
'1ORS',
'1SGE',
'1SGF',
'1SGS',
'1SPE',
'1SPF',
'1SPS',
'1SRE',
'1SRF',
'1SRS',
'2DGE',
'2DGF',
'2DGS',
'2DPE',
'2DPF',
'2DPS',
'2DRE',
'2DRF',
'2DRS',
'2OGE',
'2OGF',
'2OGS',
'2OPE',
'2OPF',
'2OPS',
'2ORE',
'2ORF',
'2ORS',
'2SGE',
'2SGF',
'2SGS',
'2SPE',
'2SPF',
'2SPS',
'2SRE',
'2SRF',
'2SRS',
'3DGE',
'3DGF',
'3DGS',
'3DPE',
'3DPF',
'3DPS',
'3DRE',
'3DRF',
'3DRS',
'3OGE',
'3OGF',
'3OGS',
'3OPE',
'3OPF',
'3OPS',
'3ORE',
'3ORF',
'3ORS',
'3SGE',
'3SGF',
'3SGS',
'3SPE',
'3SPF',
'3SPS',
'3SRE',
'3SRF',
'3SRS']
model_path = 'static/model/final.h5'
image_size = (150, 150)
folderpath = 'static/images/cropped'
#Create Model and Load Weights
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(150, 150, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(81))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.load_weights(model_path)
# Predict the image
def predictor(image_path):
img = image.load_img(image_path, target_size=image_size)
x = image.img_to_array(img)
# print(x.shape)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
predictions = model.predict(x)
# print(max(predictions[0]))
list_a = predictions[0]
list_b = class_list
zip_test = list(zip(list_a, list_b))
return max(zip_test)
def find_cropped_images(folderpath):
images = [folderpath + '/' + v for v in os.listdir(folderpath) if v != '.DS_Store']
return images
def loadImages(imagepath):
imagesList = imagepath
loadedImages = []
for image in imagesList:
img = Image.open(image)
loadedImages.append(img)
return loadedImages
def prediction_tuples():
predictions = [predictor(png) for png in find_cropped_images(folderpath)]
return predictions
print(prediction_tuples())