Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,136 +8,97 @@ mp_pose = mp.solutions.pose
|
|
| 8 |
pose = mp_pose.Pose(static_image_mode=False, min_detection_confidence=0.5, model_complexity=1)
|
| 9 |
mp_drawing = mp.solutions.drawing_utils
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# Define a function to classify yoga poses
|
| 12 |
-
def
|
| 13 |
-
'''
|
| 14 |
-
This function classifies yoga poses depending upon the angles of various body joints.
|
| 15 |
-
Args:
|
| 16 |
-
landmarks: A list of detected landmarks of the person whose pose needs to be classified.
|
| 17 |
-
output_image: A image of the person with the detected pose landmarks drawn.
|
| 18 |
-
display: A boolean value that is if set to true the function displays the resultant image with the pose label
|
| 19 |
-
written on it and returns nothing.
|
| 20 |
-
Returns:
|
| 21 |
-
output_image: The image with the detected pose landmarks drawn and pose label written.
|
| 22 |
-
label: The classified pose label of the person in the output_image.
|
| 23 |
-
|
| 24 |
-
'''
|
| 25 |
-
|
| 26 |
-
# Initialize the label of the pose. It is not known at this stage.
|
| 27 |
label = 'Unknown Pose'
|
| 28 |
-
|
| 29 |
-
# Specify the color (Red) with which the label will be written on the image.
|
| 30 |
color = (0, 0, 255)
|
| 31 |
|
| 32 |
-
# Calculate the required angles
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
# Get the angle between the right hip, shoulder and elbow points.
|
| 51 |
-
right_shoulder_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value],
|
| 52 |
-
landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value],
|
| 53 |
-
landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value])
|
| 54 |
-
|
| 55 |
-
# Get the angle between the left hip, knee and ankle points.
|
| 56 |
-
left_knee_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.LEFT_HIP.value],
|
| 57 |
-
landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value],
|
| 58 |
-
landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value])
|
| 59 |
-
|
| 60 |
-
# Get the angle between the right hip, knee and ankle points
|
| 61 |
-
right_knee_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value],
|
| 62 |
-
landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value],
|
| 63 |
-
landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value])
|
| 64 |
-
|
| 65 |
-
#----------------------------------------------------------------------------------------------------------------
|
| 66 |
# Check for Five-Pointed Star Pose
|
| 67 |
-
if abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value]
|
| 68 |
-
abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value]
|
| 69 |
-
abs(landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value]
|
| 70 |
-
abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value]
|
| 71 |
-
label = "Five-Pointed Star Pose"
|
| 72 |
-
|
| 73 |
-
# Check if it is the warrior II pose or the T pose.
|
| 74 |
-
# As for both of them, both arms should be straight and shoulders should be at the specific angle.
|
| 75 |
-
#----------------------------------------------------------------------------------------------------------------
|
| 76 |
-
|
| 77 |
-
# Check if the both arms are straight.
|
| 78 |
-
if left_elbow_angle > 165 and left_elbow_angle < 195 and right_elbow_angle > 165 and right_elbow_angle < 195:
|
| 79 |
-
|
| 80 |
-
# Check if shoulders are at the required angle.
|
| 81 |
-
if left_shoulder_angle > 80 and left_shoulder_angle < 110 and right_shoulder_angle > 80 and right_shoulder_angle < 110:
|
| 82 |
-
|
| 83 |
-
# Check if it is the warrior II pose.
|
| 84 |
-
#----------------------------------------------------------------------------------------------------------------
|
| 85 |
-
|
| 86 |
-
# Check if one leg is straight.
|
| 87 |
-
if left_knee_angle > 165 and left_knee_angle < 195 or right_knee_angle > 165 and right_knee_angle < 195:
|
| 88 |
-
|
| 89 |
-
# Check if the other leg is bended at the required angle.
|
| 90 |
-
if left_knee_angle > 90 and left_knee_angle < 120 or right_knee_angle > 90 and right_knee_angle < 120:
|
| 91 |
-
|
| 92 |
-
# Specify the label of the pose that is Warrior II pose.
|
| 93 |
-
label = 'Warrior II Pose'
|
| 94 |
-
|
| 95 |
-
#----------------------------------------------------------------------------------------------------------------
|
| 96 |
-
|
| 97 |
-
# Check if it is the T pose.
|
| 98 |
-
#----------------------------------------------------------------------------------------------------------------
|
| 99 |
-
|
| 100 |
-
# Check if both legs are straight
|
| 101 |
-
if left_knee_angle > 160 and left_knee_angle < 195 and right_knee_angle > 160 and right_knee_angle < 195:
|
| 102 |
-
|
| 103 |
-
# Specify the label of the pose that is tree pose.
|
| 104 |
-
label = 'T Pose'
|
| 105 |
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
# Check for Upward Salute Pose
|
| 121 |
-
if abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value]
|
| 122 |
-
abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value]
|
| 123 |
-
landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value]
|
| 124 |
-
landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value]
|
| 125 |
-
abs(landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value]
|
| 126 |
label = "Upward Salute Pose"
|
| 127 |
|
| 128 |
-
|
| 129 |
-
if landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value]
|
| 130 |
-
landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value]
|
| 131 |
-
abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value]
|
| 132 |
-
abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value]
|
| 133 |
-
label = "Hands Under Feet Pose"
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
#----------------------------------------------------------------------------------------------------------------
|
| 137 |
-
|
| 138 |
-
# Check if the pose is classified successfully
|
| 139 |
-
if label != 'Unknown Pose':
|
| 140 |
-
|
| 141 |
|
| 142 |
def detect_and_classify_pose(input_image):
|
| 143 |
frame = cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB)
|
|
@@ -145,7 +106,7 @@ def detect_and_classify_pose(input_image):
|
|
| 145 |
pose_classification = "No pose detected"
|
| 146 |
if results.pose_landmarks:
|
| 147 |
mp_drawing.draw_landmarks(frame, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
|
| 148 |
-
pose_classification = classify_pose(results.pose_landmarks.landmark)
|
| 149 |
return frame, pose_classification
|
| 150 |
|
| 151 |
iface = gr.Interface(
|
|
|
|
| 8 |
pose = mp_pose.Pose(static_image_mode=False, min_detection_confidence=0.5, model_complexity=1)
|
| 9 |
mp_drawing = mp.solutions.drawing_utils
|
| 10 |
|
| 11 |
+
# Function to calculate the angle between three points
|
| 12 |
+
def calculate_angle(a, b, c):
|
| 13 |
+
a = np.array(a) # First point
|
| 14 |
+
b = np.array(b) # Mid point
|
| 15 |
+
c = np.array(c) # End point
|
| 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 |
+
left_elbow_angle = calculate_angle(
|
| 32 |
+
[landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].x, landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y],
|
| 33 |
+
[landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].x, landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].y],
|
| 34 |
+
[landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x, landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y])
|
| 35 |
+
|
| 36 |
+
right_elbow_angle = calculate_angle(
|
| 37 |
+
[landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].y],
|
| 38 |
+
[landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value].y],
|
| 39 |
+
[landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y])
|
| 40 |
+
|
| 41 |
+
left_shoulder_angle = calculate_angle(
|
| 42 |
+
[landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].x, landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].y],
|
| 43 |
+
[landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].x, landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y],
|
| 44 |
+
[landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].x, landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].y])
|
| 45 |
+
|
| 46 |
+
right_shoulder_angle = calculate_angle(
|
| 47 |
+
[landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].y],
|
| 48 |
+
[landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].y],
|
| 49 |
+
[landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value].y])
|
| 50 |
+
|
| 51 |
+
left_knee_angle = calculate_angle(
|
| 52 |
+
[landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].x, landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].y],
|
| 53 |
+
[landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value].x, landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value].y],
|
| 54 |
+
[landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].x, landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].y])
|
| 55 |
+
|
| 56 |
+
right_knee_angle = calculate_angle(
|
| 57 |
+
[landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].y],
|
| 58 |
+
[landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value].y],
|
| 59 |
+
[landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].y])
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
# Check for Five-Pointed Star Pose
|
| 62 |
+
if abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y - landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].y) < 0.1 and \
|
| 63 |
+
abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y - landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].y) < 0.1 and \
|
| 64 |
+
abs(landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].x) > 0.2 and \
|
| 65 |
+
abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x) > 0.2:
|
| 66 |
+
label = "Five-Pointed Star Pose"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
# Check for Warrior II pose
|
| 69 |
+
if 165 < left_elbow_angle < 195 and 165 < right_elbow_angle < 195 and \
|
| 70 |
+
80 < left_shoulder_angle < 110 and 80 < right_shoulder_angle < 110:
|
| 71 |
+
if (165 < left_knee_angle < 195 or 165 < right_knee_angle < 195) and \
|
| 72 |
+
(90 < left_knee_angle < 120 or 90 < right_knee_angle < 120):
|
| 73 |
+
label = 'Warrior II Pose'
|
| 74 |
+
|
| 75 |
+
# Check for T pose
|
| 76 |
+
if 165 < left_elbow_angle < 195 and 165 < right_elbow_angle < 195 and \
|
| 77 |
+
80 < left_shoulder_angle < 110 and 80 < right_shoulder_angle < 110 and \
|
| 78 |
+
160 < left_knee_angle < 195 and 160 < right_knee_angle < 195:
|
| 79 |
+
label = 'T Pose'
|
| 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].x - landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].x) < 0.1 and \
|
| 88 |
+
abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].x) < 0.1 and \
|
| 89 |
+
landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y < landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y and \
|
| 90 |
+
landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y < landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].y and \
|
| 91 |
+
abs(landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y - landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].y) < 0.05:
|
| 92 |
label = "Upward Salute Pose"
|
| 93 |
|
| 94 |
+
# Check for Hands Under Feet Pose
|
| 95 |
+
if landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y > landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value].y and \
|
| 96 |
+
landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y > landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value].y and \
|
| 97 |
+
abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].x) < 0.05 and \
|
| 98 |
+
abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].x) < 0.05:
|
| 99 |
+
label = "Hands Under Feet Pose"
|
| 100 |
+
|
| 101 |
+
return label
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
def detect_and_classify_pose(input_image):
|
| 104 |
frame = cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB)
|
|
|
|
| 106 |
pose_classification = "No pose detected"
|
| 107 |
if results.pose_landmarks:
|
| 108 |
mp_drawing.draw_landmarks(frame, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
|
| 109 |
+
pose_classification = classify_pose(results.pose_landmarks.landmark, frame)
|
| 110 |
return frame, pose_classification
|
| 111 |
|
| 112 |
iface = gr.Interface(
|