Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,9 +10,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)
|
|
@@ -29,34 +29,34 @@ def classify_pose(landmarks, output_image, display=False):
|
|
| 29 |
|
| 30 |
# Calculate the required angles
|
| 31 |
left_elbow_angle = calculate_angle(
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
right_elbow_angle = calculate_angle(
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
|
| 41 |
left_shoulder_angle = calculate_angle(
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
right_shoulder_angle = calculate_angle(
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
left_knee_angle = calculate_angle(
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
|
| 56 |
right_knee_angle = calculate_angle(
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 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 \
|
|
@@ -98,16 +98,15 @@ def classify_pose(landmarks, output_image, display=False):
|
|
| 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 |
-
|
| 105 |
-
results = pose.process(frame)
|
| 106 |
pose_classification = "No pose detected"
|
| 107 |
if results.pose_landmarks:
|
| 108 |
-
mp_drawing.draw_landmarks(
|
| 109 |
-
pose_classification = classify_pose(results.pose_landmarks.landmark,
|
| 110 |
-
return
|
| 111 |
|
| 112 |
iface = gr.Interface(
|
| 113 |
fn=detect_and_classify_pose,
|
|
|
|
| 10 |
|
| 11 |
# Function to calculate the angle between three points
|
| 12 |
def calculate_angle(a, b, c):
|
| 13 |
+
a = np.array([a.x, a.y]) # First point
|
| 14 |
+
b = np.array([b.x, b.y]) # Mid point
|
| 15 |
+
c = np.array([c.x, c.y]) # 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)
|
|
|
|
| 29 |
|
| 30 |
# Calculate the required angles
|
| 31 |
left_elbow_angle = calculate_angle(
|
| 32 |
+
landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value],
|
| 33 |
+
landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value],
|
| 34 |
+
landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value])
|
| 35 |
|
| 36 |
right_elbow_angle = calculate_angle(
|
| 37 |
+
landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value],
|
| 38 |
+
landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value],
|
| 39 |
+
landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value])
|
| 40 |
|
| 41 |
left_shoulder_angle = calculate_angle(
|
| 42 |
+
landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value],
|
| 43 |
+
landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value],
|
| 44 |
+
landmarks[mp_pose.PoseLandmark.LEFT_HIP.value])
|
| 45 |
|
| 46 |
right_shoulder_angle = calculate_angle(
|
| 47 |
+
landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value],
|
| 48 |
+
landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value],
|
| 49 |
+
landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value])
|
| 50 |
|
| 51 |
left_knee_angle = calculate_angle(
|
| 52 |
+
landmarks[mp_pose.PoseLandmark.LEFT_HIP.value],
|
| 53 |
+
landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value],
|
| 54 |
+
landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value])
|
| 55 |
|
| 56 |
right_knee_angle = calculate_angle(
|
| 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].y - landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].y) < 0.1 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 output_image, label
|
| 102 |
|
| 103 |
def detect_and_classify_pose(input_image):
|
| 104 |
+
results = pose.process(input_image)
|
|
|
|
| 105 |
pose_classification = "No pose detected"
|
| 106 |
if results.pose_landmarks:
|
| 107 |
+
mp_drawing.draw_landmarks(input_image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
|
| 108 |
+
input_image, pose_classification = classify_pose(results.pose_landmarks.landmark, input_image)
|
| 109 |
+
return input_image, pose_classification
|
| 110 |
|
| 111 |
iface = gr.Interface(
|
| 112 |
fn=detect_and_classify_pose,
|