Spaces:
Runtime error
Runtime error
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1,178 +1,308 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
self.
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 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 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 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 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
return self.angles
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import time
|
| 3 |
+
|
| 4 |
+
class Utils:
|
| 5 |
+
def __init__(self, axes, restSpeed, minPoints, requiredFrames):
|
| 6 |
+
self.axes = axes
|
| 7 |
+
self.REST_SPEED_THRESHOLD = restSpeed # pixels/second
|
| 8 |
+
self.MIN_POINTS_FOR_REST = minPoints
|
| 9 |
+
self.REQUIRED_CONSECUTIVE_FRAMES = requiredFrames
|
| 10 |
+
|
| 11 |
+
self.rest_buffer_frame = 0
|
| 12 |
+
# persistent state used across consecutive calls to is_resting
|
| 13 |
+
self.prev_positions = {
|
| 14 |
+
"positions": {}, # dict e.g. {"left_wrist": np.array([...]), ...}
|
| 15 |
+
"timestamp": None,
|
| 16 |
+
"counters": { # consecutive low-speed frame counters per key
|
| 17 |
+
"left_wrist": 0,
|
| 18 |
+
"right_wrist": 0,
|
| 19 |
+
"left_shoulder": 0,
|
| 20 |
+
"right_shoulder": 0
|
| 21 |
+
},
|
| 22 |
+
"rest_start_time": None
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
def _landmark_to_pixel(self, landmark, img_shape):
|
| 26 |
+
"""Convert normalized landmark to pixel (x,y)."""
|
| 27 |
+
h, w = img_shape[0], img_shape[1]
|
| 28 |
+
return np.array([landmark.x * w, landmark.y * h], dtype=float)
|
| 29 |
+
|
| 30 |
+
# Replace previous is_resting with time-based version
|
| 31 |
+
def is_resting(self, res_hands, img_shape, rest_delay_seconds: float = 2.0, fps: float = None):
|
| 32 |
+
"""
|
| 33 |
+
Time-based rest detection.
|
| 34 |
+
Returns True if average landmark speed stays below REST_SPEED_THRESHOLD
|
| 35 |
+
for at least rest_delay_seconds.
|
| 36 |
+
"""
|
| 37 |
+
current_time = time.time()
|
| 38 |
+
points = {}
|
| 39 |
+
|
| 40 |
+
# Hands: get wrist landmark (index 0) if available
|
| 41 |
+
if getattr(res_hands, "multi_hand_landmarks", None) and getattr(res_hands, "multi_handedness", None):
|
| 42 |
+
for hand_landmarks, handedness in zip(res_hands.multi_hand_landmarks, res_hands.multi_handedness):
|
| 43 |
+
label = handedness.classification[0].label
|
| 44 |
+
try:
|
| 45 |
+
wrist = self._landmark_to_pixel(hand_landmarks.landmark[0], img_shape)
|
| 46 |
+
except Exception:
|
| 47 |
+
continue
|
| 48 |
+
|
| 49 |
+
if label == 'Left':
|
| 50 |
+
points['left_wrist'] = wrist
|
| 51 |
+
elif label == 'Right':
|
| 52 |
+
points['right_wrist'] = wrist
|
| 53 |
+
|
| 54 |
+
# Initialize previous timestamp if missing
|
| 55 |
+
if self.prev_positions['timestamp'] is None:
|
| 56 |
+
self.prev_positions['timestamp'] = current_time
|
| 57 |
+
self.prev_positions['positions'].update({k: v for k, v in points.items()})
|
| 58 |
+
self.prev_positions['rest_start_time'] = None
|
| 59 |
+
return False
|
| 60 |
+
|
| 61 |
+
# No landmarks detected -> not resting (reset)
|
| 62 |
+
if len(points) < 1:
|
| 63 |
+
self.prev_positions['timestamp'] = current_time
|
| 64 |
+
self.prev_positions['positions'].update({k: v for k, v in points.items()})
|
| 65 |
+
self.prev_positions['rest_start_time'] = None
|
| 66 |
+
# reset counters for keys that are missing
|
| 67 |
+
for k in self.prev_positions['counters']:
|
| 68 |
+
if k not in points:
|
| 69 |
+
self.prev_positions['counters'][k] = 0
|
| 70 |
+
return False
|
| 71 |
+
|
| 72 |
+
dt = current_time - self.prev_positions['timestamp']
|
| 73 |
+
if dt <= 0:
|
| 74 |
+
self.prev_positions['timestamp'] = current_time
|
| 75 |
+
self.prev_positions['positions'].update({k: v for k, v in points.items()})
|
| 76 |
+
self.prev_positions['rest_start_time'] = None
|
| 77 |
+
return False
|
| 78 |
+
|
| 79 |
+
speeds = []
|
| 80 |
+
speed_map = {}
|
| 81 |
+
|
| 82 |
+
# compute speeds only where previous positions exist
|
| 83 |
+
for key, cur_pos in points.items():
|
| 84 |
+
prev_pos = self.prev_positions['positions'].get(key)
|
| 85 |
+
if prev_pos is not None:
|
| 86 |
+
dist = np.linalg.norm(cur_pos - prev_pos)
|
| 87 |
+
speed = dist / dt
|
| 88 |
+
speeds.append(speed)
|
| 89 |
+
speed_map[key] = speed
|
| 90 |
+
|
| 91 |
+
# update timestamp and previous positions for next call
|
| 92 |
+
self.prev_positions['timestamp'] = current_time
|
| 93 |
+
self.prev_positions['positions'].update({k: v for k, v in points.items()})
|
| 94 |
+
|
| 95 |
+
# reset counters for disappeared landmarks
|
| 96 |
+
for k in list(self.prev_positions['counters'].keys()):
|
| 97 |
+
if k not in speed_map:
|
| 98 |
+
self.prev_positions['counters'][k] = 0
|
| 99 |
+
|
| 100 |
+
# Optionally scale threshold for very low FPS if fps provided
|
| 101 |
+
adjusted_threshold = self.REST_SPEED_THRESHOLD
|
| 102 |
+
if fps is not None and fps > 0:
|
| 103 |
+
adjusted_threshold = self.REST_SPEED_THRESHOLD * (30.0 / max(fps, 1.0))
|
| 104 |
+
|
| 105 |
+
# Using avg speed to detect on enough landmarks
|
| 106 |
+
if len(speeds) >= self.MIN_POINTS_FOR_REST:
|
| 107 |
+
avg_speed = float(np.mean(speeds))
|
| 108 |
+
if avg_speed < adjusted_threshold:
|
| 109 |
+
if self.prev_positions['rest_start_time'] is None:
|
| 110 |
+
self.prev_positions['rest_start_time'] = current_time
|
| 111 |
+
# Check duration
|
| 112 |
+
if (current_time - self.prev_positions['rest_start_time']) >= rest_delay_seconds:
|
| 113 |
+
return True
|
| 114 |
+
else:
|
| 115 |
+
# Reset rest tracking if movement detected
|
| 116 |
+
self.prev_positions['rest_start_time'] = None
|
| 117 |
+
for k in self.prev_positions['counters']:
|
| 118 |
+
self.prev_positions['counters'][k] = 0
|
| 119 |
+
return False
|
| 120 |
+
|
| 121 |
+
# Single landmark case: treat similarly
|
| 122 |
+
if len(speeds) == 1:
|
| 123 |
+
key = next(iter(speed_map))
|
| 124 |
+
speed = speed_map[key]
|
| 125 |
+
if speed < adjusted_threshold:
|
| 126 |
+
if self.prev_positions['rest_start_time'] is None:
|
| 127 |
+
self.prev_positions['rest_start_time'] = current_time
|
| 128 |
+
if (current_time - self.prev_positions['rest_start_time']) >= rest_delay_seconds:
|
| 129 |
+
return True
|
| 130 |
+
else:
|
| 131 |
+
self.prev_positions['rest_start_time'] = None
|
| 132 |
+
self.prev_positions['counters'][key] = 0
|
| 133 |
+
return False
|
| 134 |
+
|
| 135 |
+
# default
|
| 136 |
+
self.prev_positions['rest_start_time'] = None
|
| 137 |
+
return False
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
def calculate_angle1(self, vec1, vec2):
|
| 141 |
+
dot_product = np.dot(vec1, vec2)
|
| 142 |
+
norm_vec1 = np.linalg.norm(vec1)
|
| 143 |
+
norm_vec2 = np.linalg.norm(vec2)
|
| 144 |
+
self.cosine_angle = dot_product / (norm_vec1 * norm_vec2) if norm_vec1 and norm_vec2 else 0
|
| 145 |
+
return self.cosine_angle
|
| 146 |
+
|
| 147 |
+
def get_coordinates_safe(self, landmark, index):
|
| 148 |
+
try:
|
| 149 |
+
return np.array([landmark[index].x, landmark[index].y, landmark[index].z])
|
| 150 |
+
except IndexError:
|
| 151 |
+
return np.array([-1, -1, -1])
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
def angle_between_vectors(self, v1, v2):
|
| 155 |
+
dot_product = np.dot(v1, v2)
|
| 156 |
+
magnitude_v1 = np.linalg.norm(v1)
|
| 157 |
+
magnitude_v2 = np.linalg.norm(v2)
|
| 158 |
+
cos_theta = dot_product / (magnitude_v1 * magnitude_v2)
|
| 159 |
+
cos_theta = np.clip(cos_theta, -1.0, 1.0)
|
| 160 |
+
self.theta = np.arccos(cos_theta)
|
| 161 |
+
return np.degrees(self.theta)
|
| 162 |
+
|
| 163 |
+
# Function to classify palm orientation
|
| 164 |
+
def get_palm_orientation(self, normal):
|
| 165 |
+
angles = {axis: self.angle_between_vectors(normal, direction) for axis, direction in self.axes.items()}
|
| 166 |
+
# Find the axis with the smallest angle
|
| 167 |
+
self.best_match_axis = min(angles, key=angles.get)
|
| 168 |
+
return self.best_match_axis
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
# Function to extract hand features (angles between vectors and axes)
|
| 172 |
+
def extract_features(self, hand_landmarks, pose_landmarks=None):
|
| 173 |
+
hand_pairs = [
|
| 174 |
+
(1 , 3) , # Thumb
|
| 175 |
+
(6 , 8 ), # Index finger
|
| 176 |
+
(10, 12), # Middle finger
|
| 177 |
+
(14, 16), # Ring finger
|
| 178 |
+
(18, 20), # Pinky finger
|
| 179 |
+
(0 , 9) # Palm direction
|
| 180 |
+
]
|
| 181 |
+
|
| 182 |
+
self.features = []
|
| 183 |
+
for pair in hand_pairs:
|
| 184 |
+
landmark1 = hand_landmarks[pair[0]]
|
| 185 |
+
landmark2 = hand_landmarks[pair[1]]
|
| 186 |
+
|
| 187 |
+
vector = np.array([landmark2.x - landmark1.x, landmark2.y - landmark1.y, landmark2.z - landmark1.z])
|
| 188 |
+
x_axis = np.array([1, 0, 0])
|
| 189 |
+
y_axis = np.array([0, 1, 0])
|
| 190 |
+
z_axis = np.array([0, 0, 1])
|
| 191 |
+
|
| 192 |
+
angle_x = self.calculate_angle1(vector, x_axis)
|
| 193 |
+
angle_y = self.calculate_angle1(vector, y_axis)
|
| 194 |
+
angle_z = self.calculate_angle1(vector, z_axis)
|
| 195 |
+
|
| 196 |
+
self.features.extend([angle_x, angle_y, angle_z])
|
| 197 |
+
|
| 198 |
+
# Safe access to landmarks for 0, 5, and 17
|
| 199 |
+
vector_0_to_5 = self.get_coordinates_safe(hand_landmarks, 5) - self.get_coordinates_safe(hand_landmarks, 0)
|
| 200 |
+
vector_0_to_17 = self.get_coordinates_safe(hand_landmarks, 17) - self.get_coordinates_safe(hand_landmarks, 0)
|
| 201 |
+
|
| 202 |
+
normal_vector = np.cross(vector_0_to_5, vector_0_to_17)
|
| 203 |
+
|
| 204 |
+
normal_angle_x = self.calculate_angle1(normal_vector, x_axis)
|
| 205 |
+
normal_angle_y = self.calculate_angle1(normal_vector, y_axis)
|
| 206 |
+
normal_angle_z = self.calculate_angle1(normal_vector, z_axis)
|
| 207 |
+
|
| 208 |
+
self.features.extend([normal_angle_x, normal_angle_y, normal_angle_z])
|
| 209 |
+
|
| 210 |
+
# If pose landmarks are available, calculate the distance between nose and wrist
|
| 211 |
+
|
| 212 |
+
nose_landmark = self.get_coordinates_safe(pose_landmarks, 0) # Nose is at index 0 in pose landmarks
|
| 213 |
+
wrist_landmark = self.get_coordinates_safe(hand_landmarks, 0) # Wrist is at index 0 in hand landmarks
|
| 214 |
+
|
| 215 |
+
# Calculate the distance in the x and y axes
|
| 216 |
+
distance_x = abs(nose_landmark[0] - wrist_landmark[0])
|
| 217 |
+
distance_y = abs(nose_landmark[1] - wrist_landmark[1])
|
| 218 |
+
|
| 219 |
+
# Append the x and y distances as new features
|
| 220 |
+
self.features.extend([distance_x, distance_y])
|
| 221 |
+
|
| 222 |
+
return self.features
|
| 223 |
+
|
| 224 |
+
#Initialize pose extraction functions
|
| 225 |
+
def calculate_normal_safe(self,p1, p2, p3):
|
| 226 |
+
# Check if any of the points is [-1, -1, -1] (default value for missing landmarks)
|
| 227 |
+
if np.array_equal(p1, [-1, -1, -1]) or np.array_equal(p2, [-1, -1, -1]) or np.array_equal(p3, [-1, -1, -1]):
|
| 228 |
+
return np.array([-1, -1, -1]) # Return [-1, -1, -1] if any point is missing
|
| 229 |
+
else:
|
| 230 |
+
return self.calculate_normal(p1, p2, p3) # Otherwise, calculate the normal as usual
|
| 231 |
+
|
| 232 |
+
# Function to calculate angle between three points
|
| 233 |
+
def calculate_angle2(self,p1, p2, p3):
|
| 234 |
+
# Create vectors from points p1, p2, p3
|
| 235 |
+
v1 = p1 - p2
|
| 236 |
+
v2 = p3 - p2
|
| 237 |
+
|
| 238 |
+
# Calculate the cosine of the angle using dot product
|
| 239 |
+
self.cos_theta = np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2))
|
| 240 |
+
|
| 241 |
+
return self.cos_theta
|
| 242 |
+
|
| 243 |
+
# Function to calculate the normal of the plane formed by three points
|
| 244 |
+
def calculate_normal(self, p1, p2, p3):
|
| 245 |
+
# Vectors on the plane
|
| 246 |
+
v1 = p2 - p1
|
| 247 |
+
v2 = p3 - p1
|
| 248 |
+
|
| 249 |
+
# Cross product gives the normal vector
|
| 250 |
+
self.normal = np.cross(v1, v2)
|
| 251 |
+
|
| 252 |
+
# Normalize the normal vector
|
| 253 |
+
self.normal = self.normal / np.linalg.norm(self.normal)
|
| 254 |
+
|
| 255 |
+
return self.normal
|
| 256 |
+
|
| 257 |
+
# Function to calculate the angle between the normal and each of the axes
|
| 258 |
+
def calculate_normal_angles(self,normal):
|
| 259 |
+
# Calculate angles with x, y, z axes
|
| 260 |
+
self.cos_values = []
|
| 261 |
+
for axis in np.eye(3): # x, y, z unit vectors
|
| 262 |
+
cos_value = np.dot(normal, axis)
|
| 263 |
+
self.cos_values.append(cos_value)
|
| 264 |
+
return self.cos_values
|
| 265 |
+
|
| 266 |
+
# Function to calculate the x and y distance between two points
|
| 267 |
+
def calculate_xy_distance(self, p1, p2):
|
| 268 |
+
self.x_distance = abs(p1[0] - p2[0])
|
| 269 |
+
self.y_distance = abs(p1[1] - p2[1])
|
| 270 |
+
return self.x_distance, self.y_distance
|
| 271 |
+
|
| 272 |
+
# Function to extract the pose features
|
| 273 |
+
def extract_pose_features(self, landmarks):
|
| 274 |
+
# Define the landmark indices for the required sets of points (using Pose landmark indices)
|
| 275 |
+
points_sets = {
|
| 276 |
+
"angle_11_12_14": (self.get_coordinates_safe(landmarks, 11), self.get_coordinates_safe(landmarks, 12), self.get_coordinates_safe(landmarks, 14)), # Left shoulder, right shoulder, right elbow
|
| 277 |
+
"angle_12_14_16": (self.get_coordinates_safe(landmarks, 12), self.get_coordinates_safe(landmarks, 11), self.get_coordinates_safe(landmarks, 13)), # Right shoulder, right elbow, right wrist
|
| 278 |
+
"angle_11_13_15": (self.get_coordinates_safe(landmarks, 11), self.get_coordinates_safe(landmarks, 13), self.get_coordinates_safe(landmarks, 15)), # Left shoulder, left elbow, left wrist
|
| 279 |
+
"angle_13_15_17": (self.get_coordinates_safe(landmarks, 12), self.get_coordinates_safe(landmarks, 14), self.get_coordinates_safe(landmarks, 16)), # Left elbow, left wrist, left hand
|
| 280 |
+
"normal_1": (self.get_coordinates_safe(landmarks, 15), self.get_coordinates_safe(landmarks, 17), self.get_coordinates_safe(landmarks, 19)), # Plane formed by left shoulder, left hip, left knee
|
| 281 |
+
"normal_2": (self.get_coordinates_safe(landmarks, 16), self.get_coordinates_safe(landmarks, 18), self.get_coordinates_safe(landmarks, 20)) # Plane formed by right shoulder, right hip, right knee
|
| 282 |
+
}
|
| 283 |
+
|
| 284 |
+
# Calculate the angles between the specific sets of points
|
| 285 |
+
self.angles = []
|
| 286 |
+
for key, (p1, p2, p3) in points_sets.items():
|
| 287 |
+
if key.startswith("angle"):
|
| 288 |
+
angle = self.calculate_angle2(p1, p2, p3)
|
| 289 |
+
self.angles.append(angle)
|
| 290 |
+
|
| 291 |
+
# Calculate normals and angles with axes
|
| 292 |
+
for key, (p1, p2, p3) in points_sets.items():
|
| 293 |
+
if key.startswith("normal"):
|
| 294 |
+
normal = self.calculate_normal_safe(p1, p2, p3) # Safe normal calculation
|
| 295 |
+
if np.array_equal(normal, [-1, -1, -1]):
|
| 296 |
+
# If normal is [-1, -1, -1], it indicates missing points, so append [-1, -1, -1] for each axis angle
|
| 297 |
+
self.angles.extend([-1, -1, -1])
|
| 298 |
+
else:
|
| 299 |
+
normal_angles = self.calculate_normal_angles(normal)
|
| 300 |
+
self.angles.extend(normal_angles) # Append angles with x, y, z axes
|
| 301 |
+
|
| 302 |
+
# Add the distance between points 15 (left wrist) and 16 (right wrist)
|
| 303 |
+
p15 = self.get_coordinates_safe(landmarks, 15) # Left wrist
|
| 304 |
+
p16 = self.get_coordinates_safe(landmarks, 16) # Right wrist
|
| 305 |
+
x_distance, y_distance = self.calculate_xy_distance(p15, p16)
|
| 306 |
+
self.angles.extend([x_distance, y_distance]) # Append x and y distance to the feature list
|
| 307 |
+
|
| 308 |
return self.angles
|