Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files
pose_estimation/__init__.py
ADDED
|
File without changes
|
pose_estimation/angle_calculation.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import math
|
| 3 |
+
|
| 4 |
+
def calculate_angle(a, b, c):
|
| 5 |
+
"""Calculates the angle between three points (e.g., elbow, shoulder, hip).
|
| 6 |
+
Args:
|
| 7 |
+
a: First point (list or tuple of [x, y]).
|
| 8 |
+
b: Second point (vertex) (list or tuple of [x, y]).
|
| 9 |
+
c: Third point (list or tuple of [x, y]).
|
| 10 |
+
Returns:
|
| 11 |
+
The angle in degrees.
|
| 12 |
+
"""
|
| 13 |
+
a = np.array(a) # First
|
| 14 |
+
b = np.array(b) # Mid
|
| 15 |
+
c = np.array(c) # End
|
| 16 |
+
|
| 17 |
+
radians = np.arctan2(c[1] - b[1], c[0] - b[0]) - \
|
| 18 |
+
np.arctan2(a[1] - b[1], a[0] - b[0])
|
| 19 |
+
angle = np.abs(radians * 180.0 / np.pi)
|
| 20 |
+
|
| 21 |
+
if angle > 180.0:
|
| 22 |
+
angle = 360 - angle
|
| 23 |
+
|
| 24 |
+
return angle
|
| 25 |
+
|
| 26 |
+
def calculate_angle_3d(a, b, c):
|
| 27 |
+
"""Calculates the angle between three 3D points.
|
| 28 |
+
Args:
|
| 29 |
+
a: First landmark (MediaPipe Landmark object or dict with x, y, z).
|
| 30 |
+
b: Second landmark (vertex) (MediaPipe Landmark object or dict with x, y, z).
|
| 31 |
+
c: Third landmark (MediaPipe Landmark object or dict with x, y, z).
|
| 32 |
+
Returns:
|
| 33 |
+
The angle in degrees.
|
| 34 |
+
"""
|
| 35 |
+
# Convert to numpy arrays
|
| 36 |
+
vec_a = np.array([a.x, a.y, a.z]) if hasattr(a, 'x') else np.array(a)
|
| 37 |
+
vec_b = np.array([b.x, b.y, b.z]) if hasattr(b, 'x') else np.array(b)
|
| 38 |
+
vec_c = np.array([c.x, c.y, c.z]) if hasattr(c, 'x') else np.array(c)
|
| 39 |
+
|
| 40 |
+
# Calculate vectors from mid_point to other points
|
| 41 |
+
vec_ba = vec_a - vec_b
|
| 42 |
+
vec_bc = vec_c - vec_b
|
| 43 |
+
|
| 44 |
+
# Calculate dot product
|
| 45 |
+
dot_product = np.dot(vec_ba, vec_bc)
|
| 46 |
+
|
| 47 |
+
# Calculate magnitudes
|
| 48 |
+
magnitude_ba = np.linalg.norm(vec_ba)
|
| 49 |
+
magnitude_bc = np.linalg.norm(vec_bc)
|
| 50 |
+
|
| 51 |
+
# Calculate cosine of the angle
|
| 52 |
+
# Add a small epsilon to prevent division by zero if magnitudes are zero
|
| 53 |
+
epsilon = 1e-7
|
| 54 |
+
cosine_angle = dot_product / (magnitude_ba * magnitude_bc + epsilon)
|
| 55 |
+
|
| 56 |
+
# Ensure cosine_angle is within the valid range for arccos [-1, 1]
|
| 57 |
+
cosine_angle = np.clip(cosine_angle, -1.0, 1.0)
|
| 58 |
+
|
| 59 |
+
# Calculate angle in radians
|
| 60 |
+
angle_rad = np.arccos(cosine_angle)
|
| 61 |
+
|
| 62 |
+
# Convert angle to degrees
|
| 63 |
+
angle_deg = np.degrees(angle_rad)
|
| 64 |
+
|
| 65 |
+
return angle_deg
|