File size: 54,917 Bytes
941258c
5d4aafa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
from streamlit_webrtc import webrtc_streamer
import av
import streamlit as st
import mediapipe as mp
import cv2
import time
import math
import numpy as np
# import pandas as pd
# import tensorflow as tf
import variables

from download import download_file
from turn import get_ice_servers

# # face bounder indices
# FACE_OVAL=[ 10, 338, 297, 332, 284, 251, 389, 356, 454, 323, 361, 288, 397, 365, 379, 378, 400, 377, 152, 148, 176, 149, 150, 136, 172, 58, 132, 93, 234, 127, 162, 21, 54, 103,67, 109]
# variables
frame_counter = 0
CEF_COUNTER = 0
TOTAL_BLINKS = 0
# constants
CLOSED_EYES_FRAME = 3
FONTS = cv2.FONT_HERSHEY_COMPLEX

# face bounder indices
FACE_OVAL = [10, 338, 297, 332, 284, 251, 389, 356, 454, 323, 361, 288, 397, 365, 379, 378,
             400, 377, 152, 148, 176, 149, 150, 136, 172, 58, 132, 93, 234, 127, 162, 21, 54, 103, 67, 109]

# lips indices for Landmarks
LIPS = [61, 146, 91, 181, 84, 17, 314, 405, 321, 375, 291, 308, 324, 318, 402, 317, 14, 87, 178,
        88, 95, 185, 40, 39, 37, 0, 267, 269, 270, 409, 415, 310, 311, 312, 13, 82, 81, 42, 183, 78]
LOWER_LIPS = [61, 146, 91, 181, 84, 17, 314, 405, 321,
              375, 291, 308, 324, 318, 402, 317, 14, 87, 178, 88, 95]
UPPER_LIPS = [185, 40, 39, 37, 0, 267, 269, 270,
              409, 415, 310, 311, 312, 13, 82, 81, 42, 183, 78]

# Left eyes indices
LEFT_EYE = [362, 382, 381, 380, 374, 373, 390,
            249, 263, 466, 388, 387, 386, 385, 384, 398]
LEFT_EYEBROW = [336, 296, 334, 293, 300, 276, 283, 282, 295, 285]

# right eyes indices
RIGHT_EYE = [33, 7, 163, 144, 145, 153, 154,
             155, 133, 173, 157, 158, 159, 160, 161, 246]
RIGHT_EYEBROW = [70, 63, 105, 66, 107, 55, 65, 52, 53, 46]

UPPER_EYE_LEFT = [246, 161, 160, 159, 158, 157, 173, 133]
UPPER_EYE_RIGHT = [7, 33, 161, 160, 159, 158, 157, 173]

counter = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

full_counter = 0


def landmarksDetection(img, results, draw=False):
    img_height, img_width = img.shape[:2]
    # list[(x,y), (x,y)....]
    mesh_coord = [(int(point.x * img_width), int(point.y * img_height))
                  for point in results.face_landmarks.landmark]
    if draw:
        [cv2.circle(img, p, 2, (0, 0, 255), -1) for p in mesh_coord]

    # returning the list of tuples for each landmarks
    return mesh_coord

# Euclaidean distance


def euclaideanDistance(point, point1):
    x, y = point
    x1, y1 = point1
    distance = math.sqrt((x1 - x)**2 + (y1 - y)**2)
    return distance

# Blinking Ratio


def blinkRatio(img, landmarks, right_indices, left_indices):

    rh_right = landmarks[right_indices[0]]
    rh_left = landmarks[right_indices[8]]
    # vertical line
    rv_top = landmarks[right_indices[12]]
    rv_bottom = landmarks[right_indices[4]]

    lh_right = landmarks[left_indices[0]]
    lh_left = landmarks[left_indices[8]]

    # vertical line
    lv_top = landmarks[left_indices[12]]
    lv_bottom = landmarks[left_indices[4]]

    rhDistance = euclaideanDistance(rh_right, rh_left)
    rvDistance = euclaideanDistance(rv_top, rv_bottom)

    lvDistance = euclaideanDistance(lv_top, lv_bottom)
    lhDistance = euclaideanDistance(lh_right, lh_left)

    reRatio = rhDistance/rvDistance
    leRatio = lhDistance/lvDistance

    ratio = (reRatio+leRatio)/2
    return ratio

# Eyes Extrctor function,


def eyesExtractor(img, right_eye_coords, left_eye_coords):
    # converting color image to  scale image
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # getting the dimension of image
    dim = gray.shape

    # creating mask from gray scale dim
    mask = np.zeros(dim, dtype=np.uint8)

    # drawing Eyes Shape on mask with white color
    cv2.fillPoly(mask, [np.array(right_eye_coords, dtype=np.int32)], 255)
    cv2.fillPoly(mask, [np.array(left_eye_coords, dtype=np.int32)], 255)

    # showing the mask
    # cv.imshow('mask', mask)

    # draw eyes image on mask, where white shape is
    eyes = cv2.bitwise_and(gray, gray, mask=mask)
    # change black color to gray other than eys
    # cv.imshow('eyes draw', eyes)
    eyes[mask == 0] = 155

    # getting minium and maximum x and y  for right and left eyes
    # For Right Eye
    r_max_x = (max(right_eye_coords, key=lambda item: item[0]))[0]
    r_min_x = (min(right_eye_coords, key=lambda item: item[0]))[0]
    r_max_y = (max(right_eye_coords, key=lambda item: item[1]))[1]
    r_min_y = (min(right_eye_coords, key=lambda item: item[1]))[1]

    # For LEFT Eye
    l_max_x = (max(left_eye_coords, key=lambda item: item[0]))[0]
    l_min_x = (min(left_eye_coords, key=lambda item: item[0]))[0]
    l_max_y = (max(left_eye_coords, key=lambda item: item[1]))[1]
    l_min_y = (min(left_eye_coords, key=lambda item: item[1]))[1]

    # croping the eyes from mask
    cropped_right = eyes[r_min_y: r_max_y, r_min_x: r_max_x]
    cropped_left = eyes[l_min_y: l_max_y, l_min_x: l_max_x]

    # returning the cropped eyes
    return cropped_right, cropped_left

# Eyes Postion Estimator


def positionEstimator(cropped_eye):
    # getting height and width of eye
    h, w = cropped_eye.shape

    # remove the noise from images
    gaussain_blur = cv2.GaussianBlur(cropped_eye, (9, 9), 0)
    median_blur = cv2.medianBlur(gaussain_blur, 3)

    # applying thrsholding to convert binary_image
    ret, threshed_eye = cv2.threshold(median_blur, 130, 255, cv2.THRESH_BINARY)

    # create fixd part for eye with
    piece = int(w/3)

    # slicing the eyes into three parts
    right_piece = threshed_eye[0:h, 0:piece]
    center_piece = threshed_eye[0:h, piece: piece+piece]
    left_piece = threshed_eye[0:h, piece + piece:w]

    # calling pixel counter function
    eye_position = pixelCounter(right_piece, center_piece, left_piece)

    return eye_position

# creating pixel counter function


def pixelCounter(first_piece, second_piece, third_piece):
    # counting black pixel in each part
    right_part = np.sum(first_piece == 0)
    center_part = np.sum(second_piece == 0)
    left_part = np.sum(third_piece == 0)
    # creating list of these values
    eye_parts = [right_part, center_part, left_part]

    # getting the index of max values in the list
    max_index = eye_parts.index(max(eye_parts))
    pos_eye = ''
    if max_index == 0:
        pos_eye = "RIGHT"
    elif max_index == 1:
        pos_eye = 'CENTER'
    elif max_index == 2:
        pos_eye = 'LEFT'
    else:
        pos_eye = "Closed"
    return pos_eye

# Function to check if eyes are looking up

# Load your TFLite model
# interpreter = tf.lite.Interpreter(model_path='model_second.tflite')
# interpreter.allocate_tensors()

# # Get input and output details
# input_details = interpreter.get_input_details()
# output_details = interpreter.get_output_details()

# # Assuming your input tensor name is 'flatten_input'
# input_tensor_index = input_details[0]['index']
# output_tensor_index = output_details[0]['index']

# # You can use the following function for inference
# def predict_label(input_data):
#     # Set the input tensor
#     interpreter.set_tensor(input_tensor_index, input_data)

#     # Run inference
#     interpreter.invoke()

#     # Get the output tensor
#     output_data = interpreter.get_tensor(output_tensor_index)

#     # Convert the output to class labels
#     predicted_label = np.argmax(output_data, axis=1)

#     # Decode the predicted label
#     # predicted_class = label_encoder.classes_[predicted_label][0]

#     return predicted_label

# class Queue:
#     def __init__(self, max_size=80):
#         self.max_size = max_size
#         self.queue = []

#     def enqueue(self, value):
#         if len(self.queue) < self.max_size:
#             self.queue.append(value)
#         else:
#             self.queue.pop(0)
#             self.queue.append(value)

#     def dequeue(self):
#         if self.queue:
#             self.queue.pop(0)
#         else:
#             print("Queue is empty. Dequeue operation failed.")

#     def get_all_values(self):
#         return self.queue
    
# frames_list = Queue(max_size=80)

def is_eyes_looking_up(landmarks, upper_eye_indices):
    upper_eye_points = [landmarks[idx] for idx in upper_eye_indices]
    average_y = sum(point[1]
                    for point in upper_eye_points) / len(upper_eye_points)
    return average_y < landmarks[LEFT_EYE[0]][1] and average_y < landmarks[RIGHT_EYE[0]][1]

def gen_report():
    global nerv_bool
    global pos_val
    global report_generated
    global report_data
    global nerv_val
    report_generated = True
    # print("Arsooo")
    # print("Session: ", st.session_state.pos_val)
    if(nerv_bool):
        pos_val += f"Person was Nervous and Blinked {nerv_val} times in 1 Minute"
    report_data = pos_val

report_generated = False
nerv_bool = False
nerv_val = 0
pos_val = ""

our_time = 0

start_time = time.time()

nervous = False

mp_drawing = mp.solutions.drawing_utils  # Drawing helpers
mp_holistic = mp.solutions.holistic  # Mediapipe Solutions

holistic = mp_holistic.Holistic(
    min_detection_confidence=0.5, min_tracking_confidence=0.5)

class VideoProcessor:
    def __init__(self):
        self.reporter = False

    def recv(self, frame: av.VideoFrame) -> av.VideoFrame:
        global full_counter
        global TOTAL_BLINKS
        global CEF_COUNTER
        global CLOSED_EYES_FRAME
        global our_time
        global start_time
        global nervous
        global nerv_bool
        global pos_val
        global report_generated
        global report_data
        global nerv_val
        # global frames_list

        frame = frame.to_ndarray(format="bgr24")

        full_counter += 1

        # Recolor Feed
        image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        image.flags.writeable = False

        # Make Detections
        results = holistic.process(image)

        # Recolor image back to BGR for rendering
        image.flags.writeable = True
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

        img_h, img_w, img_c = image.shape
        face_3d = []
        face_2d = []

        # 1. Draw face landmarks
        mp_drawing.draw_landmarks(image, results.face_landmarks, mp_holistic.FACEMESH_CONTOURS,
                                  mp_drawing.DrawingSpec(
                                      color=(80, 110, 10), thickness=1, circle_radius=1),
                                  mp_drawing.DrawingSpec(
                                      color=(80, 256, 121), thickness=1, circle_radius=1)
                                  )

        # 2. Right hand
        mp_drawing.draw_landmarks(image, results.right_hand_landmarks, mp_holistic.HAND_CONNECTIONS,
                                  mp_drawing.DrawingSpec(
                                      color=(80, 22, 10), thickness=2, circle_radius=4),
                                  mp_drawing.DrawingSpec(
                                      color=(80, 44, 121), thickness=2, circle_radius=2)
                                  )

        # 3. Left Hand
        mp_drawing.draw_landmarks(image, results.left_hand_landmarks, mp_holistic.HAND_CONNECTIONS,
                                  mp_drawing.DrawingSpec(
                                      color=(121, 22, 76), thickness=2, circle_radius=4),
                                  mp_drawing.DrawingSpec(
                                      color=(121, 44, 250), thickness=2, circle_radius=2)
                                  )

        # 4. Pose Detections
        mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_holistic.POSE_CONNECTIONS,
                                  mp_drawing.DrawingSpec(
                                      color=(245, 117, 66), thickness=2, circle_radius=4),
                                  mp_drawing.DrawingSpec(
                                      color=(245, 66, 230), thickness=2, circle_radius=2)
                                  )

        if (results.right_hand_landmarks and results.left_hand_landmarks and results.face_landmarks and results.pose_landmarks):
            right_hand_landmarks = results.right_hand_landmarks.landmark
            left_hand_landmarks = results.left_hand_landmarks.landmark
            pose_landmarks = results.pose_landmarks.landmark
            face_landmarks = results.face_landmarks.landmark

            mesh_coords = landmarksDetection(frame, results, False)

            bottom_lip = face_landmarks[18]
            nose_center = face_landmarks[5]

            right_wrist = pose_landmarks[16]

            lips_left = face_landmarks[287]
            lips_right = face_landmarks[57]

            right_ear_top = face_landmarks[21]
            right_ear_bottom = face_landmarks[215]

            left_ear_top = face_landmarks[389]
            left_ear_bottom = face_landmarks[361]

            forehead = face_landmarks[151]

            bottom_lip_y = int(bottom_lip.y * frame.shape[0])
            upper_nose_y = int(nose_center.y * frame.shape[0])

            lips_left_x = int(lips_left.x * frame.shape[1])
            lips_right_x = int(lips_right.x * frame.shape[1])
            lips_left_y = int(lips_left.y * frame.shape[0])
            lips_right_y = int(lips_right.y * frame.shape[0])

            right_ear_top_y = int(right_ear_top.y * frame.shape[0])
            right_ear_top_x = int(right_ear_top.x * frame.shape[1])
            right_ear_bottom_x = int(right_ear_bottom.x * frame.shape[1])
            right_ear_bottom_y = int(right_ear_bottom.y * frame.shape[0])

            left_ear_top_y = int(left_ear_top.y * frame.shape[0])
            left_ear_top_x = int(left_ear_top.x * frame.shape[1])
            left_ear_bottom_y = int(left_ear_bottom.y * frame.shape[0])
            left_ear_bottom_x = int(left_ear_bottom.x * frame.shape[1])

            right_hand_tip_x = int(
                right_hand_landmarks[mp_holistic.HandLandmark.INDEX_FINGER_TIP].x * frame.shape[1])
            left_hand_tip_x = int(
                left_hand_landmarks[mp_holistic.HandLandmark.INDEX_FINGER_TIP].x * frame.shape[1])
            right_hand_tip_y = int(
                right_hand_landmarks[mp_holistic.HandLandmark.INDEX_FINGER_TIP].y * frame.shape[0])
            left_hand_tip_y = int(
                left_hand_landmarks[mp_holistic.HandLandmark.INDEX_FINGER_TIP].y * frame.shape[0])
            right_hand_dip_y = int(
                right_hand_landmarks[mp_holistic.HandLandmark.INDEX_FINGER_DIP].y * frame.shape[0])
            left_hand_dip_y = int(
                left_hand_landmarks[mp_holistic.HandLandmark.INDEX_FINGER_DIP].y * frame.shape[0])
            right_hand_thumb = int(
                right_hand_landmarks[mp_holistic.HandLandmark.THUMB_TIP].y * frame.shape[0])
            left_hand_thumb = int(
                left_hand_landmarks[mp_holistic.HandLandmark.THUMB_TIP].y * frame.shape[0])
            right_shoulder = pose_landmarks[12]
            left_shoulder = pose_landmarks[11]
            mouth_left = pose_landmarks[9]
            mouth_right = pose_landmarks[10]

            right_hand_middle_finger_y = int(
                right_hand_landmarks[mp_holistic.HandLandmark.MIDDLE_FINGER_TIP].y * frame.shape[0])
            right_hand_middle_finger_x = int(
                right_hand_landmarks[mp_holistic.HandLandmark.MIDDLE_FINGER_TIP].x * frame.shape[1])
            right_hand_ring_finger_y = int(
                right_hand_landmarks[mp_holistic.HandLandmark.RING_FINGER_TIP].y * frame.shape[0])
            right_hand_ring_finger_x = int(
                right_hand_landmarks[mp_holistic.HandLandmark.RING_FINGER_TIP].x * frame.shape[1])
            right_hand_pinky_finger_y = int(
                right_hand_landmarks[mp_holistic.HandLandmark.PINKY_TIP].y * frame.shape[0])
            right_hand_pinky_finger_x = int(
                right_hand_landmarks[mp_holistic.HandLandmark.PINKY_TIP].x * frame.shape[1])

            left_hand_middle_finger_y = int(
                left_hand_landmarks[mp_holistic.HandLandmark.MIDDLE_FINGER_TIP].y * frame.shape[0])
            left_hand_middle_finger_x = int(
                left_hand_landmarks[mp_holistic.HandLandmark.MIDDLE_FINGER_TIP].x * frame.shape[1])
            left_hand_ring_finger_y = int(
                left_hand_landmarks[mp_holistic.HandLandmark.RING_FINGER_TIP].y * frame.shape[0])
            left_hand_ring_finger_x = int(
                left_hand_landmarks[mp_holistic.HandLandmark.RING_FINGER_TIP].x * frame.shape[1])
            left_hand_pinky_finger_y = int(
                left_hand_landmarks[mp_holistic.HandLandmark.PINKY_TIP].y * frame.shape[0])
            left_hand_pinky_finger_x = int(
                left_hand_landmarks[mp_holistic.HandLandmark.PINKY_TIP].x * frame.shape[1])

            forehead_y = int(forehead.y * frame.shape[0])

            right_hand_middle_finger_dip_y = int(
                right_hand_landmarks[11].y * frame.shape[0])
            right_hand_ring_finger_dip_y = int(
                right_hand_landmarks[15].y * frame.shape[0])
            right_hand_pinky_dip_y = int(
                right_hand_landmarks[19].y * frame.shape[0])

            left_hand_middle_finger_dip_y = int(
                right_hand_landmarks[11].y * frame.shape[0])
            left_hand_ring_finger_dip_y = int(
                right_hand_landmarks[15].y * frame.shape[0])
            left_hand_pinky_dip_y = int(
                left_hand_landmarks[19].y * frame.shape[0])

            right_index_middle_finger_distance = right_hand_middle_finger_x - right_hand_tip_x
            right_middle_ring_finger_distance = right_hand_ring_finger_x - \
                right_hand_middle_finger_x

            left_index_middle_finger_distance = left_hand_middle_finger_x - left_hand_tip_x
            right_middle_ring_finger_distance = right_hand_ring_finger_x - \
                right_hand_middle_finger_x

            # eye_pupil_right_x = int(face_landmarks[468].x * frame.shape[1])
            # eye_pupil_left_x = int(face_landmarks[473].x * frame.shape[1])

            ring_finger_distance = right_hand_ring_finger_y - left_hand_ring_finger_y

            mouth_left_y = int(mouth_left.y * frame.shape[0])
            mouth_right_y = int(mouth_right.y * frame.shape[0])
            right_shoulder_y = int(right_shoulder.y * frame.shape[0])
            left_shoulder_y = int(left_shoulder.y * frame.shape[0])

            val_hand_tips = left_hand_tip_x - right_hand_tip_x
            val_hand_thumbs = left_hand_thumb - right_hand_thumb

            lips_left = face_landmarks[287]
            lips_right = face_landmarks[57]

            lips_left_x = int(lips_left.x * frame.shape[1])
            lips_right_x = int(lips_right.x * frame.shape[1])

            face_landmarks = results.face_landmarks.landmark

            thumb_tip = right_hand_landmarks[mp_holistic.HandLandmark.THUMB_TIP]
            index_finger_tip = right_hand_landmarks[mp_holistic.HandLandmark.INDEX_FINGER_TIP]
            right_cheek = face_landmarks[116]
            right_ear = face_landmarks[147]
            chin = face_landmarks[152]
            left_cheek = face_landmarks[323]

            thumb_tip_y = int(thumb_tip.y * frame.shape[0])
            thumb_tip_x = int(thumb_tip.x * frame.shape[1])
            index_finger_tip_y = int(index_finger_tip.y * frame.shape[0])
            index_finger_tip_x = int(index_finger_tip.x * frame.shape[1])
            cheek_y = int(right_cheek.y * frame.shape[0])
            right_ear_x = int(right_ear.x * frame.shape[1])
            chin_y = int(chin.y * frame.shape[0])
            left_cheek_x = int(left_cheek.x * frame.shape[1])

            lips_left_y_distance = lips_left_y - lips_right_y
            lips_right_y_distance = lips_right_y - lips_left_y

            index_finger_distance_x = left_hand_tip_x - right_hand_tip_x
            if ((right_hand_tip_y < right_hand_middle_finger_y) and (right_hand_tip_y < right_hand_ring_finger_y) and (right_hand_tip_y < right_hand_pinky_finger_y) and (right_hand_tip_y < thumb_tip_y)):
                if ((right_hand_tip_y > upper_nose_y) and (right_hand_tip_y < bottom_lip_y) and (right_hand_tip_x > lips_left_x) and (right_hand_tip_x < lips_right_x)):
                    cv2.putText(image, "Disagree with Spoken Word", (50, 50),
                                cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                    counter[0] += 1
            elif ((right_hand_tip_y > right_hand_dip_y) and (right_hand_middle_finger_y > right_hand_middle_finger_dip_y) and (right_hand_ring_finger_y > right_hand_ring_finger_dip_y) and (right_hand_pinky_finger_y > right_hand_pinky_dip_y) and (left_hand_tip_y > left_hand_dip_y) and (left_hand_middle_finger_y > left_hand_middle_finger_dip_y) and (left_hand_ring_finger_y > left_hand_ring_finger_dip_y) and (left_hand_pinky_finger_y > left_hand_pinky_dip_y) and (right_hand_tip_x > (right_ear_top_x-40)) and (right_hand_tip_x < right_ear_top_x) and (left_hand_tip_x < (left_ear_top_x+40)) and (left_hand_tip_x > left_ear_top_x) and (lips_left_y > lips_right_y or lips_right_y > lips_left_y) and ((lips_left_y_distance > 1 and lips_left_y_distance < 40) or (lips_right_y_distance > 1 and lips_right_y_distance < 40))):
                cv2.putText(image, "Annoyed", (50, 50),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                counter[2] += 1
            elif ((right_hand_tip_y < right_shoulder_y and left_hand_tip_y < left_shoulder_y) and (right_hand_tip_y > mouth_right_y) and (left_hand_tip_y > mouth_left_y) and (right_hand_dip_y > right_hand_tip_y and left_hand_dip_y > left_hand_tip_y) and (left_hand_tip_x < lips_left_x) and (right_hand_tip_x > lips_right_x) and (val_hand_tips < 20 and val_hand_thumbs < 20)):
                cv2.putText(image, "Wants her knowledge to be recognized now",
                            (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                counter[8] += 1
            elif (((right_hand_tip_y < right_hand_dip_y) and (right_hand_middle_finger_y < right_hand_middle_finger_dip_y) and (right_hand_ring_finger_y < right_hand_ring_finger_dip_y) and (right_hand_tip_y < right_hand_dip_y)) and ((left_hand_tip_y < left_hand_dip_y) and (left_hand_middle_finger_y < left_hand_middle_finger_dip_y) and (left_hand_ring_finger_y < left_hand_ring_finger_dip_y) and (left_hand_pinky_finger_y < left_hand_pinky_dip_y)) and (is_eyes_looking_up(mesh_coords, UPPER_EYE_LEFT + UPPER_EYE_RIGHT)) and (right_hand_tip_y < chin_y and right_hand_tip_y > forehead_y) and (left_hand_tip_y < chin_y and left_hand_tip_y > forehead_y) and (right_hand_tip_x > right_ear_top_x and left_hand_tip_x < left_ear_top_x) and (index_finger_distance_x > 20)):
                cv2.putText(image, "Reobserve, Feeling Uncomfortable", (50, 50),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                counter[1] += 1
            else:
                cv2.putText(image, "Neutral", (50, 50),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                counter[7] += 1

        elif results.face_landmarks and results.right_hand_landmarks and results.pose_landmarks:
            face_landmarks = results.face_landmarks.landmark
            right_hand_landmarks = results.right_hand_landmarks.landmark
            pose_landmarks = results.pose_landmarks.landmark

            for idx, lm in enumerate(face_landmarks):
                if idx == 33 or idx == 263 or idx == 1 or idx == 61 or idx == 291 or idx == 199:
                    if idx == 1:
                        nose_2d = (lm.x * img_w, lm.y * img_h)
                        nose_3d = (lm.x * img_w, lm.y * img_h, lm.z * 3000)

                    x, y = int(lm.x * img_w), int(lm.y * img_h)

                    # Get the 2D Coordinates
                    face_2d.append([x, y])

                    # Get the 3D Coordinates
                    face_3d.append([x, y, lm.z])

            # Convert it to the NumPy array
            face_2d = np.array(face_2d, dtype=np.float64)

            # Convert it to the NumPy array
            face_3d = np.array(face_3d, dtype=np.float64)

            # The camera matrix
            focal_length = 1 * img_w

            cam_matrix = np.array([[focal_length, 0, img_h / 2],
                                   [0, focal_length, img_w / 2],
                                   [0, 0, 1]])

            # The distortion parameters
            dist_matrix = np.zeros((4, 1), dtype=np.float64)

            # Solve PnP
            success, rot_vec, trans_vec = cv2.solvePnP(
                face_3d, face_2d, cam_matrix, dist_matrix)

            # Get rotational matrix
            rmat, jac = cv2.Rodrigues(rot_vec)

            # Get angles
            angles, mtxR, mtxQ, Qx, Qy, Qz = cv2.RQDecomp3x3(rmat)

            # Get the y rotation degree
            x = angles[0] * 360
            y = angles[1] * 360
            z = angles[2] * 360

            head_text = ""

            # See where the user's head tilting
            if y < -10:
                head_text = "Looking Left"
            elif y > 10:
                head_text = "Looking Right"
            elif x < -10:
                head_text = "Looking Down"
            elif x > 10:
                head_text = "Looking Up"
            else:
                head_text = "Forward"

            # Display the nose direction
            nose_3d_projection, jacobian = cv2.projectPoints(
                nose_3d, rot_vec, trans_vec, cam_matrix, dist_matrix)

            p1 = (int(nose_2d[0]), int(nose_2d[1]))
            p2 = (int(nose_2d[0] + y * 10), int(nose_2d[1] - x * 10))

            # cv2.line(image, p1, p2, (255, 0, 0), 3)

            # Add the text on the image
            # cv2.putText(image, head_text, (20, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 2)
            # cv2.putText(image, "x: " + str(np.round(x,2)), (500, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
            # cv2.putText(image, "y: " + str(np.round(y,2)), (500, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
            # cv2.putText(image, "z: " + str(np.round(z,2)), (500, 150), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)

            mesh_coords = landmarksDetection(frame, results, False)

            thumb_tip = right_hand_landmarks[mp_holistic.HandLandmark.THUMB_TIP]
            index_finger_tip = right_hand_landmarks[mp_holistic.HandLandmark.INDEX_FINGER_TIP]
            right_cheek = face_landmarks[116]
            right_ear = face_landmarks[147]
            chin = face_landmarks[152]
            left_cheek = face_landmarks[323]

            thumb_tip_y = int(thumb_tip.y * frame.shape[0])
            thumb_tip_x = int(thumb_tip.x * frame.shape[1])
            index_finger_tip_y = int(index_finger_tip.y * frame.shape[0])
            index_finger_tip_x = int(index_finger_tip.x * frame.shape[1])
            cheek_y = int(right_cheek.y * frame.shape[0])
            right_ear_x = int(right_ear.x * frame.shape[1])
            chin_y = int(chin.y * frame.shape[0])
            left_cheek_x = int(left_cheek.x * frame.shape[1])

            right_wrist = pose_landmarks[16]

            right_wrist_y = int(right_wrist.y * frame.shape[0])

            head_top = face_landmarks[10]
            head_below = face_landmarks[152]

            nose_top = face_landmarks[197]
            nose_bottom = face_landmarks[4]
            nose_left = face_landmarks[49]
            nose_right = face_landmarks[279]

            nose_top_y = int(nose_top.y * frame.shape[0])
            nose_bottom_y = int(nose_bottom.y * frame.shape[0])
            nose_left_x = int(nose_left.x * frame.shape[1])
            nose_right_x = int(nose_right.x * frame.shape[1])

            head_top_y = int(head_top.y * frame.shape[0])
            head_below_y = int(head_below.y * frame.shape[0])

            mouth_lip_upper = face_landmarks[13]
            mouth_lip_lower = face_landmarks[14]

            bottom_lip = face_landmarks[18]
            nose_center = face_landmarks[1]

            right_ear_top = face_landmarks[21]
            right_ear_bottom = face_landmarks[215]

            lips_left = face_landmarks[287]
            lips_right = face_landmarks[57]

            bottom_lip_y = int(bottom_lip.y * frame.shape[0])
            upper_nose_y = int(nose_center.y * frame.shape[0])

            lips_left_x = int(lips_left.x * frame.shape[1])
            lips_right_x = int(lips_right.x * frame.shape[1])

            left_cheek = face_landmarks[323]

            right_ear_top_y = int(right_ear_top.y * frame.shape[0])
            right_ear_top_x = int(right_ear_top.x * frame.shape[1])
            right_ear_bottom_x = int(right_ear_bottom.x * frame.shape[1])
            right_ear_bottom_y = int(right_ear_bottom.y * frame.shape[0])

            mouth_lip_upper_y = int(mouth_lip_upper.y * frame.shape[0])
            mouth_lip_lower_y = int(mouth_lip_lower.y * frame.shape[0])

            right_hand_tip_x = int(
                right_hand_landmarks[mp_holistic.HandLandmark.INDEX_FINGER_TIP].x * frame.shape[1])
            right_hand_tip_y = int(
                right_hand_landmarks[mp_holistic.HandLandmark.INDEX_FINGER_TIP].y * frame.shape[0])

            thumb_tip = right_hand_landmarks[mp_holistic.HandLandmark.THUMB_TIP]

            head_top_x = int(head_top.x * frame.shape[1])
            right_wrist_x = int(right_wrist.x * frame.shape[1])

            thumb_tip_y = int(thumb_tip.y * frame.shape[0])
            thumb_tip_x = int(thumb_tip.x * frame.shape[1])
            chin = face_landmarks[152]
            chin_y = int(chin.y * frame.shape[0])

            lips_distance = mouth_lip_lower_y - mouth_lip_upper_y

            right_ear_hand_distance = head_top_x - right_wrist_x

            right_hand_middle_finger_y = int(
                right_hand_landmarks[mp_holistic.HandLandmark.MIDDLE_FINGER_TIP].y * frame.shape[0])
            right_hand_middle_finger_x = int(
                right_hand_landmarks[mp_holistic.HandLandmark.MIDDLE_FINGER_TIP].x * frame.shape[1])
            right_hand_ring_finger_y = int(
                right_hand_landmarks[mp_holistic.HandLandmark.RING_FINGER_TIP].y * frame.shape[0])
            right_hand_ring_finger_x = int(
                right_hand_landmarks[mp_holistic.HandLandmark.RING_FINGER_TIP].x * frame.shape[1])
            right_hand_pinky_finger_y = int(
                right_hand_landmarks[mp_holistic.HandLandmark.PINKY_TIP].y * frame.shape[0])
            right_hand_pinky_finger_x = int(
                right_hand_landmarks[mp_holistic.HandLandmark.PINKY_TIP].x * frame.shape[1])

            left_cheek_x = int(left_cheek.x * frame.shape[1])

            if ((right_hand_tip_y < right_hand_middle_finger_y) and (right_hand_tip_y < right_hand_ring_finger_y) and (right_hand_tip_y < right_hand_pinky_finger_y) and (right_hand_tip_y < thumb_tip_y) and (right_hand_tip_y > upper_nose_y) and (right_hand_tip_y < bottom_lip_y) and (right_hand_tip_x < lips_left_x) and (right_hand_tip_x > lips_right_x)):
                cv2.putText(image, "Disagree with Spoken Word", (50, 50),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                counter[0] += 1
            elif ((right_hand_tip_y > right_ear_top_y) and (right_hand_tip_y < right_ear_bottom_y) and (right_hand_tip_x > (right_ear_top_x-40)) and (right_hand_tip_x < right_ear_top_x)):
                cv2.putText(image, "Disagree what was heard", (50, 50),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                counter[3] += 1
            elif (lips_distance > 10):
                cv2.putText(image, "Disbelief", (50, 50),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                counter[4] += 1
            elif ((right_hand_tip_y < nose_bottom_y) and (right_hand_tip_y > nose_top_y) and (right_hand_tip_x > nose_left_x) and (right_hand_tip_x < nose_right_x)):
                cv2.putText(image, "Untruthful", (50, 50),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                counter[5] += 1
            elif ((right_wrist_y > head_top_y) and (right_wrist_y < head_below_y) and (right_ear_hand_distance < 100) and (head_text == "Looking Down")):
                cv2.putText(image, "Embarrased, Got Caught", (50, 50),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                counter[6] += 1
            elif ((chin_y > thumb_tip_y) and (right_ear_x < thumb_tip_x) and (thumb_tip_y > cheek_y) and (thumb_tip_x < left_cheek_x) and (head_text == "Looking Up")) or ((chin_y > index_finger_tip_y) and (right_ear_x < index_finger_tip_x) and (index_finger_tip_y > cheek_y) and (index_finger_tip_x < left_cheek_x) and (head_text == "Looking Up")):
                cv2.putText(image, "Positive evaluation low risk situation", (50, 50),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                counter[9] += 1
            else:
                cv2.putText(image, "Neutral", (50, 50),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                counter[7] += 1

        elif results.face_landmarks and results.left_hand_landmarks and results.pose_landmarks:
            face_landmarks = results.face_landmarks.landmark
            left_hand_landmarks = results.left_hand_landmarks.landmark
            pose_landmarks = results.pose_landmarks.landmark

            for idx, lm in enumerate(face_landmarks):
                if idx == 33 or idx == 263 or idx == 1 or idx == 61 or idx == 291 or idx == 199:
                    if idx == 1:
                        nose_2d = (lm.x * img_w, lm.y * img_h)
                        nose_3d = (lm.x * img_w, lm.y * img_h, lm.z * 3000)

                    x, y = int(lm.x * img_w), int(lm.y * img_h)

                    # Get the 2D Coordinates
                    face_2d.append([x, y])

                    # Get the 3D Coordinates
                    face_3d.append([x, y, lm.z])

            # Convert it to the NumPy array
            face_2d = np.array(face_2d, dtype=np.float64)

            # Convert it to the NumPy array
            face_3d = np.array(face_3d, dtype=np.float64)

            # The camera matrix
            focal_length = 1 * img_w

            cam_matrix = np.array([[focal_length, 0, img_h / 2],
                                   [0, focal_length, img_w / 2],
                                   [0, 0, 1]])

            # The distortion parameters
            dist_matrix = np.zeros((4, 1), dtype=np.float64)

            # Solve PnP
            success, rot_vec, trans_vec = cv2.solvePnP(
                face_3d, face_2d, cam_matrix, dist_matrix)

            # Get rotational matrix
            rmat, jac = cv2.Rodrigues(rot_vec)

            # Get angles
            angles, mtxR, mtxQ, Qx, Qy, Qz = cv2.RQDecomp3x3(rmat)

            # Get the y rotation degree
            x = angles[0] * 360
            y = angles[1] * 360
            z = angles[2] * 360

            head_text = ""

            # See where the user's head tilting
            if y < -10:
                head_text = "Looking Left"
            elif y > 10:
                head_text = "Looking Right"
            elif x < -10:
                head_text = "Looking Down"
            elif x > 10:
                head_text = "Looking Up"
            else:
                head_text = "Forward"

            # Display the nose direction
            nose_3d_projection, jacobian = cv2.projectPoints(
                nose_3d, rot_vec, trans_vec, cam_matrix, dist_matrix)

            p1 = (int(nose_2d[0]), int(nose_2d[1]))
            p2 = (int(nose_2d[0] + y * 10), int(nose_2d[1] - x * 10))

            # cv2.line(image, p1, p2, (255, 0, 0), 3)

            # Add the text on the image
            # cv2.putText(image, head_text, (20, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 2)
            # cv2.putText(image, "x: " + str(np.round(x,2)), (500, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
            # cv2.putText(image, "y: " + str(np.round(y,2)), (500, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
            # cv2.putText(image, "z: " + str(np.round(z,2)), (500, 150), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)

            mesh_coords = landmarksDetection(frame, results, False)

            thumb_tip = left_hand_landmarks[mp_holistic.HandLandmark.THUMB_TIP]
            index_finger_tip = left_hand_landmarks[mp_holistic.HandLandmark.INDEX_FINGER_TIP]
            left_cheek = face_landmarks[345]
            left_ear = face_landmarks[376]
            chin = face_landmarks[152]
            left_cheek = face_landmarks[352]

            thumb_tip_y = int(thumb_tip.y * frame.shape[0])
            thumb_tip_x = int(thumb_tip.x * frame.shape[1])
            index_finger_tip_y = int(index_finger_tip.y * frame.shape[0])
            index_finger_tip_x = int(index_finger_tip.x * frame.shape[1])
            cheek_y = int(left_cheek.y * frame.shape[0])
            left_ear_x = int(left_ear.x * frame.shape[1])
            chin_y = int(chin.y * frame.shape[0])
            left_cheek_x = int(left_cheek.x * frame.shape[1])

            left_wrist = pose_landmarks[15]

            left_wrist_y = int(left_wrist.y * frame.shape[0])

            head_top = face_landmarks[10]
            head_below = face_landmarks[152]

            nose_top = face_landmarks[197]
            nose_bottom = face_landmarks[4]
            nose_left = face_landmarks[49]
            nose_right = face_landmarks[279]

            nose_top_y = int(nose_top.y * frame.shape[0])
            nose_bottom_y = int(nose_bottom.y * frame.shape[0])
            nose_left_x = int(nose_left.x * frame.shape[1])
            nose_right_x = int(nose_right.x * frame.shape[1])

            head_top_y = int(head_top.y * frame.shape[0])
            head_below_y = int(head_below.y * frame.shape[0])

            mouth_lip_upper = face_landmarks[13]
            mouth_lip_lower = face_landmarks[14]

            bottom_lip = face_landmarks[18]
            nose_center = face_landmarks[1]

            left_ear_top = face_landmarks[251]
            left_ear_bottom = face_landmarks[435]

            lips_left = face_landmarks[287]
            lips_right = face_landmarks[57]

            bottom_lip_y = int(bottom_lip.y * frame.shape[0])
            upper_nose_y = int(nose_center.y * frame.shape[0])

            lips_left_x = int(lips_left.x * frame.shape[1])
            lips_right_x = int(lips_right.x * frame.shape[1])

            left_cheek = face_landmarks[323]

            left_ear_top_y = int(left_ear_top.y * frame.shape[0])
            left_ear_top_x = int(left_ear_top.x * frame.shape[1])
            left_ear_bottom_x = int(left_ear_bottom.x * frame.shape[1])
            left_ear_bottom_y = int(left_ear_bottom.y * frame.shape[0])

            mouth_lip_upper_y = int(mouth_lip_upper.y * frame.shape[0])
            mouth_lip_lower_y = int(mouth_lip_lower.y * frame.shape[0])

            left_hand_tip_x = int(
                left_hand_landmarks[mp_holistic.HandLandmark.INDEX_FINGER_TIP].x * frame.shape[1])
            left_hand_tip_y = int(
                left_hand_landmarks[mp_holistic.HandLandmark.INDEX_FINGER_TIP].y * frame.shape[0])

            thumb_tip = left_hand_landmarks[mp_holistic.HandLandmark.THUMB_TIP]

            head_top_x = int(head_top.x * frame.shape[1])
            left_wrist_x = int(left_wrist.x * frame.shape[1])

            thumb_tip_y = int(thumb_tip.y * frame.shape[0])
            thumb_tip_x = int(thumb_tip.x * frame.shape[1])
            chin = face_landmarks[152]
            chin_y = int(chin.y * frame.shape[0])

            lips_distance = mouth_lip_lower_y - mouth_lip_upper_y

            left_ear_hand_distance = left_wrist_x - head_top_x

            left_hand_middle_finger_y = int(
                left_hand_landmarks[mp_holistic.HandLandmark.MIDDLE_FINGER_TIP].y * frame.shape[0])
            left_hand_middle_finger_x = int(
                left_hand_landmarks[mp_holistic.HandLandmark.MIDDLE_FINGER_TIP].x * frame.shape[1])
            left_hand_ring_finger_y = int(
                left_hand_landmarks[mp_holistic.HandLandmark.RING_FINGER_TIP].y * frame.shape[0])
            left_hand_ring_finger_x = int(
                left_hand_landmarks[mp_holistic.HandLandmark.RING_FINGER_TIP].x * frame.shape[1])
            left_hand_pinky_finger_y = int(
                left_hand_landmarks[mp_holistic.HandLandmark.PINKY_TIP].y * frame.shape[0])
            left_hand_pinky_finger_x = int(
                left_hand_landmarks[mp_holistic.HandLandmark.PINKY_TIP].x * frame.shape[1])

            left_cheek_x = int(left_cheek.x * frame.shape[1])

            if ((left_hand_tip_y < left_hand_middle_finger_y) and (left_hand_tip_y < left_hand_ring_finger_y) and (left_hand_tip_y < left_hand_pinky_finger_y) and (left_hand_tip_y < thumb_tip_y) and (left_hand_tip_y > upper_nose_y) and (left_hand_tip_y < bottom_lip_y) and (left_hand_tip_x < lips_left_x) and (left_hand_tip_x > lips_right_x)):
                cv2.putText(image, "Disagree with Spoken Word", (50, 50),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                counter[0] += 1
            elif ((left_hand_tip_y > left_ear_top_y) and (left_hand_tip_y < left_ear_bottom_y) and (left_hand_tip_x < (left_ear_top_x+40)) and (left_hand_tip_x > left_ear_top_x)):
                cv2.putText(image, "Disagree what was heard", (50, 50),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                counter[3] += 1
            elif (lips_distance > 10):
                cv2.putText(image, "Disbelief", (50, 50),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                counter[4] += 1
            elif ((left_hand_tip_y < nose_bottom_y) and (left_hand_tip_y > nose_top_y) and (left_hand_tip_x > nose_left_x) and (left_hand_tip_x < nose_right_x)):
                cv2.putText(image, "Untruthful", (50, 50),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                counter[5] += 1
            elif ((left_wrist_y > head_top_y) and (left_wrist_y < head_below_y) and (left_ear_hand_distance < 100) and (head_text == "Looking Down")):
                cv2.putText(image, "Embarrased, Got Caught", (50, 50),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                counter[6] += 1
            elif ((chin_y > thumb_tip_y) and (left_ear_x > thumb_tip_x) and (thumb_tip_y > cheek_y) and (thumb_tip_x > left_cheek_x) and (head_text == "Looking Up")) or ((chin_y > index_finger_tip_y) and (left_ear_x > index_finger_tip_x) and (index_finger_tip_y > cheek_y) and (index_finger_tip_x > left_cheek_x) and (head_text == "Looking Up")):
                cv2.putText(image, "Positive evaluation low risk situation", (50, 50),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                counter[9] += 1
            else:
                cv2.putText(image, "Neutral", (50, 50),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                counter[7] += 1

        elif results.pose_landmarks and results.face_landmarks:
            face_landmarks = results.face_landmarks.landmark
            pose_landmarks = results.pose_landmarks.landmark

            for idx, lm in enumerate(face_landmarks):
                if idx == 33 or idx == 263 or idx == 1 or idx == 61 or idx == 291 or idx == 199:
                    if idx == 1:
                        nose_2d = (lm.x * img_w, lm.y * img_h)
                        nose_3d = (lm.x * img_w, lm.y * img_h, lm.z * 3000)

                    x, y = int(lm.x * img_w), int(lm.y * img_h)

                    # Get the 2D Coordinates
                    face_2d.append([x, y])

                    # Get the 3D Coordinates
                    face_3d.append([x, y, lm.z])

            # Convert it to the NumPy array
            face_2d = np.array(face_2d, dtype=np.float64)

            # Convert it to the NumPy array
            face_3d = np.array(face_3d, dtype=np.float64)

            # The camera matrix
            focal_length = 1 * img_w

            cam_matrix = np.array([[focal_length, 0, img_h / 2],
                                   [0, focal_length, img_w / 2],
                                   [0, 0, 1]])

            # The distortion parameters
            dist_matrix = np.zeros((4, 1), dtype=np.float64)

            # Solve PnP
            success, rot_vec, trans_vec = cv2.solvePnP(
                face_3d, face_2d, cam_matrix, dist_matrix)

            # Get rotational matrix
            rmat, jac = cv2.Rodrigues(rot_vec)

            # Get angles
            angles, mtxR, mtxQ, Qx, Qy, Qz = cv2.RQDecomp3x3(rmat)

            # Get the y rotation degree
            x = angles[0] * 360
            y = angles[1] * 360
            z = angles[2] * 360

            head_text = ""

            # See where the user's head tilting
            if y < -10:
                head_text = "Looking Left"
            elif y > 10:
                head_text = "Looking Right"
            elif x < -10:
                head_text = "Looking Down"
            elif x > 10:
                head_text = "Looking Up"
            else:
                head_text = "Forward"

            # Display the nose direction
            nose_3d_projection, jacobian = cv2.projectPoints(
                nose_3d, rot_vec, trans_vec, cam_matrix, dist_matrix)

            p1 = (int(nose_2d[0]), int(nose_2d[1]))
            p2 = (int(nose_2d[0] + y * 10), int(nose_2d[1] - x * 10))

            mesh_coords = landmarksDetection(frame, results, False)

            right_wrist = pose_landmarks[16]

            right_wrist_x = int(right_wrist.x * frame.shape[1])
            right_wrist_y = int(right_wrist.y * frame.shape[0])

            head_top = face_landmarks[10]
            head_below = face_landmarks[152]

            head_top_y = int(head_top.y * frame.shape[0])
            head_below_y = int(head_below.y * frame.shape[0])

            head_top_x = int(head_top.x * frame.shape[1])

            right_ear_hand_distance = head_top_x - right_wrist_x

            ratio = blinkRatio(frame, mesh_coords, RIGHT_EYE, LEFT_EYE)
            # cv.putText(frame, f'ratio {ratio}', (100, 100), FONTS, 1.0, utils.GREEN, 2)

            if ratio > 5.5:
                CEF_COUNTER += 1
                # cv.putText(frame, 'Blink', (200, 50), FONTS, 1.3, utils.PINK, 2)
                pass
            else:
                if CEF_COUNTER > CLOSED_EYES_FRAME:
                    TOTAL_BLINKS += 1
                    CEF_COUNTER = 0
            # # cv.putText(frame, f'Total Blinks: {TOTAL_BLINKS}', (100, 150), FONTS, 0.6, utils.GREEN, 2)
            # Blink Detector Counter Completed
            right_coords = [mesh_coords[p] for p in RIGHT_EYE]
            left_coords = [mesh_coords[p] for p in LEFT_EYE]
            crop_right, crop_left = eyesExtractor(
                frame, right_coords, left_coords)

            eye_position = positionEstimator(crop_right)
            eye_position_left = positionEstimator(crop_left)

            mouth_lip_upper = face_landmarks[13]
            mouth_lip_lower = face_landmarks[14]

            right_eye_brow = face_landmarks[105]
            left_eye_brow = face_landmarks[334]

            right_eye_brow_upper = int(right_eye_brow.y * frame.shape[0])
            left_eye_brow_upper = int(left_eye_brow.y * frame.shape[0])

            distance_y_eye_brows = right_eye_brow_upper - left_eye_brow_upper
            # print(distance_y_eye_brows)

            mouth_lip_upper_y = int(mouth_lip_upper.y * frame.shape[0])
            mouth_lip_lower_y = int(mouth_lip_lower.y * frame.shape[0])

            lips_distance = mouth_lip_lower_y - mouth_lip_upper_y

            frames_list.enqueue(x)
            frames_list.enqueue(y)

            if ((right_wrist_y > head_top_y) and (right_wrist_y < head_below_y) and (right_ear_hand_distance < 100) and (head_text == "Looking Down")):
                cv2.putText(image, "Embarrased, Got Caught", (50, 50),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                counter[6] += 1
            elif (lips_distance > 10):
                cv2.putText(image, "Disbelief", (50, 50),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                counter[4] += 1
            elif (head_text == "Looking Up" and ((eye_position == "LEFT" and eye_position_left == "LEFT") or (eye_position == "RIGHT" and eye_position_left == "RIGHT"))):
                cv2.putText(image, "Person Recalling Something", (50, 50),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                counter[17] += 1
            elif((left_eye_brow_upper < right_eye_brow_upper) and (distance_y_eye_brows > 4 and distance_y_eye_brows < 10)):
                cv2.putText(image, "Anger, Dislike, Skeptical", (50, 50),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                counter[18] += 1
            elif (ratio > 5.5):
                cv2.putText(image, "Doesn't want to see, eye block", (50, 50),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                counter[11] += 1
            elif (((eye_position == "Closed" and eye_position_left == "CENTER") or (eye_position_left == "Closed" and eye_position == "CENTER")) and lips_distance > 50):
                cv2.putText(image, "Approval", (50, 50),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                counter[16] += 1
            elif((eye_position == "RIGHT" and eye_position_left == "RIGHT") or (eye_position == "LEFT" and eye_position_left == "LEFT")):
                cv2.putText(image, "Hostility, skeptical", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                counter[19] += 1
            else:
                cv2.putText(image, "Disagree with Spoken Word", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                counter[7] += 1
                # if(len(frames_list.get_all_values()) == 80):
                #     input_data = np.array(frames_list.get_all_values())
                #     input_data = input_data.reshape((40, 2))
                #     predicted_class = predict_label(np.expand_dims(input_data, axis=0).astype(np.float32))
                #     if(predicted_class[0] == 3):
                #         cv2.putText(image, "Head Shake Slow", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                #         counter[12] += 1
                #     elif(predicted_class[0] == 2):
                #         cv2.putText(image, "Head Shake Fast", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                #         counter[13] += 1
                #     elif(predicted_class[0] == 1):
                #         cv2.putText(image, "Head Nod Slow", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                #         counter[14] += 1
                #     elif(predicted_class[0] == 0):
                #         cv2.putText(image, "Head Nod Fast", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
                #         counter[15] += 1
                #     else:
                #         cv2.putText(image, "Neutral", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
                #         counter[7] += 1
                # else:
                #     cv2.putText(image, "Neutral", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
                #     counter[7] += 1
        del results
        end_time = time.time()
        timer_count = end_time - start_time
        if((int(timer_count/60) > our_time and not nervous) or (int(timer_count) <= 60 and TOTAL_BLINKS >= 15)):
            our_time = int(timer_count/60)
            print("Sunny")
            if(TOTAL_BLINKS >= 15):
                print("Arsooo")
                nervous = True
                variables.nerv_val = TOTAL_BLINKS
                variables.nerv_bool = True

        pos_val = f"""
        Disagree with Spoken Word: {(counter[0]/full_counter * 100)}%\n
        Wants Her Knowledge to be Recognized Now: {(counter[8]/full_counter * 100)}%\n
        Reobserve feeling Uncomfortable: {(counter[1]/full_counter * 100)}%\n
        Positive Evaluation Low Risk Situation: {(counter[9]/full_counter * 100)}%\n
        Annoyed: {(counter[2]/full_counter * 100)}%\n
        Disagree what was heard: {(counter[3]/full_counter * 100)}%\n
        Disbelief: {(counter[4]/full_counter * 100)}%\n
        Untruthful: {(counter[5]/full_counter * 100)}%\n
        Embarrased Got Caught: {(counter[6]/full_counter * 100)}%\n
        Doesn't want to see, Eye Block: {(counter[11]/full_counter * 100)}%\n
        Person Recalling Something: {(counter[17]/full_counter * 100)}%\n
        Approval: {(counter[16]/full_counter * 100)}%\n
        Anger, Dislike, Skeptical: {(counter[18]/full_counter * 100)}%\n
        Hostility, Skeptical: {(counter[19]/full_counter * 100)}%\n
        Neutral: {(counter[7]/full_counter * 100)}%\n
        """
        self.reporter = True
        variables.my_variable += 10
        variables.my_report = pos_val

        return av.VideoFrame.from_ndarray(image, format='bgr24')

ctx = webrtc_streamer(key="key",
                video_processor_factory=VideoProcessor,
                rtc_configuration={
                    "iceServers": get_ice_servers(),
                    "iceTransportPolicy": "relay",
                },
                media_stream_constraints={"video": True, "audio": False},
                # async_processing=True
                )

if(ctx.video_transformer):
    variables.my_variable = 10
    variables.my_report = ""
    variables.nerv_bool = False
    variables.nerv_val = 0
elif(not ctx.video_transformer and variables.my_variable > 10):
    # print(variables.my_report)
    if(variables.nerv_bool):
        variables.my_report += f"Person was Nervous and Blinked {variables.nerv_val} times in 1 Minute"
    st.write(variables.my_report)
    variables.my_variable = 10
    variables.my_report = ""
    variables.nerv_bool = False
    variables.nerv_val = 0
    # del frames_list
else:
    variables.my_variable = 10
    variables.my_report = ""
    variables.nerv_bool = False
    variables.nerv_val = 0
    st.write(variables.my_report)
    # del frames_list