-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbody_part_angle.py
125 lines (98 loc) · 5.39 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
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
'''
This file is originally from "Sports With AI" https://github.com/Furkan-Gulsen/Sport-With-AI/blob/main/body_part_angle.py
'''
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)
#小腿與地面的角度
def angle_of_the_calf_horizon(self):
# 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]
# calculate angle of the avg ankle
l_ankle = detection_body_part(self.landmarks, "LEFT_ANKLE")
r_ankle = detection_body_part(self.landmarks, "RIGHT_ANKLE")
ankle_avg = [(r_ankle[0] + l_ankle[0]) / 2, (r_ankle[1] + l_ankle[1]) / 2]
#腳踝的水平面
calf_horizon = [((r_ankle[0] + l_ankle[0]) / 2) + 0.1, (r_ankle[1] + l_ankle[1]) / 2]
return calculate_angle(knee_avg, ankle_avg, calf_horizon)
#左腋下角度
def angle_of_the_left_shoulder(self):
l_hip = detection_body_part(self.landmarks, "LEFT_HIP")
l_shoulder = detection_body_part(self.landmarks, "LEFT_SHOULDER")
l_elbow = detection_body_part(self.landmarks, "LEFT_ELBOW")
return calculate_angle(l_hip, l_shoulder, l_elbow)
#右腋下角度
def angle_of_the_right_shoulder(self):
r_hip = detection_body_part(self.landmarks, "RIGHT_HIP")
r_shoulder = detection_body_part(self.landmarks, "RIGHT_SHOULDER")
r_elbow = detection_body_part(self.landmarks, "RIGHT_ELBOW")
return calculate_angle(r_hip, r_shoulder, r_elbow)
#左手肘與地平面的角度
def left_angle_of_the_elbow_horizon(self):
l_elbow = detection_body_part(self.landmarks, "LEFT_ELBOW")
l_wrist = detection_body_part(self.landmarks, "LEFT_WRIST")
#手肘的水平面
elbow_horizon = [l_elbow[0]-0.1, l_elbow[1]]
return calculate_angle(l_wrist, l_elbow, elbow_horizon)
#右手肘與地平面的角度
def right_angle_of_the_elbow_horizon(self):
r_elbow = detection_body_part(self.landmarks, "RIGHT_ELBOW")
r_wrist = detection_body_part(self.landmarks, "RIGHT_WRIST")
#手肘的水平面
elbow_horizon = [r_elbow[0]+0.1, r_elbow[1]]
return calculate_angle(r_wrist, r_elbow, elbow_horizon)