-
Notifications
You must be signed in to change notification settings - Fork 24
/
body_part_angle.py
69 lines (56 loc) · 3.06 KB
/
body_part_angle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import mediapipe as mp
import pandas as pd
import numpy as np
import cv2
from utils import *
class BodyPartAngle:
def __init__(self, landmarks):
self.landmarks = landmarks
def angle_of_the_left_arm(self):
l_shoulder = detection_body_part(self.landmarks, "LEFT_SHOULDER")
l_elbow = detection_body_part(self.landmarks, "LEFT_ELBOW")
l_wrist = detection_body_part(self.landmarks, "LEFT_WRIST")
return calculate_angle(l_shoulder, l_elbow, l_wrist)
def angle_of_the_right_arm(self):
r_shoulder = detection_body_part(self.landmarks, "RIGHT_SHOULDER")
r_elbow = detection_body_part(self.landmarks, "RIGHT_ELBOW")
r_wrist = detection_body_part(self.landmarks, "RIGHT_WRIST")
return calculate_angle(r_shoulder, r_elbow, r_wrist)
def angle_of_the_left_leg(self):
l_hip = detection_body_part(self.landmarks, "LEFT_HIP")
l_knee = detection_body_part(self.landmarks, "LEFT_KNEE")
l_ankle = detection_body_part(self.landmarks, "LEFT_ANKLE")
return calculate_angle(l_hip, l_knee, l_ankle)
def angle_of_the_right_leg(self):
r_hip = detection_body_part(self.landmarks, "RIGHT_HIP")
r_knee = detection_body_part(self.landmarks, "RIGHT_KNEE")
r_ankle = detection_body_part(self.landmarks, "RIGHT_ANKLE")
return calculate_angle(r_hip, r_knee, r_ankle)
def angle_of_the_neck(self):
r_shoulder = detection_body_part(self.landmarks, "RIGHT_SHOULDER")
l_shoulder = detection_body_part(self.landmarks, "LEFT_SHOULDER")
r_mouth = detection_body_part(self.landmarks, "MOUTH_RIGHT")
l_mouth = detection_body_part(self.landmarks, "MOUTH_LEFT")
r_hip = detection_body_part(self.landmarks, "RIGHT_HIP")
l_hip = detection_body_part(self.landmarks, "LEFT_HIP")
shoulder_avg = [(r_shoulder[0] + l_shoulder[0]) / 2,
(r_shoulder[1] + l_shoulder[1]) / 2]
mouth_avg = [(r_mouth[0] + l_mouth[0]) / 2,
(r_mouth[1] + l_mouth[1]) / 2]
hip_avg = [(r_hip[0] + l_hip[0]) / 2, (r_hip[1] + l_hip[1]) / 2]
return abs(180 - calculate_angle(mouth_avg, shoulder_avg, hip_avg))
def angle_of_the_abdomen(self):
# calculate angle of the avg shoulder
r_shoulder = detection_body_part(self.landmarks, "RIGHT_SHOULDER")
l_shoulder = detection_body_part(self.landmarks, "LEFT_SHOULDER")
shoulder_avg = [(r_shoulder[0] + l_shoulder[0]) / 2,
(r_shoulder[1] + l_shoulder[1]) / 2]
# calculate angle of the avg hip
r_hip = detection_body_part(self.landmarks, "RIGHT_HIP")
l_hip = detection_body_part(self.landmarks, "LEFT_HIP")
hip_avg = [(r_hip[0] + l_hip[0]) / 2, (r_hip[1] + l_hip[1]) / 2]
# calculate angle of the avg knee
r_knee = detection_body_part(self.landmarks, "RIGHT_KNEE")
l_knee = detection_body_part(self.landmarks, "LEFT_KNEE")
knee_avg = [(r_knee[0] + l_knee[0]) / 2, (r_knee[1] + l_knee[1]) / 2]
return calculate_angle(shoulder_avg, hip_avg, knee_avg)