File size: 14,049 Bytes
c4ba2f9
 
 
c977de5
c4ba2f9
f0f282b
 
 
c4ba2f9
f0f282b
c4ba2f9
f0f282b
 
 
c4ba2f9
 
 
f0f282b
 
 
 
 
 
 
 
 
 
c4ba2f9
b3eed86
c4ba2f9
 
 
f0f282b
c4ba2f9
f0f282b
 
 
 
 
b3eed86
c4ba2f9
f0f282b
c4ba2f9
 
f0f282b
 
c4ba2f9
b3eed86
c4ba2f9
f0f282b
c4ba2f9
f0f282b
 
 
 
 
 
 
 
b3eed86
f0f282b
c4ba2f9
 
f0f282b
 
 
 
c4ba2f9
b3eed86
c4ba2f9
 
f0f282b
 
 
 
 
c4ba2f9
b3eed86
c4ba2f9
 
 
 
b3eed86
c4ba2f9
f0f282b
c4ba2f9
f0f282b
 
c4ba2f9
b3eed86
c4ba2f9
 
f0f282b
 
 
 
 
 
b3eed86
c4ba2f9
 
f0f282b
 
 
 
c4ba2f9
b3eed86
c4ba2f9
 
f0f282b
 
c4ba2f9
b3eed86
c4ba2f9
 
f0f282b
 
 
 
c4ba2f9
b3eed86
c4ba2f9
f0f282b
 
 
 
 
c4ba2f9
b3eed86
f0f282b
c4ba2f9
f0f282b
 
 
 
c4ba2f9
b3eed86
c4ba2f9
 
f0f282b
 
 
 
c4ba2f9
b3eed86
c4ba2f9
 
f0f282b
 
 
 
c4ba2f9
b3eed86
c4ba2f9
 
f0f282b
 
c4ba2f9
b3eed86
f0f282b
 
 
 
 
 
 
 
c4ba2f9
b3eed86
c4ba2f9
 
 
f0f282b
 
 
 
 
c4ba2f9
 
 
 
 
f0f282b
 
c4ba2f9
 
 
 
f0f282b
 
 
 
c4ba2f9
 
 
 
f0f282b
 
 
c4ba2f9
 
 
f0f282b
 
 
 
 
 
c4ba2f9
 
b3eed86
 
f0f282b
 
 
 
8ede11e
 
 
 
f0f282b
 
8ede11e
 
f0f282b
 
8ede11e
 
 
 
 
b3eed86
f0f282b
 
c977de5
f0f282b
 
 
 
 
 
 
 
 
 
8ede11e
b3eed86
 
 
 
f0f282b
 
 
b3eed86
f0f282b
84b361a
f0f282b
 
 
 
 
 
 
b3eed86
 
f0f282b
 
 
 
 
 
 
 
b3eed86
 
 
 
 
 
f0f282b
 
 
 
b3eed86
 
 
 
f0f282b
b3eed86
 
f0f282b
 
 
 
 
b3eed86
f0f282b
b3eed86
 
f0f282b
 
 
 
 
 
b3eed86
 
 
 
c4ba2f9
b3eed86
f0f282b
 
b3eed86
 
f0f282b
 
b3eed86
 
f0f282b
84b361a
f0f282b
 
 
b3eed86
 
 
 
f0f282b
 
 
 
 
b3eed86
f0f282b
b3eed86
c4ba2f9
f0f282b
 
 
 
c4ba2f9
b3eed86
 
f0f282b
 
b3eed86
 
f0f282b
 
c4ba2f9
f0f282b
 
84b361a
f0f282b
 
 
 
 
 
b3eed86
 
 
 
f0f282b
 
 
 
 
 
 
c4ba2f9
 
f0f282b
 
b3eed86
f0f282b
 
 
 
 
 
 
 
c4ba2f9
b3eed86
c4ba2f9
 
 
 
 
b3eed86
c4ba2f9
 
 
 
 
b3eed86
 
2fe3b48
 
 
 
 
 
b3eed86
dc385c9
f0f282b
b3eed86
f0f282b
 
b3eed86
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
STRONG = 3
MEDIUM = 2
WEAK = 1
HAIR_TYPE_WEIGHT = 1.4

FEMALE_DIMS = {"layers", "updo", "curtain_fringe"}

def _base_scores(gender):
    scores = {
        "volume_top":   0,
        "volume_sides": 0,
        "longer_hair":  0,
        "fringe":       0,
        "clean_lines":  0,
        "textured_top": 0,
        "soft_texture": 0,
    }
    if gender == "Woman":
        scores.update({"layers": 0, "updo": 0, "curtain_fringe": 0})
    else:
        scores["short_sides"] = 0
    return scores


def apply_rules(traits, gender="Man"):
    scores = _base_scores(gender)
    is_female = gender == "Woman"

    if traits.get("face_length") == "long":
        scores["volume_sides"] += STRONG
        scores["fringe"] += STRONG
        scores["volume_top"] -= MEDIUM
        scores["longer_hair"] -= MEDIUM if not is_female else WEAK
        scores["clean_lines"] -= WEAK
        if is_female:
            scores["curtain_fringe"] += MEDIUM
            scores["layers"] += MEDIUM
            scores["updo"] -= MEDIUM
    
    elif traits.get("face_length") == "short":
        scores["volume_top"] += STRONG
        scores["longer_hair"] += MEDIUM if not is_female else STRONG
        scores["fringe"] -= MEDIUM
        scores["volume_sides"] -= WEAK
        if is_female:
            scores["updo"] += MEDIUM

    if traits.get("jaw") == "wide":
        scores["soft_texture"] += STRONG
        scores["longer_hair"] += WEAK if not is_female else MEDIUM
        scores["clean_lines"] -= MEDIUM
        if is_female:
            scores["layers"] += MEDIUM
            scores["curtain_fringe"] += MEDIUM
            scores["volume_sides"] -= MEDIUM
        else:
            scores["volume_top"] += MEDIUM
            scores["short_sides"] -= STRONG

    elif traits.get("jaw") == "narrow":
        scores["volume_sides"] += MEDIUM if not is_female else STRONG
        scores["clean_lines"] += WEAK
        scores["soft_texture"] -= WEAK
        if is_female:
            scores["layers"] += MEDIUM
        else:
            scores["short_sides"] += STRONG

    if traits.get("jaw_height") == "high":
        scores["longer_hair"] += MEDIUM
        scores["volume_top"] -= MEDIUM
        if is_female:
            scores["layers"] += WEAK
        else:
            scores["soft_texture"] += WEAK
            scores["short_sides"] -= WEAK

    elif traits.get("jaw_height") == "low":
        scores["volume_top"] += MEDIUM
        scores["fringe"] += WEAK
        scores["longer_hair"] -= WEAK

    if traits.get("eyes") == "wide":
        scores["clean_lines"] += MEDIUM
        scores["volume_top"] += MEDIUM if not is_female else WEAK
        scores["volume_sides"] -= WEAK
        if is_female:
            scores["curtain_fringe"] -= WEAK

    elif traits.get("eyes") == "close":
        scores["volume_sides"] += MEDIUM
        scores["fringe"] -= MEDIUM
        if is_female:
            scores["curtain_fringe"] += STRONG
        else:
            scores["soft_texture"] += WEAK
            scores["clean_lines"] -= WEAK
    
    if traits.get("eye_openness") == "narrow":
        scores["volume_top"] += MEDIUM
        scores["fringe"] -= STRONG
        if is_female:
            scores["curtain_fringe"] -= MEDIUM
        else:
            scores["clean_lines"] += WEAK

    elif traits.get("eye_openness") == "open":
        scores["fringe"] += MEDIUM
        scores["textured_top"] += WEAK
        if is_female:
            scores["curtain_fringe"] += MEDIUM

    if traits.get("lips") == "wide":
        scores["soft_texture"] += MEDIUM
        scores["clean_lines"] -= WEAK
        if is_female:
            scores["layers"] += WEAK
        else:
            scores["volume_top"] += WEAK

    elif traits.get("lips") == "narrow":
        scores["clean_lines"] += MEDIUM
        if is_female:
            scores["updo"] += WEAK
        else:
            scores["short_sides"] += WEAK
            scores["soft_texture"] -= WEAK

    if traits.get("nose") == "upper-dominant":
        scores["fringe"] += MEDIUM if not is_female else STRONG
        scores["volume_top"] -= STRONG
        if is_female:
            scores["curtain_fringe"] += MEDIUM
        else:
            scores["volume_sides"] += WEAK

    elif traits.get("nose") == "lower-dominant":
        scores["volume_top"] += STRONG
        scores["fringe"] -= MEDIUM
        if is_female:
            scores["updo"] += WEAK
        else:
            scores["longer_hair"] += WEAK

    if traits.get("lower_face") == "long":
        scores["longer_hair"] += MEDIUM
        scores["soft_texture"] += MEDIUM
        if is_female:
            scores["layers"] += WEAK
        else:
            scores["short_sides"] -= WEAK

    elif traits.get("lower_face") == "short":
        scores["clean_lines"] += MEDIUM
        scores["longer_hair"] -= WEAK
        if not is_female:
            scores["short_sides"] += MEDIUM

    if traits.get("chin") == "prominent":
        scores["longer_hair"] += MEDIUM if not is_female else STRONG
        scores["soft_texture"] += MEDIUM if not is_female else STRONG
        scores["clean_lines"] -= WEAK if not is_female else MEDIUM
        if is_female:
            scores["layers"] += MEDIUM
        else:
            scores["textured_top"] += STRONG
            scores["short_sides"] -= WEAK

    elif traits.get("chin") == "recessed":
        scores["volume_top"] += STRONG
        scores["clean_lines"] += WEAK
        scores["longer_hair"] -= WEAK
        if is_female:
            scores["updo"] += WEAK
        else:
            scores["short_sides"] += MEDIUM
            scores["textured_top"] -= MEDIUM

    if traits.get("facial_thirds") == "lower_dominant":
        scores["volume_top"] += STRONG
        scores["fringe"] -= MEDIUM
        scores["longer_hair"] -= WEAK
        if is_female:
            scores["updo"] += MEDIUM

    elif traits.get("facial_thirds") == "middle_dominant":
        scores["fringe"] += MEDIUM
        scores["volume_top"] -= MEDIUM
        if is_female:
            scores["curtain_fringe"] += MEDIUM
        else:
            scores["volume_sides"] += WEAK

    if traits.get("forehead") == "high":
        scores["fringe"] += STRONG
        scores["volume_top"] -= MEDIUM
        if is_female:
            scores["curtain_fringe"] += MEDIUM
            scores["updo"] -= MEDIUM

    elif traits.get("forehead") == "low":
        scores["fringe"] -= STRONG
        if is_female:
            scores["updo"] += MEDIUM
            scores["curtain_fringe"] -= MEDIUM
        else:
            scores["volume_top"] += WEAK
            scores["textured_top"] += WEAK

    if traits.get("thirds_balance") == "imbalanced":
        scores["soft_texture"] += MEDIUM
        scores["clean_lines"] -= MEDIUM
        if is_female:
            scores["layers"] += MEDIUM

    if traits.get("hair_type") == "curly":
        scores["soft_texture"] += MEDIUM * HAIR_TYPE_WEIGHT
        scores["textured_top"] += MEDIUM * HAIR_TYPE_WEIGHT
        scores["clean_lines"] -= WEAK * HAIR_TYPE_WEIGHT
        scores["longer_hair"] -= WEAK * HAIR_TYPE_WEIGHT

    elif traits.get("hair_type") in ("straight", "wavy"):
        scores["clean_lines"] += WEAK * HAIR_TYPE_WEIGHT
        scores["longer_hair"] += WEAK * HAIR_TYPE_WEIGHT

    elif traits.get("hair_type") == "coily":
        scores["soft_texture"] += STRONG * HAIR_TYPE_WEIGHT
        scores["textured_top"] += STRONG * HAIR_TYPE_WEIGHT
        scores["volume_top"] += MEDIUM * HAIR_TYPE_WEIGHT
        scores["clean_lines"] -= MEDIUM * HAIR_TYPE_WEIGHT
        scores["longer_hair"] -= MEDIUM * HAIR_TYPE_WEIGHT

    if traits.get("hairline") == "receding":
        scores["textured_top"] += WEAK
        scores["fringe"] -= STRONG
        scores["volume_top"] -= WEAK
        if not is_female:
            scores["short_sides"] += WEAK

    elif traits.get("hairline") == "uneven":
        scores["fringe"] -= MEDIUM
        scores["soft_texture"] += WEAK

    scores = _apply_interaction_rules(scores, traits, gender)
    scores = _apply_symmetry_modulation(scores, traits, gender)
    scores = _apply_hard_constraints(scores, traits)
    scores = clamp_scores(scores)

    return scores

def _apply_interaction_rules(scores, traits, gender):
    is_female = gender == "Woman"

    if traits.get("face_length") == "long" and traits.get("forehead") == "high":
        scores["fringe"] += MEDIUM if not is_female else WEAK
        scores["volume_sides"] += WEAK
        scores["volume_top"] -= STRONG if not is_female else MEDIUM
        if is_female:
            scores["curtain_fringe"] += WEAK
            scores["updo"] -= WEAK
        else:
            scores["longer_hair"] -= MEDIUM
            scores["clean_lines"] -= WEAK

    if traits.get("face_length") == "long" and traits.get("jaw") == "narrow":
        scores["volume_sides"] += WEAK if not is_female else MEDIUM
        if is_female:
            scores["layers"] += MEDIUM
            scores["curtain_fringe"] += WEAK
            scores["volume_top"] -= WEAK
            scores["updo"] -= WEAK
        else:
            scores["short_sides"] -= WEAK

    if traits.get("face_length") == "short" and traits.get("jaw") == "narrow":
        scores["volume_top"] += MEDIUM
        scores["clean_lines"] += WEAK
        scores["fringe"] -= MEDIUM
        scores["volume_sides"] -= WEAK
        if is_female:
            scores["updo"] += MEDIUM
        else:
            scores["short_sides"] += MEDIUM

    if traits.get("jaw") == "wide" and traits.get("chin") == "prominent":
        scores["soft_texture"] += STRONG
        scores["textured_top"] += MEDIUM
        scores["longer_hair"] += WEAK if not is_female else MEDIUM
        scores["short_sides"] -= MEDIUM
        scores["clean_lines"] -= MEDIUM
        if is_female:
            scores["layers"] += MEDIUM
            scores["curtain_fringe"] += WEAK
            scores["updo"] -= WEAK

    if traits.get("jaw") == "narrow" and traits.get("chin") == "recessed":
        scores["clean_lines"] += MEDIUM if not is_female else WEAK
        scores["volume_top"] += WEAK
        scores["soft_texture"] -= WEAK
        if is_female:
            scores["longer_hair"] -= WEAK
            if traits.get("face_length") != "long" and traits.get("forehead") != "high":
                scores["updo"] += WEAK
        else:
            scores["short_sides"] += MEDIUM

    if traits.get("eyes") == "close" and traits.get("forehead") == "low":
        scores["volume_sides"] += MEDIUM
        scores["volume_top"] += WEAK
        scores["soft_texture"] += WEAK
        scores["fringe"] -= STRONG
        if is_female:
            scores["curtain_fringe"] += MEDIUM

    if traits.get("eyes") == "wide" and traits.get("forehead") == "high":
        scores["fringe"] += MEDIUM if not is_female else WEAK
        scores["volume_top"] -= MEDIUM if not is_female else WEAK

    if traits.get("eye_openness") == "narrow" and traits.get("face_length") == "long":
        scores["volume_sides"] += WEAK if not is_female else MEDIUM
        scores["fringe"] -= MEDIUM
        if is_female:
            scores["layers"] += WEAK
            scores["curtain_fringe"] -= WEAK

    if traits.get("eye_openness") == "narrow" and traits.get("forehead") == "high":
        scores["fringe"] += MEDIUM
        scores["volume_top"] -= WEAK
        if is_female:
            scores["curtain_fringe"] += WEAK
            scores["soft_texture"] += WEAK
        else:
            scores["textured_top"] += WEAK

    if (traits.get("facial_thirds") == "lower_dominant" and traits.get("chin") == "prominent"):
        scores["soft_texture"] += MEDIUM
        scores["clean_lines"] -= WEAK
        if is_female:
            scores["layers"] += MEDIUM
        else:
            scores["textured_top"] += MEDIUM

        if traits.get("face_length") != "long":
            scores["volume_top"] += MEDIUM
            if is_female:
                scores["updo"] += WEAK
        else:
            scores["volume_sides"] += MEDIUM
            if is_female:
                scores["curtain_fringe"] += WEAK

    if (traits.get("facial_thirds") == "middle_dominant" 
        and traits.get("face_length") == "long"):
        scores["volume_top"] -= WEAK
        scores["volume_sides"] += WEAK
        if is_female:
            scores["fringe"] += WEAK
            scores["curtain_fringe"] += WEAK
        else:
            scores["fringe"] += WEAK

    if traits.get("symmetry") == "low" and traits.get("jaw") == "wide":
        scores["soft_texture"] += WEAK
        scores["clean_lines"] -= WEAK
        if is_female:
            scores["layers"] += MEDIUM
            scores["updo"] -= WEAK
        else:
            scores["textured_top"] += MEDIUM
            scores["short_sides"] -= WEAK

    return scores

def _apply_symmetry_modulation(scores, traits, gender):
    is_female = gender == "Woman"
    TEXTURE_KEYS = ["soft_texture", "volume_sides", "fringe", "textured_top"]
    CLEAN_KEYS = ["clean_lines"]

    if is_female:
        TEXTURE_KEYS = ["soft_texture", "volume_sides", "fringe",
                        "curtain_fringe", "layers"]
        CLEAN_KEYS   = ["clean_lines", "updo"]
    else:
        CLEAN_KEYS = ["clean_lines", "short_sides"]

    if traits.get("symmetry") == "low":
        for k in TEXTURE_KEYS:
            scores[k] = scores[k] * 1.3 + 1
        for k in CLEAN_KEYS:
            scores[k] = scores[k] * 0.7

    elif traits.get("symmetry") == "high":
        for k in CLEAN_KEYS:
            scores[k] = scores[k] * 1.2
        scores["clean_lines"] += WEAK
        for k in TEXTURE_KEYS:
            scores[k] = scores[k] * 0.9
    return scores

def _apply_hard_constraints(scores, traits):
    if traits.get("hairline") == "receding":
        scores["fringe"] = min(scores["fringe"], -2)
    
    return scores

def clamp_scores(scores, min_score=-5, max_score=10):
    if scores is None:
        raise ValueError("clamp_scores received None")
    return {
        k: max(min_score, min(max_score, v))
        for k, v in scores.items()
    }