Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,136 +1,154 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
radians = np.arctan2(c[1] - b[1], c[0] - b[0]) - np.arctan2(a[1] - b[1], a[0] - b[0])
|
| 18 |
-
angle = np.abs(radians * 180.0 / np.pi)
|
| 19 |
-
|
| 20 |
-
if angle > 180.0:
|
| 21 |
-
angle = 360 - angle
|
| 22 |
-
|
| 23 |
-
return angle
|
| 24 |
-
|
| 25 |
-
# Define a function to classify yoga poses
|
| 26 |
-
def classify_pose(landmarks, output_image, display=False):
|
| 27 |
label = 'Unknown Pose'
|
|
|
|
|
|
|
| 28 |
color = (0, 0, 255)
|
| 29 |
|
| 30 |
-
# Calculate the required angles
|
| 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 |
-
landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value],
|
| 58 |
-
landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value],
|
| 59 |
-
landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value])
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
# Check for Five-Pointed Star Pose
|
| 62 |
-
if abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value]
|
| 63 |
-
abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value]
|
| 64 |
-
abs(landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value]
|
| 65 |
-
abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value]
|
| 66 |
-
label = "Five-Pointed Star Pose"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
# Check for Tree Pose
|
| 82 |
-
if (165 < left_knee_angle < 195 or 165 < right_knee_angle < 195) and \
|
| 83 |
-
(315 < left_knee_angle < 335 or 25 < right_knee_angle < 45):
|
| 84 |
-
label = 'Tree Pose'
|
| 85 |
|
| 86 |
# Check for Upward Salute Pose
|
| 87 |
-
if abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value]
|
| 88 |
-
abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value]
|
| 89 |
-
landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value]
|
| 90 |
-
landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value]
|
| 91 |
-
abs(landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value]
|
| 92 |
label = "Upward Salute Pose"
|
| 93 |
|
| 94 |
-
|
| 95 |
-
if landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value]
|
| 96 |
-
landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value]
|
| 97 |
-
abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value]
|
| 98 |
-
abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value]
|
| 99 |
-
label = "Hands Under Feet Pose"
|
| 100 |
-
|
| 101 |
# Check for Plank Pose
|
|
|
|
| 102 |
# The body should be in a straight line from head to heels,
|
| 103 |
# so the shoulder and hip angles should be close to 180 degrees
|
| 104 |
if left_shoulder_angle > 160 and left_shoulder_angle < 200 and \
|
| 105 |
right_shoulder_angle > 160 and right_shoulder_angle < 200 and \
|
| 106 |
left_knee_angle > 160 and left_knee_angle < 200 and \
|
| 107 |
right_knee_angle > 160 and right_knee_angle < 200:
|
| 108 |
-
label = "Plank Pose"
|
| 109 |
-
|
| 110 |
-
|
|
|
|
| 111 |
if label != 'Unknown Pose':
|
|
|
|
|
|
|
| 112 |
color = (0, 255, 0)
|
| 113 |
-
|
| 114 |
-
# Write the label on the output image
|
| 115 |
-
cv2.putText(output_image, label, (10, 30),
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
iface = gr.Interface(
|
| 129 |
-
fn=detect_and_classify_pose,
|
| 130 |
-
inputs="image",
|
| 131 |
-
outputs=["image", "text"],
|
| 132 |
-
title="Live Yoga Pose Detection and Classification",
|
| 133 |
-
description="This app detects and classifies yoga poses from the live camera feed using MediaPipe.",
|
| 134 |
-
)
|
| 135 |
-
|
| 136 |
-
iface.launch()
|
|
|
|
| 1 |
+
def classifyPose(landmarks, output_image, display=False):
|
| 2 |
+
'''
|
| 3 |
+
This function classifies yoga poses depending upon the angles of various body joints.
|
| 4 |
+
Args:
|
| 5 |
+
landmarks: A list of detected landmarks of the person whose pose needs to be classified.
|
| 6 |
+
output_image: A image of the person with the detected pose landmarks drawn.
|
| 7 |
+
display: A boolean value that is if set to true the function displays the resultant image with the pose label
|
| 8 |
+
written on it and returns nothing.
|
| 9 |
+
Returns:
|
| 10 |
+
output_image: The image with the detected pose landmarks drawn and pose label written.
|
| 11 |
+
label: The classified pose label of the person in the output_image.
|
| 12 |
+
|
| 13 |
+
'''
|
| 14 |
+
|
| 15 |
+
# Initialize the label of the pose. It is not known at this stage.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
label = 'Unknown Pose'
|
| 17 |
+
|
| 18 |
+
# Specify the color (Red) with which the label will be written on the image.
|
| 19 |
color = (0, 0, 255)
|
| 20 |
|
| 21 |
+
# Calculate the required angles.
|
| 22 |
+
#----------------------------------------------------------------------------------------------------------------
|
| 23 |
+
|
| 24 |
+
# Get the angle between the left shoulder, elbow and wrist points.
|
| 25 |
+
left_elbow_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value],
|
| 26 |
+
landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value],
|
| 27 |
+
landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value])
|
| 28 |
+
|
| 29 |
+
# Get the angle between the right shoulder, elbow and wrist points.
|
| 30 |
+
right_elbow_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value],
|
| 31 |
+
landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value],
|
| 32 |
+
landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value])
|
| 33 |
+
|
| 34 |
+
# Get the angle between the left elbow, shoulder and hip points.
|
| 35 |
+
left_shoulder_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value],
|
| 36 |
+
landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value],
|
| 37 |
+
landmarks[mp_pose.PoseLandmark.LEFT_HIP.value])
|
| 38 |
+
|
| 39 |
+
# Get the angle between the right hip, shoulder and elbow points.
|
| 40 |
+
right_shoulder_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value],
|
| 41 |
+
landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value],
|
| 42 |
+
landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value])
|
| 43 |
+
|
| 44 |
+
# Get the angle between the left hip, knee and ankle points.
|
| 45 |
+
left_knee_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.LEFT_HIP.value],
|
| 46 |
+
landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value],
|
| 47 |
+
landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value])
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
# Get the angle between the right hip, knee and ankle points
|
| 50 |
+
right_knee_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value],
|
| 51 |
+
landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value],
|
| 52 |
+
landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value])
|
| 53 |
+
|
| 54 |
+
#----------------------------------------------------------------------------------------------------------------
|
| 55 |
# Check for Five-Pointed Star Pose
|
| 56 |
+
if abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value][1] - landmarks[mp_pose.PoseLandmark.LEFT_HIP.value][1]) < 100 and \
|
| 57 |
+
abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value][1] - landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value][1]) < 100 and \
|
| 58 |
+
abs(landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value][0] - landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value][0]) > 200 and \
|
| 59 |
+
abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value][0] - landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value][0]) > 200:
|
| 60 |
+
label = "Five-Pointed Star Pose"
|
| 61 |
+
|
| 62 |
+
# Check if it is the warrior II pose or the T pose.
|
| 63 |
+
# As for both of them, both arms should be straight and shoulders should be at the specific angle.
|
| 64 |
+
#----------------------------------------------------------------------------------------------------------------
|
| 65 |
+
|
| 66 |
+
# Check if the both arms are straight.
|
| 67 |
+
if left_elbow_angle > 165 and left_elbow_angle < 195 and right_elbow_angle > 165 and right_elbow_angle < 195:
|
| 68 |
+
|
| 69 |
+
# Check if shoulders are at the required angle.
|
| 70 |
+
if left_shoulder_angle > 80 and left_shoulder_angle < 110 and right_shoulder_angle > 80 and right_shoulder_angle < 110:
|
| 71 |
+
|
| 72 |
+
# Check if it is the warrior II pose.
|
| 73 |
+
#----------------------------------------------------------------------------------------------------------------
|
| 74 |
+
|
| 75 |
+
# Check if one leg is straight.
|
| 76 |
+
if left_knee_angle > 165 and left_knee_angle < 195 or right_knee_angle > 165 and right_knee_angle < 195:
|
| 77 |
+
|
| 78 |
+
# Check if the other leg is bended at the required angle.
|
| 79 |
+
if left_knee_angle > 90 and left_knee_angle < 120 or right_knee_angle > 90 and right_knee_angle < 120:
|
| 80 |
+
|
| 81 |
+
# Specify the label of the pose that is Warrior II pose.
|
| 82 |
+
label = 'Warrior II Pose'
|
| 83 |
+
|
| 84 |
+
#----------------------------------------------------------------------------------------------------------------
|
| 85 |
+
|
| 86 |
+
# Check if it is the T pose.
|
| 87 |
+
#----------------------------------------------------------------------------------------------------------------
|
| 88 |
+
|
| 89 |
+
# Check if both legs are straight
|
| 90 |
+
if left_knee_angle > 160 and left_knee_angle < 195 and right_knee_angle > 160 and right_knee_angle < 195:
|
| 91 |
+
|
| 92 |
+
# Specify the label of the pose that is tree pose.
|
| 93 |
+
label = 'T Pose'
|
| 94 |
|
| 95 |
+
#----------------------------------------------------------------------------------------------------------------
|
| 96 |
+
|
| 97 |
+
# Check if it is the tree pose.
|
| 98 |
+
#----------------------------------------------------------------------------------------------------------------
|
| 99 |
+
|
| 100 |
+
# Check if one leg is straight
|
| 101 |
+
if left_knee_angle > 165 and left_knee_angle < 195 or right_knee_angle > 165 and right_knee_angle < 195:
|
| 102 |
+
|
| 103 |
+
# Check if the other leg is bended at the required angle.
|
| 104 |
+
if left_knee_angle > 315 and left_knee_angle < 335 or right_knee_angle > 25 and right_knee_angle < 45:
|
| 105 |
+
|
| 106 |
+
# Specify the label of the pose that is tree pose.
|
| 107 |
+
label = 'Tree Pose'
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
# Check for Upward Salute Pose
|
| 110 |
+
if abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value][0] - landmarks[mp_pose.PoseLandmark.LEFT_HIP.value][0]) < 100 and \
|
| 111 |
+
abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value][0] - landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value][0]) < 100 and \
|
| 112 |
+
landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value][1] < landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value][1] and \
|
| 113 |
+
landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value][1] < landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value][1] and \
|
| 114 |
+
abs(landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value][1] - landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value][1]) < 50:
|
| 115 |
label = "Upward Salute Pose"
|
| 116 |
|
| 117 |
+
# Check for Hands Under Feet Pose
|
| 118 |
+
if landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value][1] > landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value][1] and \
|
| 119 |
+
landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value][1] > landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value][1] and \
|
| 120 |
+
abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value][0] - landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value][0]) < 50 and \
|
| 121 |
+
abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value][0] - landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value][0]) < 50:
|
| 122 |
+
label = "Hands Under Feet Pose"
|
| 123 |
+
|
| 124 |
# Check for Plank Pose
|
| 125 |
+
#----------------------------------------------------------------------------------------------------------------
|
| 126 |
# The body should be in a straight line from head to heels,
|
| 127 |
# so the shoulder and hip angles should be close to 180 degrees
|
| 128 |
if left_shoulder_angle > 160 and left_shoulder_angle < 200 and \
|
| 129 |
right_shoulder_angle > 160 and right_shoulder_angle < 200 and \
|
| 130 |
left_knee_angle > 160 and left_knee_angle < 200 and \
|
| 131 |
right_knee_angle > 160 and right_knee_angle < 200:
|
| 132 |
+
label = "Plank Pose"
|
| 133 |
+
#----------------------------------------------------------------------------------------------------------------
|
| 134 |
+
|
| 135 |
+
# Check if the pose is classified successfully
|
| 136 |
if label != 'Unknown Pose':
|
| 137 |
+
|
| 138 |
+
# Update the color (to green) with which the label will be written on the image.
|
| 139 |
color = (0, 255, 0)
|
| 140 |
+
|
| 141 |
+
# Write the label on the output image.
|
| 142 |
+
cv2.putText(output_image, label, (10, 30),cv2.FONT_HERSHEY_PLAIN, 2, color, 2)
|
| 143 |
+
|
| 144 |
+
# Check if the resultant image is specified to be displayed.
|
| 145 |
+
if display:
|
| 146 |
+
|
| 147 |
+
# Display the resultant image.
|
| 148 |
+
plt.figure(figsize=[10,10])
|
| 149 |
+
plt.imshow(output_image[:,:,::-1]);plt.title("Output Image");plt.axis('off');
|
| 150 |
+
|
| 151 |
+
else:
|
| 152 |
+
|
| 153 |
+
# Return the output image and the classified label.
|
| 154 |
+
return output_image, label
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|