-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmain.py
More file actions
148 lines (110 loc) · 4.79 KB
/
main.py
File metadata and controls
148 lines (110 loc) · 4.79 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
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
'''
@author Andrea Corriga
@contact me@andreacorriga.com
@date 2018
@version 1.0
'''
import time
import argparse
import cv2
from PIL import Image
# Import my alghorithms
from algorithms.LBP import LBP
# Import my utils method
from utils.utils import *
from utils.dataset import *
from sklearn.metrics import accuracy_score #c alculate accuracy
from sklearn.externals import joblib # save and load model
from sklearn.model_selection import train_test_split # in order to split training and test
import numpy
from skimage.feature import local_binary_pattern
# Classifier
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn import svm
def main():
parser = argparse.ArgumentParser(description='')
parser.add_argument('--dataset', dest='dataset', type=str, default='YaleFaces', help='Main folder of the dataset')
parser.add_argument('--classifier', dest='classifier', type=str, default='svm', help='Classifier to use: "svm" or "naivebayes" or "knn"')
parser.add_argument('--training', dest='training', action='store_true', default=False, help='whether or not an output image should be produced')
parser.add_argument('--histEq', dest='histEq', action='store_true', default=False, help='if you want to equialize the histogram before calculating LBP')
parser.add_argument('--output', dest='output', action='store_true', default=False, help='if you want to save the png of LBP image')
arguments = parser.parse_args()
datasetMainFolder = os.getcwd() + "/datasets/"
# Security check about the classifier
if(arguments.classifier != "svm" and arguments.classifier != "naivebayes" and arguments.classifier != "knn"):
print("Classifier not valid. Choose between svm, naivebayes or knn")
return
# Security check about the dataset
if os.path.isdir(datasetMainFolder + arguments.dataset) == False:
print('The Dataset "' + arguments.dataset + '" doesn\'t exist')
return
# Helpful instad of write datasetMainFolder + arguments.dataset + "/"
datasetFolder = datasetMainFolder + arguments.dataset + "/"
# Get Dataset information
classes, filenames, xFilepaths, y = getDataset(arguments.dataset)
x = []
print("Launching LBP on the " + arguments.dataset + " dataset...")
startTime = time.time()
# This counter is used to store the png
counter = 0
# if --output is passed as parameter
if arguments.output == True:
createFolderLBP(arguments.dataset, "LBP" )
for xfp in xFilepaths:
img = imgRead(datasetFolder + xfp)
#imgShow(numpy.matrix(img))
# Check if img exist (security check)
if img:
# if --histEq is passed as parameter, perform an histogram equalization
if(arguments.histEq == True):
img = histogramEqualization(img)
lbp_value = local_binary_pattern(img, 8, 1)
# Split img into 12*12 blocks (image size: 168 * 192)
shaped = blockshaped(lbp_value, 16, 14)
# Split img into 6*6 blocks (image size: 168 * 192)
#shaped = blockshaped(lbp_value, 32, 28)
# Split img into 3*3 blocks (image size: 168 * 192)
#shaped = blockshaped(lbp_value, 64, 56)
# Calculate the histogram for each block
xBlocks = []
for s in shaped:
xBlocks.append(getHistogram(s))
# Concatenate the various histogram, the resulting histogram is append into feature vector
x.append(numpy.concatenate(xBlocks))
# if --output is passed as parameter
if arguments.output == True:
saveImgLBP(getImgObjFromArray(lbp_value), arguments.dataset, filenames[counter], "LBP" )
# If the image doens't exist
else:
print("The image: " + datasetFolder + xfp + " doesn't exist")
# Add counter for new image
counter = counter + 1
print("--- LBP done in %s seconds ---" % (time.time() - startTime))
print("Split dataset into training and test set [0.77] [0.33]")
# Split dataset x (feature vector) and y (label) into training and test set
xTrain, xTest, yTrain, yTest = train_test_split(x, y, test_size=0.33)
print("Launching " + arguments.classifier.upper() + "...")
startTime = time.time()
filename = arguments.classifier + ".pkl"
# if --training is passet as parameter, perform the training of model
if arguments.training == True:
trainingTime = time.time()
if arguments.classifier == "svm":
clf = svm.LinearSVC()
if arguments.classifier == "naivebayes":
clf = GaussianNB()
if arguments.classifier == "knn":
clf = KNeighborsClassifier(n_neighbors=3)
print("Start training...")
clf.fit(xTrain, yTrain)
joblib.dump(clf, 'model/' + filename)
print("--- Training done in %s seconds ---" % (time.time() - trainingTime))
# Test the model
clf = joblib.load('model/' + filename)
print("Start testing...")
predicted = clf.predict(xTest)
print("--- " + arguments.classifier.upper() + " done in %s seconds ---" % (time.time() - startTime))
print("Accuracy: " + str(accuracy_score(yTest, predicted)))
if __name__ == "__main__":
main()