Spaces:
Sleeping
Sleeping
Niharmahesh
commited on
Create landmarks.py
Browse files- landmarks.py +37 -0
landmarks.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
from numpy.linalg import norm
|
3 |
+
|
4 |
+
def normalize_landmarks(landmarks):
|
5 |
+
"""
|
6 |
+
Normalize the hand landmarks.
|
7 |
+
|
8 |
+
Parameters:
|
9 |
+
landmarks (numpy.ndarray): Array of hand landmarks.
|
10 |
+
|
11 |
+
Returns:
|
12 |
+
numpy.ndarray: Normalized landmarks.
|
13 |
+
"""
|
14 |
+
center = np.mean(landmarks, axis=0)
|
15 |
+
landmarks_centered = landmarks - center
|
16 |
+
std_dev = np.std(landmarks_centered, axis=0)
|
17 |
+
landmarks_normalized = landmarks_centered / std_dev
|
18 |
+
return np.nan_to_num(landmarks_normalized)
|
19 |
+
|
20 |
+
def calculate_angles(landmarks):
|
21 |
+
"""
|
22 |
+
Calculate angles between hand landmarks.
|
23 |
+
|
24 |
+
Parameters:
|
25 |
+
landmarks (numpy.ndarray): Array of hand landmarks.
|
26 |
+
|
27 |
+
Returns:
|
28 |
+
list: List of calculated angles.
|
29 |
+
"""
|
30 |
+
angles = []
|
31 |
+
for i in range(20):
|
32 |
+
for j in range(i + 1, 21):
|
33 |
+
vector = landmarks[j] - landmarks[i]
|
34 |
+
angle_x = np.arccos(np.clip(vector[0] / norm(vector), -1.0, 1.0))
|
35 |
+
angle_y = np.arccos(np.clip(vector[1] / norm(vector), -1.0, 1.0))
|
36 |
+
angles.extend([angle_x, angle_y])
|
37 |
+
return angles
|