-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCardDetectionModel.py
75 lines (67 loc) · 2.32 KB
/
CardDetectionModel.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
import numpy as np
from PIL import Image
from keras import models
from keras import layers, losses, preprocessing
import random
import os
import sys
def createModel(shape=(146, 204, 1)):
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation="relu", input_shape=shape))
model.add(layers.MaxPool2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation="relu"))
model.add(layers.MaxPool2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation="relu"))
model.add(layers.MaxPool2D((2, 2)))
model.add(layers.Dropout(0.5))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation="relu"))
model.add(layers.Dense(2, activation="sigmoid"))
model.summary()
model.compile(
optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"]
)
return model
def loadData(filepath=""):
train = []
labels = []
labelClasses = {}
i = 0
for item in os.listdir(filepath):
labelClasses[i] = item
_filepath = filepath + "/" + item + "/"
for imgPath in os.listdir(_filepath):
_img = Image.open(_filepath + imgPath).convert("L")
train.append(np.array(_img) / 255.0)
labels.append(i)
del _img
i += 1
shuffler = np.random.permutation(len(train))
train = np.array(train)
labels = np.array(labels)
train = train[shuffler]
labels = labels[shuffler]
train = train.reshape([-1, 146, 204, 1])
return train, labels, labelClasses
def trainModel(model, train, labels, epochs=10):
history = model.fit(x=train, y=labels, epochs=epochs, validation_split=0.1)
return model, history
def loadModel(filepath=""):
model = models.load_model(filepath)
return model
def loadLabelClasses(filepath=""):
labelClasses = {}
i = 0
for item in os.listdir(filepath):
labelClasses[i] = item
i += 1
return labelClasses
if __name__ == "__main__":
CardDetectionModel = createModel()
filePath = "CardDetectionData"
if "-train" in sys.argv:
train, labels, labelClasses = loadData(filepath="CardDetectionData")
CardDetectionModel, history = trainModel(CardDetectionModel, train, labels)
CardDetectionModel.save("CardDetectionModel")
elif "-load" in sys.argv:
CardDetectionModel = loadModel()