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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
|
function analyseCriteria(face) {
// Note: MediaPipe uses camera perspective (mirrored), so left/right are swapped
// leftIris uses rightEyeIris because from camera's view, right eye is on left side
let points = {
leftIris: face.annotations.rightEyeIris[0],
rightIris: face.annotations.leftEyeIris[0],
leftLateralCanthus: face.annotations.rightEyeLower1[0],
leftMedialCanthus: face.annotations.rightEyeLower1[7],
rightLateralCanthus: face.annotations.leftEyeLower1[0],
rightMedialCanthus: face.annotations.leftEyeLower1[7],
leftEyeUpper: face.annotations.rightEyeUpper0[4],
leftEyeLower: face.annotations.rightEyeLower0[4],
rightEyeUpper: face.annotations.leftEyeUpper0[4],
rightEyeLower: face.annotations.leftEyeLower0[4],
leftEyebrow: face.annotations.rightEyebrowUpper[6],
rightEyebrow: face.annotations.leftEyebrowUpper[6],
leftZygo: face.annotations.silhouette[28],
rightZygo: face.annotations.silhouette[8],
noseBottom: face.annotations.noseBottom[0],
leftNoseCorner: face.annotations.noseRightCorner[0],
rightNoseCorner: face.annotations.noseLeftCorner[0],
leftCupidBow: face.annotations.lipsUpperOuter[4],
lipSeparation: face.annotations.lipsUpperInner[5],
rightCupidBow: face.annotations.lipsUpperOuter[6],
leftLipCorner: face.annotations.lipsUpperOuter[0],
rightLipCorner: face.annotations.lipsUpperOuter[10],
lowerLip: face.annotations.lipsLowerOuter[4],
upperLip: face.annotations.lipsUpperOuter[5],
leftGonial: face.annotations.silhouette[24],
rightGonial: face.annotations.silhouette[12],
chinLeft: face.annotations.silhouette[19],
chinTip: face.annotations.silhouette[18],
chinRight: face.annotations.silhouette[17],
};
// Validate critical points exist
for (let key in points) {
if (!points[key] || !Array.isArray(points[key]) || points[key].length < 2) {
console.warn(`Missing or invalid point: ${key}`);
}
}
return [
points,
{
midfaceRatio: new MidfaceRatio(face, points),
facialWidthToHeightRatio: new FacialWidthToHeightRatio(face, points),
chinToPhiltrumRatio: new ChinToPhiltrumRatio(face, points),
canthalTilt: new CanthalTilt(face, points),
mouthToNoseRatio: new MouthToNoseRatio(face, points),
bigonialWidth: new BigonialWidth(face, points),
lipRatio: new LipRatio(face, points),
eyeSeparationRatio: new EyeSeparationRatio(face, points),
eyeToMouthAngle: new EyeToMouthAngle(face, points),
lowerThirdHeight: new LowerThirdHeight(face, points),
palpebralFissureLength: new PalpebralFissureLength(face, points),
eyeColor: new EyeColor(face, points),
},
];
}
async function setupDatabase() {
return await fetch("database.json")
.then((res) => res.text())
.then((text) => {
console.log("✅ database.json loaded");
return JSON.parse(text);
})
.catch((err) => {
console.error("❌ Failed to load or parse database.json:", err);
return { entries: {} };
});
}
class Criteria {
constructor(face, points) {
this.face = face;
this.points = points;
for (let i in points) {
if (points.hasOwnProperty(i)) {
Object.defineProperty(this, i, { get: () => this.points[i] });
}
}
return this;
}
createPoint(name, value) {
this.points[name] = value;
Object.defineProperty(this, name, { get: () => this.points[name] });
}
calculate() {
/* abstract */
}
render() {
/* abstract */
}
ideal() {
/* abstract */
}
assess() {
/* abstract */
}
draw(ctx) {
/* abstract */
}
necessaryPoints() {
/* abstract */
}
}
class MidfaceRatio extends Criteria {
constructor(face, points) {
super(face, points);
let bottomLine = Fn.fromTwoPoints(this.leftCupidBow, this.rightCupidBow);
let leftLine = bottomLine.perpendicular(this.leftIris);
let rightLine = bottomLine.perpendicular(this.rightIris);
this.createPoint("bottomLeftMidface", bottomLine.intersect(leftLine));
this.createPoint("bottomRightMidface", bottomLine.intersect(rightLine));
}
calculate() {
let eyeDistance = distance(this.leftIris, this.rightIris);
let leftDistance = distance(this.leftIris, this.bottomLeftMidface);
let rightDistance = distance(this.rightIris, this.bottomRightMidface);
// Avoid division by zero
if (leftDistance < 1e-10 || rightDistance < 1e-10) {
console.warn("MidfaceRatio: distance too small, using fallback");
this.ratio = 0;
return;
}
this.ratio = (eyeDistance / leftDistance + eyeDistance / rightDistance) / 2;
}
render() {
return `${round(this.ratio, 2)}`;
}
ideal() {
return `${database.entries.midfaceRatio.idealLower} to ${database.entries.midfaceRatio.idealUpper}`;
}
assess() {
let { idealLower, idealUpper, deviation, deviatingLow, deviatingHigh } =
database.entries.midfaceRatio;
return assess(
this.ratio,
idealLower,
idealUpper,
deviation,
deviatingLow,
deviatingHigh
);
}
draw(ctx) {
draw(ctx, "red", [
this.leftIris,
this.rightIris,
this.bottomRightMidface,
this.bottomLeftMidface,
]);
}
necessaryPoints() {
return ["leftIris", "rightIris", "bottomLeftMidface", "bottomRightMidface"];
}
}
class FacialWidthToHeightRatio extends Criteria {
constructor(face, points) {
super(face, points);
let topLine = Fn.fromTwoPoints(this.leftEyeUpper, this.rightEyeUpper);
let bottomLine = Fn.fromTwoPoints(this.leftCupidBow, this.rightCupidBow);
let leftLine = topLine.perpendicular(this.leftZygo);
let rightLine = topLine.perpendicular(this.rightZygo);
this.createPoint("topLeft", leftLine.intersect(topLine));
this.createPoint("topRight", rightLine.intersect(topLine));
this.createPoint("bottomLeft", leftLine.intersect(bottomLine));
this.createPoint("bottomRight", rightLine.intersect(bottomLine));
}
calculate() {
let topWidth = distance(this.topLeft, this.topRight);
let leftHeight = distance(this.topLeft, this.bottomLeft);
let bottomWidth = distance(this.bottomLeft, this.bottomRight);
let rightHeight = distance(this.topRight, this.bottomRight);
// Avoid division by zero
if (leftHeight < 1e-10 || rightHeight < 1e-10) {
console.warn(
"FacialWidthToHeightRatio: height too small, using fallback"
);
this.ratio = 0;
return;
}
this.ratio = (topWidth / leftHeight + bottomWidth / rightHeight) / 2;
}
render() {
return `${round(this.ratio, 2)}`;
}
ideal() {
return `more than ${database.entries.facialWidthToHeightRatio.idealLower}`;
}
assess() {
let { idealLower, deviation, deviatingLow } =
database.entries.facialWidthToHeightRatio;
return assess(
this.ratio,
idealLower,
undefined,
deviation,
deviatingLow,
undefined
);
}
draw(ctx) {
draw(ctx, "lightblue", [
this.topLeft,
this.topRight,
this.bottomRight,
this.bottomLeft,
]);
}
necessaryPoints() {
return ["topLeft", "topRight", "bottomLeft", "bottomRight"];
}
}
class ChinToPhiltrumRatio extends Criteria {
calculate() {
let chinDistance = distance(this.chinTip, this.lowerLip);
let philtrumDistance = distance(this.upperLip, this.noseBottom);
// Avoid division by zero
if (philtrumDistance < 1e-10) {
console.warn(
"ChinToPhiltrumRatio: philtrumDistance too small, using fallback"
);
this.ratio = 0;
return;
}
this.ratio = chinDistance / philtrumDistance;
}
render() {
return `${round(this.ratio, 2)}`;
}
ideal() {
return `${database.entries.chinToPhiltrumRatio.idealLower} to ${database.entries.chinToPhiltrumRatio.idealUpper}`;
}
assess() {
let { idealLower, idealUpper, deviation, deviatingLow, deviatingHigh } =
database.entries.chinToPhiltrumRatio;
return assess(
this.ratio,
idealLower,
idealUpper,
deviation,
deviatingLow,
deviatingHigh
);
}
draw(ctx) {
draw(ctx, "blue", [this.chinTip, this.lowerLip]);
draw(ctx, "blue", [this.upperLip, this.noseBottom]);
}
necessaryPoints() {
return ["chinTip", "lowerLip", "upperLip", "noseBottom"];
}
}
class CanthalTilt extends Criteria {
calculate() {
let line = [
this.rightZygo[0] - this.leftZygo[0],
this.rightZygo[1] - this.leftZygo[1],
];
let lineFn = Fn.fromTwoPoints(this.rightZygo, this.leftZygo);
let left = [
this.leftLateralCanthus[0] - this.leftMedialCanthus[0],
this.leftLateralCanthus[1] - this.leftMedialCanthus[1],
];
let right = [
this.rightLateralCanthus[0] - this.rightMedialCanthus[0],
this.rightLateralCanthus[1] - this.rightMedialCanthus[1],
];
let pointOnLeftLine = lineFn.getY(this.leftMedialCanthus[0]) + left[1];
let pointOnRightLine = lineFn.getY(this.rightMedialCanthus[0]) + right[1];
// Calculate left canthal tilt
let lineMagnitude = Math.sqrt(line[0] ** 2 + line[1] ** 2);
let leftMagnitude = Math.sqrt(left[0] ** 2 + left[1] ** 2);
let rightMagnitude = Math.sqrt(right[0] ** 2 + right[1] ** 2);
if (lineMagnitude < 1e-10 || leftMagnitude < 1e-10) {
console.warn("CanthalTilt: left calculation - magnitude too small");
this.leftCanthalTilt = 0;
} else {
let leftDotProduct = Math.abs(
-1 * (line[0] * left[0] + line[1] * left[1])
);
let leftCosAngle = leftDotProduct / (lineMagnitude * leftMagnitude);
leftCosAngle = Math.max(-1, Math.min(1, leftCosAngle)); // Clamp for acos
this.leftCanthalTilt =
Math.acos(leftCosAngle) *
(180 / Math.PI) *
(lineFn.getY(this.leftLateralCanthus[0]) - pointOnLeftLine > 0
? 1
: -1);
}
// Calculate right canthal tilt
if (lineMagnitude < 1e-10 || rightMagnitude < 1e-10) {
console.warn("CanthalTilt: right calculation - magnitude too small");
this.rightCanthalTilt = 0;
} else {
let rightDotProduct = Math.abs(line[0] * right[0] + line[1] * right[1]);
let rightCosAngle = rightDotProduct / (lineMagnitude * rightMagnitude);
rightCosAngle = Math.max(-1, Math.min(1, rightCosAngle)); // Clamp for acos
this.rightCanthalTilt =
Math.acos(rightCosAngle) *
(180 / Math.PI) *
(lineFn.getY(this.rightLateralCanthus[0]) - pointOnRightLine > 0
? 1
: -1);
}
}
render() {
return `left ${round(this.rightCanthalTilt, 0)}°, right ${round(
this.leftCanthalTilt,
0
)}°`;
}
ideal() {
return `more than ${database.entries.canthalTilt.idealLower}`;
}
assess() {
let { idealLower, deviation, deviatingLow } = database.entries.canthalTilt;
return assess(
(this.leftCanthalTilt + this.rightCanthalTilt) / 2,
idealLower,
undefined,
deviation,
deviatingLow,
undefined
);
}
draw(ctx) {
draw(ctx, "pink", [this.leftLateralCanthus, this.leftMedialCanthus]);
draw(ctx, "pink", [this.rightLateralCanthus, this.rightMedialCanthus]);
}
necessaryPoints() {
return [
"leftLateralCanthus",
"leftMedialCanthus",
"rightLateralCanthus",
"rightMedialCanthus",
];
}
}
class MouthToNoseRatio extends Criteria {
calculate() {
let mouthWidth = distance(this.leftLipCorner, this.rightLipCorner);
let noseWidth = distance(this.leftNoseCorner, this.rightNoseCorner);
// Avoid division by zero
if (noseWidth < 1e-10) {
console.warn("MouthToNoseRatio: noseWidth too small, using fallback");
this.ratio = 0;
return;
}
this.ratio = mouthWidth / noseWidth;
}
render() {
return `${round(this.ratio, 2)}`;
}
ideal() {
return `${database.entries.mouthToNoseRatio.idealLower} to ${database.entries.mouthToNoseRatio.idealUpper}`;
}
assess() {
let { idealLower, idealUpper, deviation, deviatingLow, deviatingHigh } =
database.entries.mouthToNoseRatio;
return assess(
this.ratio,
idealLower,
idealUpper,
deviation,
deviatingLow,
deviatingHigh
);
}
draw(ctx) {
draw(ctx, "purple", [this.leftLipCorner, this.rightLipCorner]);
draw(ctx, "purple", [this.leftNoseCorner, this.rightNoseCorner]);
}
necessaryPoints() {
return [
"leftLipCorner",
"rightLipCorner",
"leftNoseCorner",
"rightNoseCorner",
];
}
}
class BigonialWidth extends Criteria {
calculate() {
let zygomaticWidth = distance(this.leftZygo, this.rightZygo);
let gonialWidth = distance(this.leftGonial, this.rightGonial);
// Avoid division by zero
if (gonialWidth < 1e-10) {
console.warn("BigonialWidth: gonialWidth too small, using fallback");
this.ratio = 0;
return;
}
this.ratio = zygomaticWidth / gonialWidth;
}
render() {
return `${round(this.ratio, 2)}`;
}
ideal() {
return `${database.entries.bigonialWidth.idealLower} to ${database.entries.bigonialWidth.idealUpper}`;
}
assess() {
let { idealLower, idealUpper, deviation, deviatingLow, deviatingHigh } =
database.entries.bigonialWidth;
return assess(
this.ratio,
idealLower,
idealUpper,
deviation,
deviatingLow,
deviatingHigh
);
}
draw(ctx) {
draw(ctx, "gold", [this.leftGonial, this.rightGonial]);
draw(ctx, "gold", [this.leftZygo, this.rightZygo]);
}
necessaryPoints() {
return ["leftZygo", "rightZygo", "leftGonial", "rightGonial"];
}
}
class LipRatio extends Criteria {
constructor(face, points) {
super(face, points);
let topLip = Fn.fromTwoPoints(this.leftCupidBow, this.rightCupidBow);
let lowerLip = topLip.parallel(this.lowerLip);
this.createPoint(
"upperLipEnd",
topLip.intersect(topLip.perpendicular(this.lipSeparation))
);
this.createPoint(
"lowerLipEnd",
lowerLip.intersect(lowerLip.perpendicular(this.lipSeparation))
);
}
calculate() {
let lowerDistance = distance(this.lowerLipEnd, this.lipSeparation);
let upperDistance = distance(this.upperLipEnd, this.lipSeparation);
// Avoid division by zero
if (upperDistance < 1e-10) {
console.warn("LipRatio: upperDistance too small, using fallback");
this.ratio = 0;
return;
}
this.ratio = lowerDistance / upperDistance;
}
render() {
return `${round(this.ratio, 2)}`;
}
ideal() {
return `${database.entries.lipRatio.idealLower} to ${database.entries.lipRatio.idealUpper}`;
}
assess() {
let { idealLower, idealUpper, deviation, deviatingLow, deviatingHigh } =
database.entries.lipRatio;
return assess(
this.ratio,
idealLower,
idealUpper,
deviation,
deviatingLow,
deviatingHigh
);
}
draw(ctx) {
draw(ctx, "lightgreen", [this.upperLipEnd, this.lipSeparation]);
draw(ctx, "lightgreen", [this.lipSeparation, this.lowerLipEnd]);
}
necessaryPoints() {
return ["upperLipEnd", "lowerLipEnd", "lipSeparation"];
}
}
class EyeSeparationRatio extends Criteria {
calculate() {
let eyeDistance = distance(this.leftIris, this.rightIris);
let faceWidth = distance(this.leftZygo, this.rightZygo);
// Avoid division by zero
if (faceWidth < 1e-10) {
console.warn("EyeSeparationRatio: faceWidth too small, using fallback");
this.ratio = 0;
return;
}
this.ratio = eyeDistance / faceWidth;
}
render() {
return `${round(this.ratio, 2)}`;
}
ideal() {
return `${database.entries.eyeSeparationRatio.idealLower} to ${database.entries.eyeSeparationRatio.idealUpper}`;
}
assess() {
let { idealLower, idealUpper, deviation, deviatingLow, deviatingHigh } =
database.entries.eyeSeparationRatio;
return assess(
this.ratio,
idealLower,
idealUpper,
deviation,
deviatingLow,
deviatingHigh
);
}
draw(ctx) {
draw(ctx, "orange", [this.leftIris, this.rightIris]);
draw(ctx, "orange", [this.leftZygo, this.rightZygo]);
}
necessaryPoints() {
return ["leftIris", "rightIris", "leftZygo", "rightZygo"];
}
}
class EyeToMouthAngle extends Criteria {
calculate() {
let a = [
this.leftIris[0] - this.lipSeparation[0],
this.leftIris[1] - this.lipSeparation[1],
];
let b = [
this.rightIris[0] - this.lipSeparation[0],
this.rightIris[1] - this.lipSeparation[1],
];
let dotProduct = a[0] * b[0] + a[1] * b[1];
let magnitudeA = Math.sqrt(a[0] ** 2 + a[1] ** 2);
let magnitudeB = Math.sqrt(b[0] ** 2 + b[1] ** 2);
// Avoid division by zero and clamp acos input to valid range [-1, 1]
if (magnitudeA < 1e-10 || magnitudeB < 1e-10) {
console.warn("EyeToMouthAngle: magnitude too small, using fallback");
this.angle = 0;
return;
}
let cosAngle = dotProduct / (magnitudeA * magnitudeB);
// Clamp to valid range for acos
cosAngle = Math.max(-1, Math.min(1, cosAngle));
this.angle = Math.acos(cosAngle) * (180 / Math.PI);
}
render() {
return `${round(this.angle, 0)}°`;
}
ideal() {
return `${database.entries.eyeToMouthAngle.idealLower}° to ${database.entries.eyeToMouthAngle.idealUpper}°`;
}
assess() {
let { idealLower, idealUpper, deviation, deviatingLow, deviatingHigh } =
database.entries.eyeToMouthAngle;
return assess(
this.angle,
idealLower,
idealUpper,
deviation,
deviatingLow,
deviatingHigh
);
}
draw(ctx) {
draw(ctx, "brown", [
this.leftIris,
this.lipSeparation,
this.rightIris,
this.lipSeparation,
this.leftIris,
]);
}
necessaryPoints() {
return ["leftIris", "lipSeparation", "rightIris"];
}
}
class LowerThirdHeight extends Criteria {
calculate() {
const MIDPOINT_FACTOR = 0.5; // Constant for midpoint calculation
let middlePoint = [
this.leftNoseCorner[0] +
MIDPOINT_FACTOR * (this.rightNoseCorner[0] - this.leftNoseCorner[0]),
this.leftNoseCorner[1] +
MIDPOINT_FACTOR * (this.rightNoseCorner[1] - this.leftNoseCorner[1]),
];
let middleLine = Fn.fromTwoPoints(
this.leftNoseCorner,
this.rightNoseCorner
).perpendicular(middlePoint);
let topPoint = middleLine.intersect(
Fn.fromTwoPoints(this.leftEyebrow, this.rightEyebrow)
);
let bottomPoint = middleLine.intersect(
Fn.fromTwoPoints(this.chinLeft, this.chinRight)
);
let bottomDistance = distance(bottomPoint, middlePoint);
let topDistance = distance(middlePoint, topPoint);
// Avoid division by zero
if (topDistance < 1e-10) {
console.warn("LowerThirdHeight: topDistance too small, using fallback");
this.ratio = 0;
return;
}
this.ratio = bottomDistance / topDistance;
}
render() {
return `${round(this.ratio, 2)}`;
}
ideal() {
return `more than ${database.entries.lowerThirdHeight.idealLower}`;
}
assess() {
let { idealLower, deviation, deviatingLow } =
database.entries.lowerThirdHeight;
return assess(
this.ratio,
idealLower,
undefined,
deviation,
deviatingLow,
undefined
);
}
draw(ctx) {
draw(ctx, "grey", [
this.leftEyebrow,
this.rightEyebrow,
this.rightNoseCorner,
this.leftNoseCorner,
]);
draw(ctx, "grey", [
this.leftNoseCorner,
this.rightNoseCorner,
this.chinRight,
this.chinLeft,
]);
}
necessaryPoints() {
return [
"leftNoseCorner",
"rightNoseCorner",
"leftEyebrow",
"rightEyebrow",
"chinLeft",
"chinRight",
];
}
}
class PalpebralFissureLength extends Criteria {
calculate() {
let leftCanthalDistance = distance(
this.leftLateralCanthus,
this.leftMedialCanthus
);
let leftEyeHeight = distance(this.leftEyeUpper, this.leftEyeLower);
let rightCanthalDistance = distance(
this.rightLateralCanthus,
this.rightMedialCanthus
);
let rightEyeHeight = distance(this.rightEyeUpper, this.rightEyeLower);
// Avoid division by zero
if (leftEyeHeight < 1e-10) {
console.warn("PalpebralFissureLength: leftEyeHeight too small");
this.leftPFL = 0;
} else {
this.leftPFL = leftCanthalDistance / leftEyeHeight;
}
if (rightEyeHeight < 1e-10) {
console.warn("PalpebralFissureLength: rightEyeHeight too small");
this.rightPFL = 0;
} else {
this.rightPFL = rightCanthalDistance / rightEyeHeight;
}
}
render() {
return `left ${round(this.rightPFL, 2)}, right ${round(this.leftPFL, 2)}`;
}
ideal() {
return `more than ${database.entries.palpebralFissureLength.idealLower}`;
}
assess() {
let { idealLower, deviation, deviatingLow } =
database.entries.palpebralFissureLength;
return assess(
(this.leftPFL + this.rightPFL) / 2,
idealLower,
undefined,
deviation,
deviatingLow,
undefined
);
}
draw(ctx) {
draw(ctx, "aquamarine", [this.leftLateralCanthus, this.leftMedialCanthus]);
draw(ctx, "aquamarine", [this.leftEyeUpper, this.leftEyeLower]);
draw(ctx, "aquamarine", [
this.rightLateralCanthus,
this.rightMedialCanthus,
]);
draw(ctx, "aquamarine", [this.rightEyeUpper, this.rightEyeLower]);
}
necessaryPoints() {
return [
"leftLateralCanthus",
"leftMedialCanthus",
"leftEyeUpper",
"leftEyeLower",
"rightLateralCanthus",
"rightMedialCanthus",
"rightEyeUpper",
"rightEyeLower",
];
}
}
class EyeColor extends Criteria {
calculate() {
this.leftIrisCoordinates = this.face.annotations.rightEyeIris;
this.leftIrisWidth =
this.leftIrisCoordinates[1][0] - this.leftIrisCoordinates[3][0];
this.leftIrisHeight =
this.leftIrisCoordinates[4][1] - this.leftIrisCoordinates[2][1];
this.rightIrisCoordinates = this.face.annotations.leftEyeIris;
this.rightIrisWidth =
this.rightIrisCoordinates[3][0] - this.rightIrisCoordinates[1][0];
this.rightIrisHeight =
this.rightIrisCoordinates[4][1] - this.rightIrisCoordinates[2][1];
}
render() {
return `<canvas height="0" width="0"></canvas><canvas height="0" width="0"></canvas>`;
}
ideal() {
return "";
}
assess() {
return "";
}
detect(image, [ctx0, ctx1]) {
ctx0.canvas.width = this.leftIrisWidth;
ctx0.canvas.height = this.leftIrisHeight;
ctx0.drawImage(
image,
this.leftIrisCoordinates[3][0],
this.leftIrisCoordinates[2][1],
this.leftIrisWidth,
this.leftIrisHeight,
0,
0,
this.leftIrisWidth,
this.leftIrisHeight
);
ctx1.canvas.width = this.rightIrisWidth;
ctx1.canvas.height = this.rightIrisHeight;
ctx1.drawImage(
image,
this.rightIrisCoordinates[1][0],
this.rightIrisCoordinates[2][1],
this.rightIrisWidth,
this.rightIrisHeight,
0,
0,
this.rightIrisWidth,
this.rightIrisHeight
);
}
necessaryPoints() {
return [];
}
}
function distance([ax, ay], [bx, by]) {
return Math.sqrt((ax - bx) ** 2 + (ay - by) ** 2);
}
class Fn {
constructor(a, b) {
// y = ax + b
this.a = a;
this.b = b;
this.slope = this.a;
this.yintersect = this.b;
}
static fromTwoPoints([ax, ay], [bx, by]) {
// Handle vertical line (same x-coordinate)
if (Math.abs(ax - bx) < 1e-10) {
// Return a vertical line representation
// For vertical lines, we'll use a very large slope and special handling
return new Fn(Infinity, ax); // Store x-coordinate in b for vertical lines
}
// y = (ay - by) / (ax - bx) * (x - ax) + ay
return Fn.fromOffset((ay - by) / (ax - bx), ax, ay);
}
static fromOffset(a, b, c) {
// y = a * (x - b) + c
return new Fn(a, c - a * b);
}
getY(x) {
// Handle vertical line
if (!isFinite(this.a)) {
return this.b; // For vertical lines, b stores the x-coordinate
}
return this.a * x + this.b;
}
perpendicular([x, y]) {
// Handle vertical line (perpendicular is horizontal)
if (!isFinite(this.a)) {
return new Fn(0, y); // Horizontal line
}
// Handle horizontal line (perpendicular is vertical)
if (Math.abs(this.a) < 1e-10) {
return new Fn(Infinity, x); // Vertical line
}
return Fn.fromOffset(-1 * (1 / this.a), x, y);
}
parallel([x, y]) {
return Fn.fromOffset(this.a, x, y);
}
intersect(fn) {
// Handle vertical lines
if (!isFinite(this.a)) {
// This line is vertical at x = this.b
if (!isFinite(fn.a)) {
// Both are vertical - parallel or same line
return [this.b, 0]; // Return arbitrary point
}
let x = this.b;
return [x, fn.getY(x)];
}
if (!isFinite(fn.a)) {
// Other line is vertical at x = fn.b
let x = fn.b;
return [x, this.getY(x)];
}
// Handle parallel lines
if (Math.abs(this.a - fn.a) < 1e-10) {
// Lines are parallel, return midpoint or first point
return [0, this.b];
}
let x = (fn.b - this.b) / (this.a - fn.a);
return [x, this.getY(x)];
}
draw(ctx, color) {
let points = [];
for (let i = 0; i < ctx.canvas.width; i += 1) {
points.push([i, this.getY(i)]);
}
draw(ctx, color || "red", points);
}
}
function round(n, digits) {
digits = 10 ** (isNaN(digits) ? 2 : digits);
return Math.round(n * digits) / digits;
}
function assess(
value,
idealLower,
idealUpper,
deviation,
deviatingLow,
deviatingHigh
) {
// Validate inputs
if (typeof value !== "number" || !isFinite(value)) {
console.warn("Invalid value in assess:", value);
return '<span class="deviation-4">invalid measurement</span>';
}
if (deviation <= 0) {
console.warn("Invalid deviation in assess:", deviation);
return '<span class="deviation-4">invalid configuration</span>';
}
function renderMultiplier(multiplier) {
if (multiplier === 0) {
return "slightly too";
} else if (multiplier === 1) {
return "noticeably";
} else if (multiplier === 2) {
return "significantly too";
} else if (multiplier === 3) {
return "horribly";
} else {
return "extremely";
}
}
function calculate(value, idealLower, idealUpper, deviation) {
// Check if value is in ideal range
if (idealUpper !== undefined && idealLower !== undefined) {
if (idealUpper >= value && idealLower <= value) {
return {
type: "perfect",
};
}
} else if (
(idealUpper && !idealLower && value <= idealUpper) ||
(!idealUpper && idealLower && value >= idealLower)
) {
return {
type: "perfect",
};
}
// Calculate deviation multiplier (don't mutate the original value)
if (idealLower !== undefined && value < idealLower) {
let multiplier = 0;
let testValue = value;
while ((testValue += deviation) < idealLower) {
multiplier++;
}
return {
type: "low",
multiplier: Math.min(multiplier, 4),
text: renderMultiplier(multiplier),
};
}
if (idealUpper !== undefined && value > idealUpper) {
let multiplier = 0;
let testValue = value;
while ((testValue -= deviation) > idealUpper) {
multiplier++;
}
return {
type: "high",
multiplier: Math.min(multiplier, 4),
text: renderMultiplier(multiplier),
};
}
// Fallback (shouldn't reach here)
return {
type: "perfect",
};
}
let result = calculate(value, idealLower, idealUpper, deviation);
if (!result) {
return '<span class="deviation-4">calculation error</span>';
}
let { type, multiplier, text } = result;
if (type === "perfect") {
return `<span class="perfect">perfect</span>`;
} else if (type === "low") {
return `<span class="deviation-${multiplier}">${text} ${deviatingLow}</span>`;
} else if (type === "high") {
return `<span class="deviation-${multiplier}">${text} ${deviatingHigh}</span>`;
}
}
// Cache watermark settings to avoid recalculating on every draw
let watermarkCache = null;
function draw(ctx, color, points) {
// Validate inputs
if (!points || points.length === 0) {
return;
}
// Validate points are valid coordinates
for (let point of points) {
if (
!Array.isArray(point) ||
point.length < 2 ||
!isFinite(point[0]) ||
!isFinite(point[1])
) {
console.warn("Invalid point in draw:", point);
return;
}
}
ctx.strokeStyle = color;
ctx.fillStyle = color;
let current = points[0];
// Draw watermark only once per canvas (cache the settings)
if (
!watermarkCache ||
watermarkCache.canvasWidth !== canvas.width ||
watermarkCache.canvasHeight !== canvas.height
) {
watermarkCache = {
canvasWidth: canvas.width,
canvasHeight: canvas.height,
fontBase: canvas.width * 0.6,
fontSize: 20,
text: "Rysk: Facial Analysis",
};
var ratio = watermarkCache.fontSize / watermarkCache.fontBase;
watermarkCache.size = canvas.width * ratio;
watermarkCache.font =
canvas.width > 600
? watermarkCache.size + "px sans-serif"
: "18px sans-serif";
}
// Draw watermark (only if not already drawn on this frame)
if (!ctx._watermarkDrawn) {
ctx.save();
ctx.font = watermarkCache.font;
ctx.globalAlpha = 0.5;
var textWidth = ctx.measureText(watermarkCache.text).width;
var cw = watermarkCache.canvasWidth;
var ch = watermarkCache.canvasHeight;
// Gray shadow
ctx.fillStyle = "gray";
ctx.fillText(watermarkCache.text, cw - textWidth - 10, ch - 20);
// White text
ctx.fillStyle = "white";
ctx.fillText(watermarkCache.text, cw - textWidth - 10 + 2, ch - 20 + 2);
ctx.restore();
ctx._watermarkDrawn = true;
}
// Draw the actual shape/points
for (let i of points.concat([points[0]])) {
let [x, y] = i;
ctx.beginPath();
ctx.moveTo(current[0], current[1]);
ctx.lineTo(x, y);
ctx.stroke();
ctx.beginPath();
ctx.arc(x, y, ctx.arcRadius || 3, 0, 2 * Math.PI);
ctx.fill();
current = i;
}
}
|