-
Notifications
You must be signed in to change notification settings - Fork 0
/
chernoff.py
156 lines (137 loc) · 5.3 KB
/
chernoff.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 matplotlib
import matplotlib.pyplot as plt
from numpy.random import rand
from numpy import pi, arctan
from sklearn.decomposition import PCA
from sklearn.preprocessing import MinMaxScaler
matplotlib.use('Agg')
def cface(ax, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15,
x16, x17, x18):
# x1 = height of upper face
# x2 = overlap of lower face
# x3 = half of vertical size of face
# x4 = width of upper face
# x5 = width of lower face
# x6 = length of nose
# x7 = vertical position of mouth
# x8 = curvature of mouth
# x9 = width of mouth
# x10 = vertical position of eyes
# x11 = separation of eyes
# x12 = slant of eyes
# x13 = eccentricity of eyes
# x14 = size of eyes
# x15 = position of pupils
# x16 = vertical position of eyebrows
# x17 = slant of eyebrows
# x18 = size of eyebrows
# transform some values so that input between 0,1 yields variety of output
x3 = 1.9 * (x3 - .5)
x4 = (x4 + .25)
x5 = (x5 + .2)
x6 = .3 * (x6 + .01)
x8 = 5 * (x8 + .001)
x11 /= 5
x12 = 2 * (x12 - .5)
x13 += .05
x14 += .1
x15 = .5 * (x15 - .5)
x16 = .25 * x16
x17 = .5 * (x17 - .5)
x18 = .5 * (x18 + .1)
# top of face, in box with l=-x4, r=x4, t=x1, b=x3
e = matplotlib.patches.Ellipse((0, (x1 + x3) / 2),
2 * x4, (x1 - x3),
fc='white',
edgecolor='black',
linewidth=2)
# e.set_clip_box(ax.bbox)
# e.set_facecolor([0,0,0])
ax.add_artist(e)
# bottom of face, in box with l=-x5, r=x5, b=-x1, t=x2+x3
e = matplotlib.patches.Ellipse((0, (-x1 + x2 + x3) / 2),
2 * x5, (x1 + x2 + x3),
fc='white',
edgecolor='black',
linewidth=2)
ax.add_artist(e)
# cover overlaps
e = matplotlib.patches.Ellipse((0, (x1 + x3) / 2),
2 * x4, (x1 - x3),
fc='white',
edgecolor='black',
ec='none')
ax.add_artist(e)
e = matplotlib.patches.Ellipse((0, (-x1 + x2 + x3) / 2),
2 * x5, (x1 + x2 + x3),
fc='white',
edgecolor='black',
ec='none')
ax.add_artist(e)
# draw nose
ax.plot([0, 0], [-x6 / 2, x6 / 2], 'k')
# draw mouth
p = matplotlib.patches.Arc((0, -x7 + .5 / x8),
1 / x8,
1 / x8,
theta1=270 - 180 / pi * arctan(x8 * x9),
theta2=270 + 180 / pi * arctan(x8 * x9))
ax.add_artist(p)
# draw eyes
p = matplotlib.patches.Ellipse((-x11 - x14 / 2, x10),
x14,
x13 * x14,
angle=-180 / pi * x12,
facecolor='white',
edgecolor='black')
ax.add_artist(p)
p = matplotlib.patches.Ellipse((x11 + x14 / 2, x10),
x14,
x13 * x14,
angle=180 / pi * x12,
facecolor='white',
edgecolor='black')
ax.add_artist(p)
# draw pupils
p = matplotlib.patches.Ellipse((-x11 - x14 / 2 - x15 * x14 / 2, x10),
.05,
.05,
facecolor='black')
ax.add_artist(p)
p = matplotlib.patches.Ellipse((x11 + x14 / 2 - x15 * x14 / 2, x10),
.05,
.05,
facecolor='black')
ax.add_artist(p)
# draw eyebrows
ax.plot([-x11 - x14 / 2 - x14 * x18 / 2, -x11 - x14 / 2 + x14 * x18 / 2],
[x10 + x13 * x14 * (x16 + x17), x10 + x13 * x14 * (x16 - x17)],
'k')
ax.plot([x11 + x14 / 2 + x14 * x18 / 2, x11 + x14 / 2 - x14 * x18 / 2],
[x10 + x13 * x14 * (x16 + x17), x10 + x13 * x14 * (x16 - x17)],
'k')
def plot_cface(data):
scaled_data = MinMaxScaler().fit_transform(X=data)
pca_data = PCA(n_components=17).fit_transform(scaled_data)
print(pca_data)
fig = plt.figure(figsize=(11, 11))
for i in range(25):
ax = fig.add_subplot(5, 5, i + 1, aspect='equal')
cface(ax, 0.9, *pca_data[i])
ax.axis([-1.2, 1.2, -1.2, 1.2])
ax.set_xticks([])
ax.set_yticks([])
fig.subplots_adjust(hspace=0, wspace=0)
plt.savefig('predicted.png', bbox_inches='tight')
if __name__ == "__main__":
data = rand(25, 17)
plot_cface(data)
# fig = plt.figure(figsize=(11, 11))
# for i in range(25):
# ax = fig.add_subplot(5, 5, i + 1, aspect='equal')
# cface(ax, 1.1, *rand(17))
# ax.axis([-1.2, 1.2, -1.2, 1.2])
# ax.set_xticks([])
# ax.set_yticks([])
# fig.subplots_adjust(hspace=0, wspace=0)
# plt.savefig('predicted.png', bbox_inches='tight')