-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain_predict.py
218 lines (171 loc) · 7.09 KB
/
train_predict.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import os
import itertools
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import extract
import categorize
X_baseline_angle = []
X_top_margin = []
X_letter_size = []
X_line_spacing = []
X_word_spacing = []
X_pen_pressure = []
X_slant_angle = []
y_t1 = []
y_t2 = []
y_t3 = []
y_t4 = []
y_t5 = []
y_t6 = []
y_t7 = []
y_t8 = []
page_ids = []
if os.path.isfile("label_list"):
#print ("Info: label_list found.")
#=================================================================
with open("label_list", "r") as labels:
for line in labels:
content = line.split()
baseline_angle = float(content[0])
X_baseline_angle.append(baseline_angle)
top_margin = float(content[1])
X_top_margin.append(top_margin)
letter_size = float(content[2])
X_letter_size.append(letter_size)
line_spacing = float(content[3])
X_line_spacing.append(line_spacing)
word_spacing = float(content[4])
X_word_spacing.append(word_spacing)
pen_pressure = float(content[5])
X_pen_pressure.append(pen_pressure)
slant_angle = float(content[6])
X_slant_angle.append(slant_angle)
trait_1 = float(content[7])
y_t1.append(trait_1)
trait_2 = float(content[8])
y_t2.append(trait_2)
trait_3 = float(content[9])
y_t3.append(trait_3)
trait_4 = float(content[10])
y_t4.append(trait_4)
trait_5 = float(content[11])
y_t5.append(trait_5)
trait_6 = float(content[12])
y_t6.append(trait_6)
trait_7 = float(content[13])
y_t7.append(trait_7)
trait_8 = float(content[14])
y_t8.append(trait_8)
page_id = content[15]
page_ids.append(page_id)
#===============================================================
# emotional stability
X_t1 = []
for a, b in zip(X_baseline_angle, X_slant_angle):
X_t1.append([a, b])
# mental energy or will power
X_t2 = []
for a, b in zip(X_letter_size, X_pen_pressure):
X_t2.append([a, b])
# modesty
X_t3 = []
for a, b in zip(X_letter_size, X_top_margin):
X_t3.append([a, b])
# personal harmony and flexibility
X_t4 = []
for a, b in zip(X_line_spacing, X_word_spacing):
X_t4.append([a, b])
# lack of discipline
X_t5 = []
for a, b in zip(X_slant_angle, X_top_margin):
X_t5.append([a, b])
# poor concentration
X_t6 = []
for a, b in zip(X_letter_size, X_line_spacing):
X_t6.append([a, b])
# non communicativeness
X_t7 = []
for a, b in zip(X_letter_size, X_word_spacing):
X_t7.append([a, b])
# social isolation
X_t8 = []
for a, b in zip(X_line_spacing, X_word_spacing):
X_t8.append([a, b])
#print X_t1
#print type(X_t1)
#print len(X_t1)
X_train, X_test, y_train, y_test = train_test_split(X_t1, y_t1, test_size = .30, random_state=8)
clf1 = SVC(kernel='rbf')
clf1.fit(X_train, y_train)
#print ("Classifier 1 accuracy: ",accuracy_score(clf1.predict(X_test), y_test))
X_train, X_test, y_train, y_test = train_test_split(X_t2, y_t2, test_size = .30, random_state=16)
clf2 = SVC(kernel='rbf')
clf2.fit(X_train, y_train)
#print ("Classifier 2 accuracy: ",accuracy_score(clf2.predict(X_test), y_test))
X_train, X_test, y_train, y_test = train_test_split(X_t3, y_t3, test_size = .30, random_state=32)
clf3 = SVC(kernel='rbf')
clf3.fit(X_train, y_train)
#print ("Classifier 3 accuracy: ",accuracy_score(clf3.predict(X_test), y_test))
X_train, X_test, y_train, y_test = train_test_split(X_t4, y_t4, test_size = .30, random_state=64)
clf4 = SVC(kernel='rbf')
clf4.fit(X_train, y_train)
#print ("Classifier 4 accuracy: ",accuracy_score(clf4.predict(X_test), y_test))
X_train, X_test, y_train, y_test = train_test_split(X_t5, y_t5, test_size = .30, random_state=42)
clf5 = SVC(kernel='rbf')
clf5.fit(X_train, y_train)
#print ("Classifier 5 accuracy: ",accuracy_score(clf5.predict(X_test), y_test))
X_train, X_test, y_train, y_test = train_test_split(X_t6, y_t6, test_size = .30, random_state=52)
clf6 = SVC(kernel='rbf')
clf6.fit(X_train, y_train)
#print ("Classifier 6 accuracy: ",accuracy_score(clf6.predict(X_test), y_test))
X_train, X_test, y_train, y_test = train_test_split(X_t7, y_t7, test_size = .30, random_state=21)
clf7 = SVC(kernel='rbf')
clf7.fit(X_train, y_train)
#print ("Classifier 7 accuracy: ",accuracy_score(clf7.predict(X_test), y_test))
X_train, X_test, y_train, y_test = train_test_split(X_t8, y_t8, test_size = .30, random_state=73)
clf8 = SVC(kernel='rbf')
clf8.fit(X_train, y_train)
#print ("Classifier 8 accuracy: ",accuracy_score(clf8.predict(X_test), y_test))
#================================================================================================
file_names=['a.png','t.png','d2.jpg','zzz.png','y.png']
result=[0 for x in range(8)]
for file_name in file_names:
raw_features = extract.start(file_name)
raw_baseline_angle = raw_features[0]
baseline_angle, comment = categorize.determine_baseline_angle(raw_baseline_angle)
#print ("Baseline Angle: "+comment)
raw_top_margin = raw_features[1]
top_margin, comment = categorize.determine_top_margin(raw_top_margin)
#print ("Top Margin: "+comment)
raw_letter_size = raw_features[2]
letter_size, comment = categorize.determine_letter_size(raw_letter_size)
#print ("Letter Size: "+comment)
raw_line_spacing = raw_features[3]
line_spacing, comment = categorize.determine_line_spacing(raw_line_spacing)
#print ("Line Spacing: "+comment)
raw_word_spacing = raw_features[4]
word_spacing, comment = categorize.determine_word_spacing(raw_word_spacing)
#print ("Word Spacing: "+comment)
raw_pen_pressure = raw_features[5]
pen_pressure, comment = categorize.determine_pen_pressure(raw_pen_pressure)
#print ("Pen Pressure: "+comment)
raw_slant_angle = raw_features[6]
slant_angle, comment = categorize.determine_slant_angle(raw_slant_angle)
#print ("Slant: "+comment)
#print
result = [result[0]+0.2*clf1.predict([[baseline_angle, slant_angle]])[0],result[1]+0.2*clf2.predict([[letter_size, pen_pressure]])[0],result[2]+0.2*clf3.predict([[letter_size, top_margin]])[0],result[3]+0.2*clf4.predict([[line_spacing, word_spacing]])[0], result[4]+0.2*clf5.predict([[slant_angle, top_margin]])[0],result[5]+0.2*clf6.predict([[letter_size, line_spacing]])[0], result[6]+0.2*clf7.predict([[letter_size, word_spacing]])[0], result[7]+0.2*clf8.predict([[line_spacing, word_spacing]])[0]]
print (result)
#Emotional Stability
#print ("Mental Energy or Will Power: ", clf2.predict([[letter_size, pen_pressure]]))
#print ("Modesty: ", clf3.predict([[letter_size, top_margin]]))
#print ("Personal Harmony and Flexibility: ", clf4.predict([[line_spacing, word_spacing]]))
#print ("Lack of Discipline: ", clf5.predict([[slant_angle, top_margin]]))
#print ("Poor Concentration: ", clf6.predict([[letter_size, line_spacing]]))
#print ("Non Communicativeness: ", clf7.predict([[letter_size, word_spacing]]))
#print ("Social Isolation: ", clf8.predict([[line_spacing, word_spacing]]))
#print ("---------------------------------------------------")
#print
#=================================================================================================
else:
print ("Error: label_list file not found.")