-
Notifications
You must be signed in to change notification settings - Fork 4
/
expert_and_explanation.py
382 lines (344 loc) · 17.9 KB
/
expert_and_explanation.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!/usr/bin/python3
'''Core of Expert eyeglasses recommendation system.
In this module the translation from facial feature to eyeglasses was implemented.
The task of recommending eyeframes based on people's appearance is quite hard
and there are no similar projects available at the moment. Facial features
consists of more than 20 facial attributes, with the help of expert module
function apply written beforehand rules to get necessary mappings. High
interpretability of such approach guaranties user's understanding and loyality
towards the system.
At the current state in translation function was already implemented:
* Applying more than 40 branches of rules
* Processing the scenario, when there is no dominant face shape
* Paying more attention to other face shapes when dominant face shape is oval/oblong
* Static-based rules, written in code
* Description of applied rules in english/russian for great interpretability
Example:
To use this module, you simply import class in your python code:
# from expert_and_explanation import translate_facevec2eyeglassesvec
After that you can call it with given protocol
Todo:
* Place description's texts in separate file
* Place rules in separate file, so it will be possible to manually change them
* Implementation of summarizing results of explanation module
* Rewrite code so it will be more scalable with raising number of rules
.. _Expert eyeglasses recommendation system with Generative Adversarial Networks:
https://github.com/Defasium/expertglasses
'''
import json
import numpy as np
VERSION = __version__ = '0.2.5 Released 27-August-2020'
def translate_facevec2eyeglassesvec(facevector: dict, s_vector: np.ndarray, c_vector: np.ndarray,
lang='ru'):
'''Maps facial features to eyeglasses features
by consequentially applying static expert rules. Also
constructs description for each rule, which can be used to
interpret the results of the translation.
Args:
facevector (dict with strings as key): Dictionary with facial attributes.
s_vector (numpy.ndarray): Initial shape attributes of eyeglasses.
c_vector (numpy.ndarray): Initial color attributes of eyeglasses.
lang (str, default='ru'): What language to use for description.
Supported values are ['ru', 'en'].
Returns:
s_vector (numpy.ndarray): Final shape attributes of eyeglasses.
c_vector (numpy.ndarray): Final color attributes of eyeglasses.
description (str): Explanation of translation and applied rules.
'''
# load specified localization from json
with open('lang/%s_lang.json' % lang, 'r') as lang_file:
lang_i = json.load(lang_file)['explanation'].copy()
description = []
##############################
######### Faceshape ##########
##############################
description.append(lang_i['faceshape']['header'])
if facevector['faceshape'][0][0] < 0.35:
emphasize_coef = 3
else:
emphasize_coef = 1
condition = facevector['faceshape'][0][1] in ['oval', 'oblong']
for fraction, faceshape in facevector['faceshape']:
if fraction < 0.05:
continue
# more attention to second component
if condition and faceshape == facevector['faceshape'][1][1]:
attention_on_other_shapes = 3
else:
attention_on_other_shapes = 1
if faceshape == 'oval':
s_vector[[0, 1, 4, 8]] += fraction * 2
s_vector[[2, 3, 5, 6, 7, 9]] += fraction / 2
s_vector[14] += fraction # small and oversized are bad choice
s_vector[[13, 15, 16, 18]] -= fraction
if facevector['ratio'] == 'wider':
s_vector[12] += fraction / len(facevector['faceshape'])
else:
s_vector[[10, 11]] += fraction
c_vector += fraction
elif faceshape == 'triangle':
s_vector[[3, 5, 7]] += fraction * 2 * attention_on_other_shapes
s_vector[[0, 1, 2, 4]] += fraction / 2 * attention_on_other_shapes
s_vector[[28]] += fraction * attention_on_other_shapes
s_vector[[29]] += fraction * attention_on_other_shapes / 2
c_vector[[0]] -= fraction * attention_on_other_shapes
c_vector[[3, 8, 12]] += fraction * attention_on_other_shapes
c_vector[[9]] += fraction * attention_on_other_shapes / 3
elif faceshape == 'oblong':
s_vector[[3, 4]] += fraction * 2
s_vector[[2]] += fraction / 2
s_vector[14] += fraction
s_vector[15] += fraction * 2
s_vector[28] += fraction / 2
c_vector[13] += fraction
c_vector[14] += fraction
elif faceshape == 'heart':
s_vector[[2, 4, 7, 9]] += fraction * 2 * attention_on_other_shapes
s_vector[[0, 1, 3]] += fraction / 2 * attention_on_other_shapes
s_vector[[11, 12]] += fraction * attention_on_other_shapes
s_vector[[10]] += fraction / 2 * attention_on_other_shapes
s_vector[20] -= fraction * 2 * attention_on_other_shapes
s_vector[22] += fraction * attention_on_other_shapes
c_vector[[1, 8]] += fraction * attention_on_other_shapes
c_vector[[9]] += fraction / 3 * attention_on_other_shapes
elif faceshape == 'diamond':
s_vector[[2, 4, 7]] += fraction * attention_on_other_shapes
s_vector[[29]] += fraction * attention_on_other_shapes
s_vector[[11]] += fraction * attention_on_other_shapes
s_vector[20] += fraction * attention_on_other_shapes
# small and oversized are bad choice
s_vector[14] += fraction * attention_on_other_shapes
s_vector[[13, 15, 16, 18]] -= fraction * attention_on_other_shapes
c_vector[[1, 2, 13]] += fraction * attention_on_other_shapes
elif faceshape == 'round':
s_vector[[0, 1, 5, 9]] += fraction * 2 * attention_on_other_shapes
s_vector[[2, 3]] -= fraction * 2 * attention_on_other_shapes
s_vector[14] += fraction * attention_on_other_shapes
s_vector[[13, 15, 16, 18]] -= fraction * attention_on_other_shapes
s_vector[15] += fraction / 2 * attention_on_other_shapes
s_vector[19] += fraction / 2 * attention_on_other_shapes
s_vector[[10, 11]] += fraction * 2 * attention_on_other_shapes
s_vector[[22]] -= fraction * 2 * attention_on_other_shapes
c_vector[[1, 2, 13]] += fraction * attention_on_other_shapes
elif faceshape == 'square':
s_vector[[3]] += fraction * 2 * attention_on_other_shapes
s_vector[[2, 5]] += fraction * attention_on_other_shapes
s_vector[[0, 1]] -= fraction * 2 * attention_on_other_shapes
s_vector[[17, 18]] += fraction * attention_on_other_shapes
s_vector[[28]] += fraction / 2 * attention_on_other_shapes
s_vector[[21]] -= fraction * 2 * attention_on_other_shapes
s_vector[[22]] -= fraction * attention_on_other_shapes
c_vector[[10, 3, 5, 7, 12]] += fraction * attention_on_other_shapes
c_vector[[9]] += fraction / 3 * attention_on_other_shapes
elif faceshape == 'rectangle':
s_vector[[3, 4]] += fraction * attention_on_other_shapes
s_vector[[2]] += fraction / 2 * attention_on_other_shapes
s_vector[14] += fraction / 2 * attention_on_other_shapes
s_vector[15] += fraction * attention_on_other_shapes
c_vector[13] += fraction * attention_on_other_shapes
c_vector[14] += fraction * attention_on_other_shapes
s_vector[[11, 12]] += fraction / 2 * attention_on_other_shapes
# add description
description.append(lang_i['faceshape'][faceshape]['prefix'])
description.append(f'{fraction*100:.1f}%\n')
description.append(lang_i['faceshape'][faceshape]['description'])
##############################
######### Faceratio ##########
##############################
description.append(lang_i['faceratio']['header'])
if facevector['ratio'] == 'wider':
s_vector[[10, 20]] += 2.0 * emphasize_coef
s_vector[[15, 16]] -= 2.0 * emphasize_coef
elif facevector['ratio'] == 'longer':
s_vector[[20]] -= 2.0 * emphasize_coef
s_vector[[14, 15]] += 2.0 * emphasize_coef
description.append(lang_i['faceratio'][facevector['ratio']]['prefix'])
description.append(lang_i['faceratio'][facevector['ratio']]['description'])
##############################
########## Jawtype ###########
##############################
description.append(lang_i['jawtype']['header'])
if facevector['beard'] == 'no': # huge beard biases focus from faceshape
if facevector['jawtype'] == 'soft' or \
facevector['doublechin'] == 'yes' or \
facevector['chubby'] == 'yes':
description.append(lang_i['jawtype']['soft']['prefix'])
description.append(lang_i['jawtype']['soft']['description'])
s_vector[[21]] += 2.0 * emphasize_coef
s_vector[[2, 3, 5]] += 2.0 * emphasize_coef
else:
description.append(lang_i['jawtype']['defined']['prefix'])
description.append(lang_i['jawtype']['defined']['description'])
s_vector[[21]] -= 2.0 * emphasize_coef
s_vector[[0, 1]] += 2.0 * emphasize_coef
else:
description.append(lang_i['jawtype']['beard']['prefix'])
description.append(lang_i['jawtype']['beard']['description'])
s_vector[[20]] += 1.0 * emphasize_coef
##############################
######### Eyebrows ###########
##############################
description.append(lang_i['eyebrows']['header'])
if facevector['eyebrows_thickness'] == 'thick':
description.append(lang_i['eyebrows'][facevector['eyebrows_thickness']]['prefix'])
description.append(lang_i['eyebrows'][facevector['eyebrows_thickness']]['description'])
s_vector[[11, 12]] += 1.0 * emphasize_coef
s_vector[[22]] -= 1.0 * emphasize_coef
elif facevector['eyebrows_thickness'] == 'thin':
description.append(lang_i['eyebrows'][facevector['eyebrows_thickness']]['prefix'])
description.append(lang_i['eyebrows'][facevector['eyebrows_thickness']]['description'])
s_vector[[10]] += 1.0 * emphasize_coef
s_vector[[22]] += 1.0 * emphasize_coef
if facevector['eyebrows_shape'] == 'flat':
s_vector[[23]] += 2.0 * emphasize_coef
elif facevector['eyebrows_shape'] == 'curly':
s_vector[[24, 25]] += 2.0 * emphasize_coef
elif facevector['eyebrows_shape'] == 'roof':
s_vector[[26]] += 2.0 * emphasize_coef
elif facevector['eyebrows_shape'] == 'angry':
s_vector[[27]] += 2.0 * emphasize_coef
description.append(lang_i['eyebrows'][facevector['eyebrows_shape']]['prefix'])
description.append(lang_i['eyebrows'][facevector['eyebrows_shape']]['description'])
##############################
############ Nose ############
##############################
description.append(lang_i['nose']['header'])
if facevector['nose_size'] in ('big', 'long'):
s_vector[[10]] += 1.0 * emphasize_coef
s_vector[[22]] += 1.0 * emphasize_coef
s_vector[[19]] -= 1.0 * emphasize_coef
else:
s_vector[[11, 12]] += 0.25
s_vector[[19]] += 1.0
description.append(lang_i['nose'][facevector['nose_size']]['prefix'])
description.append(lang_i['nose'][facevector['nose_size']]['description'])
##############################
############ Eyes ############
##############################
description.append(lang_i['eyes']['header'])
if facevector['eyes_narrow'] == 'yes':
description.append(lang_i['eyes']['narrow']['prefix'])
description.append(lang_i['eyes']['narrow']['description'])
s_vector[[14, 15]] += 1.0 * emphasize_coef
s_vector[[17, 18]] += 1.0 * emphasize_coef # narrow eyes -> larger frames
c_vector[[0, 1]] -= 1.0 * emphasize_coef
s_vector[[4, 8]] += 0.5 * emphasize_coef
s_vector[[22]] -= 0.5 * emphasize_coef
c_vector[[9]] += 0.3
if facevector['eyes_iris'] == 'brown':
c_vector[[1, 3, 12, 13]] += 1.0
c_vector[[9]] += 0.3
elif facevector['eyes_iris'] == 'blue':
c_vector[[2, 3, 5, 12]] += 1.0
c_vector[[9]] += 0.3
elif facevector['eyes_iris'] == 'gray': # anything
c_vector += 1.0
elif facevector['eyes_iris'] == 'green':
c_vector[[1, 5, 6, 10, 7, 12]] += 1.0
c_vector[[9]] += 0.3
description.append(lang_i['eyes'][facevector['eyes_iris']]['prefix'])
description.append(lang_i['eyes'][facevector['eyes_iris']]['description'])
##############################
######### Forehead ###########
##############################
description.append(lang_i['forehead']['header'])
if facevector['forehead'] == 'big' and facevector['bangs'] == 'no':
description.append(lang_i['forehead']['big']['prefix'])
description.append(lang_i['forehead']['big']['description'])
s_vector[[22]] += 2.0 * emphasize_coef
s_vector[[10]] += 2.0 * emphasize_coef
else:
description.append(lang_i['forehead']['small']['prefix'])
description.append(lang_i['forehead']['small']['description'])
s_vector[[22]] -= 0.25 * emphasize_coef
s_vector[[11, 12]] += 0.25 * emphasize_coef
##############################
########### Lips #############
##############################
description.append(lang_i['lips']['header'])
if facevector['lips'] == 'big' and facevector['mustache'] == 'no':
description.append(lang_i['lips'][facevector['lips']]['prefix'])
description.append(lang_i['lips'][facevector['lips']]['description'])
s_vector[[22]] += 0.5 * emphasize_coef
s_vector[[10]] += 0.5 * emphasize_coef
else:
description.append(lang_i['lips'][facevector['lips']]['prefix'])
description.append(lang_i['lips'][facevector['lips']]['description'])
s_vector[[22]] -= 0.125 * emphasize_coef
s_vector[[11, 12]] += 0.125 * emphasize_coef
##############################
########## Baldness ##########
##############################
description.append(lang_i['hair']['header'])
if facevector['bald'] == 'yes':
description.append(lang_i['hair']['bald']['prefix'])
description.append(lang_i['hair']['bald']['description'])
s_vector[[23]] += 2.0 * emphasize_coef
s_vector[[22]] += 2.0 * emphasize_coef
s_vector[[10]] += 1.0 * emphasize_coef
c_vector[0] += 1.0
##############################
######### Hair color #########
##############################
if facevector['hair'] == 'black':
s_vector[29] += 1.0
s_vector[28] += 0.5
c_vector[[2, 4, 10, 0, 1, 8, 3, 12, 9]] += 1.0
elif facevector['hair'] == 'blonde':
s_vector[28] += 1.0
s_vector[29] += 0.5
c_vector[[2, 13, 5, 10, 6, 0, 12]] += 1.0
elif facevector['hair'] == 'brown':
s_vector[29] += 1.0
s_vector[28] += 0.5
c_vector[[4, 13, 8, 5, 6, 7, 1, 0]] += 1.0
elif facevector['hair'] == 'grey':
s_vector[29] += 1.0
s_vector[28] += 0.5
c_vector[[11, 5, 1, 3, 7, 12, 0, 8]] += 1.0
elif facevector['hair'] == 'red':
c_vector[[2, 6, 5, 1, 12, 3, 0]] += 1.0
description.append(lang_i['hair']['haircolor'][facevector['hair']]['prefix'])
description.append(lang_i['hair']['haircolor'][facevector['hair']]['description'])
##############################
######### Skintone ###########
##############################
description.append(lang_i['skintone']['header'])
if facevector['skintone'] == 'warm':
c_vector[[2, 5, 12, 1, 6]] += 1.0
elif facevector['skintone'] == 'neutral': # any color is good
c_vector[[1, 5, 3, 6, 9, 7, 12, 0, 8]] += 1.0
s_vector[29] += 1.0
elif facevector['skintone'] == 'cool':
c_vector[[4, 5, 3, 7, 6, 10, 9, 12, 0]] += 1.0
description.append(lang_i['skintone'][facevector['skintone']]['prefix'])
description.append(lang_i['skintone'][facevector['skintone']]['description'])
if facevector['race'] == 'black':
description.append(lang_i['skintone']['black']['prefix'])
description.append(lang_i['skintone']['black']['description'])
c_vector[[9, 12, 0, 4, 2, 11]] += 1.0
elif facevector['paleskin'] == 'yes':
description.append(lang_i['skintone']['pale']['prefix'])
description.append(lang_i['skintone']['pale']['description'])
c_vector[[9, 1, 0]] += 1.0
s_vector[[10]] += 1.0 * emphasize_coef
s_vector[[11]] += 0.5 * emphasize_coef
else:
c_vector[[0, 3, 6, 13]] += 0.5
##############################
############ Sex #############
##############################
description.append(lang_i['sex']['header'])
if facevector['gender'] == 'female':
s_vector[[28]] += 2.0 * emphasize_coef
s_vector[[10]] += 2.0 * emphasize_coef
s_vector[[22]] += 2.0 * emphasize_coef
s_vector[[30]] -= 1.0
else:
s_vector[[29]] += 1.0 * emphasize_coef
s_vector[[10]] += 0.5 * emphasize_coef
s_vector[[11]] += 1.0 * emphasize_coef
s_vector[[30]] += 1.0
description.append(lang_i['sex'][facevector['gender']]['prefix'])
description.append(lang_i['sex'][facevector['gender']]['description'])
return s_vector, c_vector, ''.join(description)