Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import numpy as np
|
|
| 5 |
|
| 6 |
# Initialize mediapipe pose class
|
| 7 |
mp_pose = mp.solutions.pose
|
| 8 |
-
pose =
|
| 9 |
mp_drawing = mp.solutions.drawing_utils
|
| 10 |
|
| 11 |
# Function to calculate the angle between three points
|
|
@@ -26,9 +26,6 @@ def calculate_angle(a, b, c):
|
|
| 26 |
def classify_pose(landmarks, output_image, display=False):
|
| 27 |
label = 'Unknown Pose'
|
| 28 |
|
| 29 |
-
|
| 30 |
-
color = (0, 0, 255)
|
| 31 |
-
|
| 32 |
# Calculate the required angles
|
| 33 |
left_elbow_angle = calculate_angle(
|
| 34 |
landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value],
|
|
@@ -60,51 +57,12 @@ def classify_pose(landmarks, output_image, display=False):
|
|
| 60 |
landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value],
|
| 61 |
landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value])
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
# Check for Five-Pointed Star Pose
|
| 70 |
if abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y - landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].y) < 0.1 and \
|
| 71 |
abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y - landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].y) < 0.1 and \
|
| 72 |
abs(landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].x) > 0.2 and \
|
| 73 |
abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x) > 0.2:
|
| 74 |
label = "Five-Pointed Star Pose"
|
| 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 |
# Check for Warrior II pose
|
| 110 |
if 165 < left_elbow_angle < 195 and 165 < right_elbow_angle < 195 and \
|
|
@@ -133,49 +91,4 @@ def classify_pose(landmarks, output_image, display=False):
|
|
| 133 |
label = "Upward Salute Pose"
|
| 134 |
|
| 135 |
# Check for Hands Under Feet Pose
|
| 136 |
-
if landmarks[mp_pose.
|
| 137 |
-
landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y > landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value].y and \
|
| 138 |
-
abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].x) < 0.05 and \
|
| 139 |
-
abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].x) < 0.05:
|
| 140 |
-
label = "Hands Under Feet Pose"
|
| 141 |
-
|
| 142 |
-
# Check for Plank Pose
|
| 143 |
-
|
| 144 |
-
# The body should be in a straight line from head to heels,
|
| 145 |
-
# so the shoulder and hip angles should be close to 180 degrees
|
| 146 |
-
if left_shoulder_angle > 160 and left_shoulder_angle < 200 and \
|
| 147 |
-
right_shoulder_angle > 160 and right_shoulder_angle < 200 and \
|
| 148 |
-
left_knee_angle > 160 and left_knee_angle < 200 and \
|
| 149 |
-
right_knee_angle > 160 and right_knee_angle < 200:
|
| 150 |
-
label = "Plank Pose"
|
| 151 |
-
|
| 152 |
-
# Update the color to green if pose is classified
|
| 153 |
-
|
| 154 |
-
if label != 'Unknown Pose':
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
color = (0, 255, 0)
|
| 158 |
-
|
| 159 |
-
# Write the label on the output image
|
| 160 |
-
cv2.putText(output_image, label, (10, 30), cv2.FONT_HERSHEY_PLAIN, 2, color, 2)
|
| 161 |
-
|
| 162 |
-
return output_image, label
|
| 163 |
-
|
| 164 |
-
def detect_and_classify_pose(input_image):
|
| 165 |
-
input_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB)
|
| 166 |
-
results = pose.process(input_image)
|
| 167 |
-
pose_classification = "No pose detected"
|
| 168 |
-
if results.pose_landmarks:
|
| 169 |
-
mp_drawing.draw_landmarks(input_image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
|
| 170 |
-
input_image, pose_classification = classify_pose(results.pose_landmarks.landmark, input_image)
|
| 171 |
-
return input_image, pose_classification
|
| 172 |
-
|
| 173 |
-
iface = gr.Interface(
|
| 174 |
-
fn=detect_and_classify_pose,
|
| 175 |
-
inputs="image",
|
| 176 |
-
outputs=["image", "text"],
|
| 177 |
-
title="Live Yoga Pose Detection and Classification",
|
| 178 |
-
description="This app detects and classifies yoga poses from the live camera feed using MediaPipe.",
|
| 179 |
-
)
|
| 180 |
-
|
| 181 |
-
iface.launch()
|
|
|
|
| 5 |
|
| 6 |
# Initialize mediapipe pose class
|
| 7 |
mp_pose = mp.solutions.pose
|
| 8 |
+
pose = None
|
| 9 |
mp_drawing = mp.solutions.drawing_utils
|
| 10 |
|
| 11 |
# Function to calculate the angle between three points
|
|
|
|
| 26 |
def classify_pose(landmarks, output_image, display=False):
|
| 27 |
label = 'Unknown Pose'
|
| 28 |
|
|
|
|
|
|
|
|
|
|
| 29 |
# Calculate the required angles
|
| 30 |
left_elbow_angle = calculate_angle(
|
| 31 |
landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value],
|
|
|
|
| 57 |
landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value],
|
| 58 |
landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value])
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
# Check for Five-Pointed Star Pose
|
| 61 |
if abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y - landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].y) < 0.1 and \
|
| 62 |
abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y - landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].y) < 0.1 and \
|
| 63 |
abs(landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].x) > 0.2 and \
|
| 64 |
abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x) > 0.2:
|
| 65 |
label = "Five-Pointed Star Pose"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
# Check for Warrior II pose
|
| 68 |
if 165 < left_elbow_angle < 195 and 165 < right_elbow_angle < 195 and \
|
|
|
|
| 91 |
label = "Upward Salute Pose"
|
| 92 |
|
| 93 |
# Check for Hands Under Feet Pose
|
| 94 |
+
if landmarks[mp_pose.Pose
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|