-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCharacterRecognition.py
156 lines (129 loc) · 6.11 KB
/
CharacterRecognition.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
import numpy
import argparse
from sklearn import svm, tree
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix, accuracy_score
from collections import Counter
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
class CharacterRecognition:
def __init__(self, training, test):
self.training = training[: -4]
self.test = test[: -4]
def parseFile(self, training, test):
with open(training, 'r') as trainingFile, open(test, 'r') as testFile:
trainingLabels = []
trainingChars = []
trainingData = numpy.loadtxt(trainingFile)
for line in trainingData:
trainingLabels.append(line[len(trainingData[0]) - 1])
trainingChars.append(line[: - 1])
numpy.savetxt(self.training + '_label.txt', trainingLabels)
numpy.savetxt(self.training + '_chars.txt', trainingChars)
testLabels = []
testChars = []
testData = numpy.loadtxt(testFile)
for line in testData:
testLabels.append(line[len(testData[0]) - 1])
testChars.append(line[: - 1])
numpy.savetxt(self.test + '_label.txt', testLabels)
numpy.savetxt(self.test + '_chars.txt', testChars)
def fileToMatrix(self, file):
with open(file) as file:
data = numpy.loadtxt(file)
return data
def svm(self, training_labels, training_chars, test_chars):
classifier = svm.SVC(kernel='poly', C=0.5)
classifier.fit(training_chars, training_labels)
return classifier.predict(test_chars)
def decisionTree(self, training_chars, training_labels, test_chars):
classifier = tree.DecisionTreeClassifier()
classifier.fit(training_chars, training_labels)
return classifier.predict(test_chars)
def knn(self, training_labels, training_chars, test_chars):
classifier = KNeighborsClassifier(n_neighbors=args.k)
classifier.fit(training_chars, training_labels)
return classifier.predict(test_chars)
def randomForest(self, training_chars, training_labels, test_chars):
classifier = RandomForestClassifier()
classifier.fit(training_chars, training_labels)
print('RandomForest Importances')
print(classifier.feature_importances_)
return classifier.predict(test_chars)
def adaBoost(self, training_chars, training_labels, test_chars):
classifier = AdaBoostClassifier()
classifier.fit(training_chars, training_labels)
return classifier.predict(test_chars)
def confusionMatrix(self, data, labels):
return confusion_matrix(data, labels)
def vote(self, knn, svm, tree, randomForest):
result = []
for i in zip(knn, svm, tree, randomForest):
result.append(Counter(i).most_common(1)[0][0])
return result
def countClasses(self, matrix):
classesNumber = 0
for i in range(25):
for j in range(25):
print('[', end='')
print(matrix[i][j], end='')
print(']', end='')
print('\t\t Class = ' + repr(classesNumber) + ' ')
classesNumber += 1
if __name__ == '__main__':
args = argparse.ArgumentParser()
args.add_argument("trainingFile", help="Training file")
args.add_argument("testFile", help="Test file")
args.add_argument("k", type=int, help="k neighborhoods")
args = args.parse_args()
charsRec = CharacterRecognition(args.trainingFile, args.testFile)
charsRec.parseFile(args.trainingFile, args.testFile)
trainingLabelsFile = args.trainingFile[: - 4] + '_label.txt'
trainingLabels = charsRec.fileToMatrix(trainingLabelsFile)
trainingCharsFile = args.trainingFile[: - 4] + '_chars.txt'
trainingChars = charsRec.fileToMatrix(trainingCharsFile)
testLabelsFile = args.testFile[: - 4] + '_label.txt'
testLabels = charsRec.fileToMatrix(testLabelsFile)
testCharsFile = args.testFile[: - 4] + '_chars.txt'
testChars = charsRec.fileToMatrix(testCharsFile)
_svm = charsRec.svm(trainingLabels, trainingChars, testChars)
knn = charsRec.knn(trainingLabels, trainingChars, testChars)
decisionTree = charsRec.decisionTree(trainingChars, trainingLabels, testChars)
randomForest = charsRec.randomForest(trainingChars, trainingLabels, testChars)
adaBoost = charsRec.adaBoost(trainingChars, trainingLabels, testChars)
vote = charsRec.vote(knn, _svm, decisionTree, randomForest)
print('vote')
matrixVote = charsRec.confusionMatrix(vote, testLabels)
charsRec.countClasses(matrixVote)
accuracy = accuracy_score(testLabels, vote) * 100
print('Accuracy = ' + repr(accuracy) + '%\n')
print('Decition Tree')
print(repr(decisionTree))
matrixDecisionTree = charsRec.confusionMatrix(decisionTree, testLabels)
charsRec.countClasses(matrixDecisionTree)
decisionTreeAccuracy = accuracy_score(testLabels, decisionTree) * 100
print('Accuracy = ' + repr(decisionTreeAccuracy) + '%\n')
print('SVM')
print(repr(_svm))
matrixSvm = charsRec.confusionMatrix(_svm, testLabels)
charsRec.countClasses(matrixSvm)
svmTreeAccuracy = accuracy_score(testLabels, _svm) * 100
print('Accuracy = ' + repr(svmTreeAccuracy) + '%\n')
print('knn k = ', end='')
print(args.k)
print(repr(knn))
matrixKnn = charsRec.confusionMatrix(knn, testLabels)
charsRec.countClasses(matrixKnn)
accuracyKnn = accuracy_score(testLabels, knn) * 100
print('Accuracy = ' + repr(accuracyKnn) + '%\n')
print('RandomForest')
print(repr(randomForest))
matrixRandomForest = charsRec.confusionMatrix(randomForest, testLabels)
charsRec.countClasses(matrixRandomForest)
accuracyRandomForest = accuracy_score(testLabels, randomForest) * 100
print('Accuracy = ' + repr(accuracyRandomForest) + '%\n')
print('AdaBoost')
print(repr(adaBoost))
matrixAdaBoost = charsRec.confusionMatrix(adaBoost, testLabels)
charsRec.countClasses(matrixAdaBoost)
accuracyAdaBoost = accuracy_score(testLabels, adaBoost) * 100
print('Accuracy = ' + repr(accuracyAdaBoost) + '%\n')