-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp1ec2face.py
executable file
·147 lines (131 loc) · 3.37 KB
/
p1ec2face.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
import math
import numpy as np
trainingData = open('facedatatrain', 'r');
label_file = open('facedatatrainlabels', 'r');
training_label = [] #array consist of training label
count = 0
index = 0
data_dict = {}
temp = []
for line in label_file:
training_label.append(int(line[0:-1]));
numberOfTrainingData = len(training_label)
for line in trainingData:
if(count < 70):
temp.append(line[0:-1])
count += 1;
else:
isFace = training_label[index]
if isFace in data_dict:
data_dict[isFace].append(temp)
else:
data_dict[isFace] = [temp];
count = 0
temp = []
index += 1
temp.append(line[0:-1])
count += 1;
## calculate possibility
# priors
class_possibility = []
for i in range(2):
class_possibility.append(float(len(data_dict[i])) / numberOfTrainingData)
m = 70
n = 60
#m*n size block
trained = []
for i in range(2):#For each number class
num_trained = {}#Dict for current class, (x, y, pixelValue):Occurance Time
for x_idx in range(70):
for y_idx in range(60):
for ele in ['#', ' ']:
num_trained[(x_idx, y_idx, ele)] = 1;
for j in range(len(data_dict[i])):
num_dict = data_dict[i][j]
for x_idx in range(len(num_dict)-(m-1)):
for y_idx in range(len(num_dict[x_idx])-(n-1)):
#obj = (x_idx, y_idx, data_dict[i][j][x_idx][y_idx])
compareString = ''
for x in range(m):
for y in range(n):
#wenwen += data_dict[i][j][x_idx+x][y_idx+y]
if data_dict[i][j][x_idx+x][y_idx+y] == '#' :
compareString += '#'
if data_dict[i][j][x_idx+x][y_idx+y] == ' ' :
compareString += ' '
obj = (x_idx, y_idx, compareString)
if obj in num_trained:
num_trained[obj] += 1;
else:
num_trained[obj] = 1;
for key in num_trained.keys():
print(key)
num_trained[key] = (num_trained[key]) / float(len(data_dict[i]));
trained.append(num_trained);
# print(trained[0])
trainingData.close();
label_file.close();
#Validate
f_testi = open('facedatatest', 'r');
f_testl = open('facedatatestlabels', 'r');
label_arr_valid = []
total_result = []
temp = []
testimage_file = []
count = 0
index = 0
t_count = 0.0;
correct_number = 0
wrong_number = 0
for line in f_testl:
label_arr_valid.append(int(line[0:-1]));
for line in f_testi:
if(count < 70):
temp.append(line[0:-1])
count += 1;
else:#get an image
t_count += 1;
testimage_file.append(temp)
result = []
for k in range(2):
curr_result = np.log2(class_possibility[k]);
for i in range(70-(m-1)):
for j in range(60-(n-1)):
temp_dict = trained[k]
stringtemp = ''
for x in range(m):
for y in range(n):
#wlb += temp[i+x][j+y]
if temp[i+x][j+y] == '#' :
stringtemp += '#'
if temp[i+x][j+y] == ' ' :
stringtemp += ' '
if (i,j,stringtemp) in temp_dict:
t = temp_dict[(i,j,stringtemp)]
else:
t = 1/float(len(data_dict[k]))
curr_result = curr_result + np.log2(t)
result.append(curr_result);
result_num = np.argmax(result);
true_num = label_arr_valid[index];
if result_num == true_num:
correct_number +=1
print("---------number:",t_count)
print("correct")
if(result_num == 1):
print("face")
else:
print("not face")
print(">>>>>>>>>")
else:
wrong_number +=1
print("wrong")
# total_result.append(np.argmax(result))
count = 0
index += 1
temp = []
temp.append(line[0:-1])
count += 1;
print(correct_number)
print(wrong_number)
print(correct_number/t_count)