Update app.py
Browse files
app.py
CHANGED
@@ -16,6 +16,183 @@ TITLE = "MediaPipe Human Pose Estimation"
|
|
16 |
DESCRIPTION = "https://google.github.io/mediapipe/"
|
17 |
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
def run(
|
20 |
image: np.ndarray,
|
21 |
model_complexity: int,
|
|
|
16 |
DESCRIPTION = "https://google.github.io/mediapipe/"
|
17 |
|
18 |
|
19 |
+
def calculateAngle(landmark1, landmark2, landmark3):
|
20 |
+
'''
|
21 |
+
This function calculates angle between three different landmarks.
|
22 |
+
Args:
|
23 |
+
landmark1: The first landmark containing the x,y and z coordinates.
|
24 |
+
landmark2: The second landmark containing the x,y and z coordinates.
|
25 |
+
landmark3: The third landmark containing the x,y and z coordinates.
|
26 |
+
Returns:
|
27 |
+
angle: The calculated angle between the three landmarks.
|
28 |
+
|
29 |
+
'''
|
30 |
+
|
31 |
+
# Get the required landmarks coordinates.
|
32 |
+
x1, y1, _ = landmark1
|
33 |
+
x2, y2, _ = landmark2
|
34 |
+
x3, y3, _ = landmark3
|
35 |
+
|
36 |
+
# Calculate the angle between the three points
|
37 |
+
angle = math.degrees(math.atan2(y3 - y2, x3 - x2) - math.atan2(y1 - y2, x1 - x2))
|
38 |
+
|
39 |
+
# Check if the angle is less than zero.
|
40 |
+
if angle < 0:
|
41 |
+
|
42 |
+
# Add 360 to the found angle.
|
43 |
+
angle += 360
|
44 |
+
|
45 |
+
# Return the calculated angle.
|
46 |
+
return angle
|
47 |
+
|
48 |
+
def classifyPose(landmarks, output_image, display=False):
|
49 |
+
'''
|
50 |
+
This function classifies yoga poses depending upon the angles of various body joints.
|
51 |
+
Args:
|
52 |
+
landmarks: A list of detected landmarks of the person whose pose needs to be classified.
|
53 |
+
output_image: A image of the person with the detected pose landmarks drawn.
|
54 |
+
display: A boolean value that is if set to true the function displays the resultant image with the pose label
|
55 |
+
written on it and returns nothing.
|
56 |
+
Returns:
|
57 |
+
output_image: The image with the detected pose landmarks drawn and pose label written.
|
58 |
+
label: The classified pose label of the person in the output_image.
|
59 |
+
|
60 |
+
'''
|
61 |
+
|
62 |
+
# Initialize the label of the pose. It is not known at this stage.
|
63 |
+
label = 'Unknown Pose'
|
64 |
+
|
65 |
+
# Specify the color (Red) with which the label will be written on the image.
|
66 |
+
color = (0, 0, 255)
|
67 |
+
|
68 |
+
# Calculate the required angles.
|
69 |
+
#----------------------------------------------------------------------------------------------------------------
|
70 |
+
|
71 |
+
# Get the angle between the left shoulder, elbow and wrist points.
|
72 |
+
left_elbow_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value],
|
73 |
+
landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value],
|
74 |
+
landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value])
|
75 |
+
|
76 |
+
# Get the angle between the right shoulder, elbow and wrist points.
|
77 |
+
right_elbow_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value],
|
78 |
+
landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value],
|
79 |
+
landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value])
|
80 |
+
|
81 |
+
# Get the angle between the left elbow, shoulder and hip points.
|
82 |
+
left_shoulder_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value],
|
83 |
+
landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value],
|
84 |
+
landmarks[mp_pose.PoseLandmark.LEFT_HIP.value])
|
85 |
+
|
86 |
+
# Get the angle between the right hip, shoulder and elbow points.
|
87 |
+
right_shoulder_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value],
|
88 |
+
landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value],
|
89 |
+
landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value])
|
90 |
+
|
91 |
+
# Get the angle between the left hip, knee and ankle points.
|
92 |
+
left_knee_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.LEFT_HIP.value],
|
93 |
+
landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value],
|
94 |
+
landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value])
|
95 |
+
|
96 |
+
# Get the angle between the right hip, knee and ankle points
|
97 |
+
right_knee_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value],
|
98 |
+
landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value],
|
99 |
+
landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value])
|
100 |
+
|
101 |
+
#----------------------------------------------------------------------------------------------------------------
|
102 |
+
# Check for Five-Pointed Star Pose
|
103 |
+
if abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value][1] - landmarks[mp_pose.PoseLandmark.LEFT_HIP.value][1]) < 100 and \
|
104 |
+
abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value][1] - landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value][1]) < 100 and \
|
105 |
+
abs(landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value][0] - landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value][0]) > 200 and \
|
106 |
+
abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value][0] - landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value][0]) > 200:
|
107 |
+
label = "Five-Pointed Star Pose"
|
108 |
+
|
109 |
+
# Check if it is the warrior II pose or the T pose.
|
110 |
+
# As for both of them, both arms should be straight and shoulders should be at the specific angle.
|
111 |
+
#----------------------------------------------------------------------------------------------------------------
|
112 |
+
|
113 |
+
# Check if the both arms are straight.
|
114 |
+
if left_elbow_angle > 165 and left_elbow_angle < 195 and right_elbow_angle > 165 and right_elbow_angle < 195:
|
115 |
+
|
116 |
+
# Check if shoulders are at the required angle.
|
117 |
+
if left_shoulder_angle > 80 and left_shoulder_angle < 110 and right_shoulder_angle > 80 and right_shoulder_angle < 110:
|
118 |
+
|
119 |
+
# Check if it is the warrior II pose.
|
120 |
+
#----------------------------------------------------------------------------------------------------------------
|
121 |
+
|
122 |
+
# Check if one leg is straight.
|
123 |
+
if left_knee_angle > 165 and left_knee_angle < 195 or right_knee_angle > 165 and right_knee_angle < 195:
|
124 |
+
|
125 |
+
# Check if the other leg is bended at the required angle.
|
126 |
+
if left_knee_angle > 90 and left_knee_angle < 120 or right_knee_angle > 90 and right_knee_angle < 120:
|
127 |
+
|
128 |
+
# Specify the label of the pose that is Warrior II pose.
|
129 |
+
label = 'Warrior II Pose'
|
130 |
+
|
131 |
+
#----------------------------------------------------------------------------------------------------------------
|
132 |
+
|
133 |
+
# Check if it is the T pose.
|
134 |
+
#----------------------------------------------------------------------------------------------------------------
|
135 |
+
|
136 |
+
# Check if both legs are straight
|
137 |
+
if left_knee_angle > 160 and left_knee_angle < 195 and right_knee_angle > 160 and right_knee_angle < 195:
|
138 |
+
|
139 |
+
# Specify the label of the pose that is tree pose.
|
140 |
+
label = 'T Pose'
|
141 |
+
|
142 |
+
#----------------------------------------------------------------------------------------------------------------
|
143 |
+
|
144 |
+
# Check if it is the tree pose.
|
145 |
+
#----------------------------------------------------------------------------------------------------------------
|
146 |
+
|
147 |
+
# Check if one leg is straight
|
148 |
+
if left_knee_angle > 165 and left_knee_angle < 195 or right_knee_angle > 165 and right_knee_angle < 195:
|
149 |
+
|
150 |
+
# Check if the other leg is bended at the required angle.
|
151 |
+
if left_knee_angle > 315 and left_knee_angle < 335 or right_knee_angle > 25 and right_knee_angle < 45:
|
152 |
+
|
153 |
+
# Specify the label of the pose that is tree pose.
|
154 |
+
label = 'Tree Pose'
|
155 |
+
|
156 |
+
# Check for Upward Salute Pose
|
157 |
+
if abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value][0] - landmarks[mp_pose.PoseLandmark.LEFT_HIP.value][0]) < 100 and \
|
158 |
+
abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value][0] - landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value][0]) < 100 and \
|
159 |
+
landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value][1] < landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value][1] and \
|
160 |
+
landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value][1] < landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value][1] and \
|
161 |
+
abs(landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value][1] - landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value][1]) < 50:
|
162 |
+
label = "Upward Salute Pose"
|
163 |
+
|
164 |
+
# Check for Hands Under Feet Pose
|
165 |
+
if landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value][1] > landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value][1] and \
|
166 |
+
landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value][1] > landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value][1] and \
|
167 |
+
abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value][0] - landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value][0]) < 50 and \
|
168 |
+
abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value][0] - landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value][0]) < 50:
|
169 |
+
label = "Hands Under Feet Pose"
|
170 |
+
|
171 |
+
|
172 |
+
#----------------------------------------------------------------------------------------------------------------
|
173 |
+
|
174 |
+
# Check if the pose is classified successfully
|
175 |
+
if label != 'Unknown Pose':
|
176 |
+
|
177 |
+
# Update the color (to green) with which the label will be written on the image.
|
178 |
+
color = (0, 255, 0)
|
179 |
+
|
180 |
+
# Write the label on the output image.
|
181 |
+
cv2.putText(output_image, label, (10, 30),cv2.FONT_HERSHEY_PLAIN, 2, color, 2)
|
182 |
+
|
183 |
+
# Check if the resultant image is specified to be displayed.
|
184 |
+
if display:
|
185 |
+
|
186 |
+
# Display the resultant image.
|
187 |
+
plt.figure(figsize=[10,10])
|
188 |
+
plt.imshow(output_image[:,:,::-1]);plt.title("Output Image");plt.axis('off');
|
189 |
+
|
190 |
+
else:
|
191 |
+
|
192 |
+
# Return the output image and the classified label.
|
193 |
+
return output_image, label
|
194 |
+
|
195 |
+
|
196 |
def run(
|
197 |
image: np.ndarray,
|
198 |
model_complexity: int,
|